Class: Stringex::Localization::Backend::I18n
- Defined in:
- lib/stringex/localization/backend/i18n.rb
Constant Summary collapse
- LOAD_PATH_BASE =
File.join(File.(File.dirname(__FILE__)), '..', '..', '..', '..', 'locales')
Class Method Summary collapse
- .default_locale ⇒ Object
- .default_locale=(new_locale) ⇒ Object
- .ensure_locales_enforced_or_not ⇒ Object
- .i18n_translations_for(locale) ⇒ Object
- .initial_translation(scope, key, locale) ⇒ Object
- .load_translations(locale = nil) ⇒ Object
- .locale ⇒ Object
- .locale=(new_locale) ⇒ Object
- .reset! ⇒ Object
- .reset_translations_cache ⇒ Object
- .store_translations(locale, scope, data) ⇒ Object
- .translations ⇒ Object
- .with_locale(new_locale, &block) ⇒ Object
Class Method Details
.default_locale ⇒ Object
21 22 23 |
# File 'lib/stringex/localization/backend/i18n.rb', line 21 def default_locale ::I18n.default_locale end |
.default_locale=(new_locale) ⇒ Object
25 26 27 |
# File 'lib/stringex/localization/backend/i18n.rb', line 25 def default_locale=(new_locale) ::I18n.default_locale = new_locale end |
.ensure_locales_enforced_or_not ⇒ Object
64 65 66 67 68 69 |
# File 'lib/stringex/localization/backend/i18n.rb', line 64 def ensure_locales_enforced_or_not return unless ::I18n.respond_to?(:enforce_available_locales) # Allow users to have set this to false manually but default to true return unless ::I18n.enforce_available_locales == nil ::I18n.enforce_available_locales = ::I18n.available_locales != [] end |
.i18n_translations_for(locale) ⇒ Object
55 56 57 58 |
# File 'lib/stringex/localization/backend/i18n.rb', line 55 def i18n_translations_for(locale) ensure_locales_enforced_or_not ::I18n.translate("stringex", :locale => locale, :default => {}) end |
.initial_translation(scope, key, locale) ⇒ Object
43 44 45 |
# File 'lib/stringex/localization/backend/i18n.rb', line 43 def initial_translation(scope, key, locale) translations[locale][scope][key.to_sym] end |
.load_translations(locale = nil) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/stringex/localization/backend/i18n.rb', line 47 def load_translations(locale = nil) locale ||= self.locale path = Dir[File.join(LOAD_PATH_BASE, "#{locale}.yml")] ::I18n.load_path |= Dir[File.join(LOAD_PATH_BASE, "#{locale}.yml")] ::I18n.backend.load_translations reset_translations_cache end |
.locale ⇒ Object
13 14 15 |
# File 'lib/stringex/localization/backend/i18n.rb', line 13 def locale @locale || ::I18n.locale end |
.locale=(new_locale) ⇒ Object
17 18 19 |
# File 'lib/stringex/localization/backend/i18n.rb', line 17 def locale=(new_locale) @locale = new_locale end |
.reset! ⇒ Object
8 9 10 11 |
# File 'lib/stringex/localization/backend/i18n.rb', line 8 def reset! super ::I18n.reload! if defined?(::I18n) && ::I18n.respond_to?(:reload!) end |
.reset_translations_cache ⇒ Object
60 61 62 |
# File 'lib/stringex/localization/backend/i18n.rb', line 60 def reset_translations_cache @translations = nil end |
.store_translations(locale, scope, data) ⇒ Object
33 34 35 36 |
# File 'lib/stringex/localization/backend/i18n.rb', line 33 def store_translations(locale, scope, data) ::I18n.backend.store_translations(locale, { :stringex => { scope => data } }) reset_translations_cache end |
.translations ⇒ Object
38 39 40 41 |
# File 'lib/stringex/localization/backend/i18n.rb', line 38 def translations # Set up hash like translations[:en][:transliterations]["é"] @translations ||= Hash.new { |hsh, locale| hsh[locale] = Hash.new({}).merge(i18n_translations_for(locale)) } end |
.with_locale(new_locale, &block) ⇒ Object
29 30 31 |
# File 'lib/stringex/localization/backend/i18n.rb', line 29 def with_locale(new_locale, &block) ::I18n.with_locale new_locale, &block end |