Class: Stringex::Localization::Backend::I18n

Inherits:
Base
  • Object
show all
Defined in:
lib/stringex/localization/backend/i18n.rb

Constant Summary collapse

LOAD_PATH_BASE =
File.join(File.expand_path(File.dirname(__FILE__)), '..', '..', '..', '..', 'locales')

Class Method Summary collapse

Class Method Details

.default_localeObject


22
23
24
# File 'lib/stringex/localization/backend/i18n.rb', line 22

def default_locale
  ::I18n.default_locale
end

.default_locale=(new_locale) ⇒ Object


26
27
28
# File 'lib/stringex/localization/backend/i18n.rb', line 26

def default_locale=(new_locale)
  ::I18n.default_locale = new_locale
end

.ensure_locales_enforced_or_notObject


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


44
45
46
# File 'lib/stringex/localization/backend/i18n.rb', line 44

def initial_translation(scope, key, locale)
  translations[locale][scope][key.to_sym]
end

.load_translations(locale = nil) ⇒ Object


48
49
50
51
52
53
# File 'lib/stringex/localization/backend/i18n.rb', line 48

def load_translations(locale = nil)
  locale ||= self.locale
  ::I18n.load_path |= Dir[File.join(LOAD_PATH_BASE, "#{locale}.yml")]
  ::I18n.backend.load_translations
  reset_translations_cache
end

.localeObject


14
15
16
# File 'lib/stringex/localization/backend/i18n.rb', line 14

def locale
  @locale || ::I18n.locale
end

.locale=(new_locale) ⇒ Object


18
19
20
# File 'lib/stringex/localization/backend/i18n.rb', line 18

def locale=(new_locale)
  @locale = new_locale
end

.reset!Object


8
9
10
11
12
# File 'lib/stringex/localization/backend/i18n.rb', line 8

def reset!
  super
  @locale = nil
  ::I18n.reload! if defined?(::I18n) && ::I18n.respond_to?(:reload!)
end

.reset_translations_cacheObject


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


34
35
36
37
# File 'lib/stringex/localization/backend/i18n.rb', line 34

def store_translations(locale, scope, data)
  ::I18n.backend.store_translations(locale, {stringex: {scope => data}})
  reset_translations_cache
end

.translationsObject


39
40
41
42
# File 'lib/stringex/localization/backend/i18n.rb', line 39

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


30
31
32
# File 'lib/stringex/localization/backend/i18n.rb', line 30

def with_locale(new_locale, &block)
  ::I18n.with_locale new_locale, &block
end