Class: PrismicRails::LanguageService

Inherits:
Object
  • Object
show all
Defined in:
lib/prismic_rails/services/language_service.rb

Overview

The PrismicRails::LanguageService tries to match your i18n locale to the Prismic locale.

E.g. If your rails app support the locales :en, :de and :fr and you want to support this locales with your prismic content you have to match this locals. In Prismic you have the possibility to define a finer graded set of locals, e.g. ‘en-gb’, ‘en-us’, ‘de-de’, ‘de-ch’ and so on. Unfortunately Prismic does not support a wildcard query on the locals, so we have solved this problem by setting up a matching logic.

You can define the matching logic in your PrismicRails configureation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale) ⇒ LanguageService

Creates a instance of the class PrismicRails::LanguageService


33
34
35
# File 'lib/prismic_rails/services/language_service.rb', line 33

def initialize(locale)
  @locale = locale
end

Instance Attribute Details

#localeObject

Stores the given locale


20
21
22
# File 'lib/prismic_rails/services/language_service.rb', line 20

def locale
  @locale
end

Class Method Details

.call(locale, reverse: false) ⇒ Object

Calls the PrismicRails::LanguageService with a i18n locale form rails. The PrismicRails::LanguageService tries to match it into a prismic locale.


24
25
26
27
28
29
30
# File 'lib/prismic_rails/services/language_service.rb', line 24

def self.call(locale, reverse: false)
  if reverse
    new(locale).reverse()
  else
    new(locale).match()
  end
end

Instance Method Details

#language_hashObject

Creates the language hash out of the PrismicRails::Config


48
49
50
# File 'lib/prismic_rails/services/language_service.rb', line 48

def language_hash
  HashWithIndifferentAccess.new(PrismicRails.config.languages)
end

#matchObject

Tries to match the given locale to a prismic locale


38
39
40
# File 'lib/prismic_rails/services/language_service.rb', line 38

def match
  return language_hash[@locale.to_sym] || '*'
end

#reverseObject

Tries to reverse match the prismic locale to a rails locale


43
44
45
# File 'lib/prismic_rails/services/language_service.rb', line 43

def reverse
  return language_hash.key(@locale)
end