Class: PrismicRails::LanguageService
- Inherits:
-
Object
- Object
- PrismicRails::LanguageService
- 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
-
#locale ⇒ Object
Stores the given locale.
Class Method Summary collapse
-
.call(locale, reverse: false) ⇒ Object
Calls the PrismicRails::LanguageService with a i18n locale form rails.
Instance Method Summary collapse
-
#initialize(locale) ⇒ LanguageService
constructor
Creates a instance of the class PrismicRails::LanguageService.
-
#language_hash ⇒ Object
Creates the language hash out of the PrismicRails::Config.
-
#match ⇒ Object
Tries to match the given locale to a prismic locale.
-
#reverse ⇒ Object
Tries to reverse match the prismic locale to a rails locale.
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
#locale ⇒ Object
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_hash ⇒ Object
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 |
#match ⇒ Object
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 |
#reverse ⇒ Object
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 |