Module: L10n::I18nExtensions::ClassMethods

Defined in:
lib/l10n/i18n_extensions.rb

Instance Method Summary collapse

Instance Method Details

#as(tmp_locale) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/l10n/i18n_extensions.rb', line 50

def as(tmp_locale)
  if normalized_code = normalize_locale_code(tmp_locale)
    previous_locale = self.locale
    self.locale = normalized_code
  end
  yield
ensure
  self.locale = previous_locale if normalized_code
end

#as_defaultObject



60
61
62
# File 'lib/l10n/i18n_extensions.rb', line 60

def as_default
  I18n.as(I18n.default_locale) { yield }
end

#as_eachObject



64
65
66
# File 'lib/l10n/i18n_extensions.rb', line 64

def as_each
  available_language_codes.inject({}) { |hash, code| hash[code] = I18n.as(code) { yield(code) }; hash }
end

#available(locale) ⇒ Object



40
41
42
43
44
# File 'lib/l10n/i18n_extensions.rb', line 40

def available(locale)
  return nil if locale.blank?
  locale = normalize_locale_code(locale)
  available_locales.include?(locale) ? locale : nil
end

#available?(locale) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/l10n/i18n_extensions.rb', line 46

def available?(locale)
  available(locale) ? true : false
end

#available_language_codesObject



32
33
34
# File 'lib/l10n/i18n_extensions.rb', line 32

def available_language_codes
  available_locales.map { |locale| normalize_language_code(locale) }.uniq
end

#default?(language_code = I18n.language_code) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/l10n/i18n_extensions.rb', line 11

def default?(language_code = I18n.language_code)
  language_code.to_s == default_language_code.to_s
end

#default_language_codeObject

deprecated def country_code

if locale.to_s.match(/\w\w-\w\w/)
  locale.to_s[-2..-1]
else
  normalize_locale_code(locale)[-2..-1]
end

end



28
29
30
# File 'lib/l10n/i18n_extensions.rb', line 28

def default_language_code
  normalize_language_code(default_locale) if default_locale
end

#language_codeObject



15
16
17
# File 'lib/l10n/i18n_extensions.rb', line 15

def language_code
  normalize_language_code(locale) if locale
end

#translation_language_codesObject



36
37
38
# File 'lib/l10n/i18n_extensions.rb', line 36

def translation_language_codes
  I18n.available_language_codes - [I18n.default_language_code]
end

#translation_suffix(language_code = nil) ⇒ Object



72
73
74
75
76
# File 'lib/l10n/i18n_extensions.rb', line 72

def translation_suffix(language_code = nil)
  language_code = available(language_code) if language_code
  language_code ||= I18n.language_code
  default?(language_code) ? '' : "_#{language_code}"
end

#translations(key) ⇒ Object



68
69
70
# File 'lib/l10n/i18n_extensions.rb', line 68

def translations(key)
  available_language_codes.inject({}) { |hash, code| hash[code] = I18n.as(code) { I18n.t(key) }; hash }
end