class I18nAdmin::Translations

Public Class Methods

for_locale(locale) click to toggle source
# File lib/i18n_admin/translations.rb, line 20
def self.for_locale(locale)
  new.for_locale(locale.to_sym)
end
translations_for(locale) click to toggle source
# File lib/i18n_admin/translations.rb, line 5
def self.translations_for(locale)
  translations = TranslationCollection.new

  for_locale(locale).each do |key, value|
    translations << Translation.new(
      key: key,
      original: for_locale(I18n.default_locale)[key],
      value: value,
      locale: locale
    )
  end

  translations
end
update(translation) click to toggle source

Save a translation object

# File lib/i18n_admin/translations.rb, line 25
def self.update(translation)
  I18n.backend.store_translations(
    translation.locale,
    translation.key => translation.value
  )

  if translation.locale == I18n.default_locale
    translation.original = translation.value
  end
end

Public Instance Methods

all_translations_for(locale) click to toggle source
# File lib/i18n_admin/translations.rb, line 44
def all_translations_for(locale)
  request_store.store[store_key_for(locale, :hash)] ||= backends.map do |backend|
    translations_for(locale, backend)
  end.reduce(&:reverse_merge)
end
backends() click to toggle source
# File lib/i18n_admin/translations.rb, line 50
def backends
  @backends ||= I18n.backend.backends
end
elegible_key?(key) click to toggle source
# File lib/i18n_admin/translations.rb, line 78
def elegible_key?(key)
  if (pattern = I18nAdmin.excluded_keys_pattern)
    !key.match(pattern)
  else
    true
  end
end
flatten_translation_hash(hash, scope = nil, translations = {}) click to toggle source
# File lib/i18n_admin/translations.rb, line 66
def flatten_translation_hash(hash, scope = nil, translations = {})
  hash.each_with_object(translations) do |(key, value), buffer|
    scoped_key = [scope, key.to_s].compact.join('.')

    if value.is_a?(Hash)
      flatten_translation_hash(value, scoped_key, buffer)
    elsif elegible_key?(scoped_key)
      buffer[scoped_key] = value
    end
  end
end
for_locale(locale = I18n.locale) click to toggle source
# File lib/i18n_admin/translations.rb, line 36
def for_locale(locale = I18n.locale)
  translations = with_empty_keys_for(locale, all_translations_for(locale))

  translations.keys.sort.each_with_object({}) do |key, hash|
    hash[key] = translations[key]
  end
end
parse_from_deep_hash(locale, translations) click to toggle source
# File lib/i18n_admin/translations.rb, line 62
def parse_from_deep_hash(locale, translations)
  flatten_translation_hash(translations[locale] || {})
end
translations_for(locale, backend) click to toggle source
# File lib/i18n_admin/translations.rb, line 54
def translations_for(locale, backend)
  translations = if backend.protected_methods.include?(:translations)
    parse_from_deep_hash(locale, backend.send(:translations))
  elsif backend.respond_to?(:store)
    backend.store.translations_for(locale)
  end
end
with_empty_keys_for(locale, hash) click to toggle source
# File lib/i18n_admin/translations.rb, line 86
def with_empty_keys_for(locale, hash)
  return hash if I18n.default_locale == locale

  all_translations_for(I18n.default_locale).keys.each do |key|
    hash[key] = "" unless hash.key?(key)
  end

  hash
end