module L10n::I18nExtensions::ClassMethods

Public Instance Methods

as(tmp_locale) { || ... } click to toggle source
# 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_default() { || ... } click to toggle source
# File lib/l10n/i18n_extensions.rb, line 60
def as_default
  I18n.as(I18n.default_locale) { yield }
end
as_each() { |code| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/l10n/i18n_extensions.rb, line 46
def available?(locale)
  available(locale) ? true : false
end
available_language_codes() click to toggle source
# 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) click to toggle source
# 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_code() click to toggle source
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

# File lib/l10n/i18n_extensions.rb, line 28
def default_language_code
  normalize_language_code(default_locale) if default_locale
end
language_code() click to toggle source
# File lib/l10n/i18n_extensions.rb, line 15
def language_code
  normalize_language_code(locale) if locale
end
translation_language_codes() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

normalize_language_code(code) click to toggle source

ISO 639-1 two-letter language code

# File lib/l10n/i18n_extensions.rb, line 81
def normalize_language_code(code)
  code.to_s[0..1].downcase.to_sym
end
normalize_locale_code(code) click to toggle source

deprecated

# File lib/l10n/i18n_extensions.rb, line 86
def normalize_locale_code(code)
  normalize_language_code(code)
end