Module: Stringex::Localization::Backend::I18n

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



16
17
18
# File 'lib/stringex/localization/backend/i18n.rb', line 16

def default_locale
  ::I18n.default_locale
end

.default_locale=(new_locale) ⇒ Object



20
21
22
# File 'lib/stringex/localization/backend/i18n.rb', line 20

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

.initial_translation(scope, key, locale) ⇒ Object



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

def initial_translation(scope, key, locale)
  # I18n can't return a nil as default as this gets interpreted as if no default
  # is specified, so we use a string instead.
  translated = ::I18n.translate(key, :scope => [:stringex, scope], :locale => locale, :default => "__default__")
  translated == "__default__" ? nil : translated
end

.load_translations(locale = nil) ⇒ Object



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

def load_translations(locale = nil)
  locale ||= ::I18n.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
end

.localeObject



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

def locale
  ::I18n.locale
end

.locale=(new_locale) ⇒ Object



12
13
14
# File 'lib/stringex/localization/backend/i18n.rb', line 12

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

.reset!Object



46
47
48
# File 'lib/stringex/localization/backend/i18n.rb', line 46

def reset!
  # Can't reset I18n. Needed?
end

.store_translations(locale, scope, data) ⇒ Object



28
29
30
# File 'lib/stringex/localization/backend/i18n.rb', line 28

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

.with_locale(new_locale, &block) ⇒ Object



24
25
26
# File 'lib/stringex/localization/backend/i18n.rb', line 24

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