Class: I18nAdmin::Translations

Inherits:
Object
  • Object
show all
Includes:
RequestStore
Defined in:
lib/i18n_admin/translations.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestStore

#request_store, #store_key_for

Class Method Details

.for_locale(locale) ⇒ Object



20
21
22
# File 'lib/i18n_admin/translations.rb', line 20

def self.for_locale(locale)
  new.for_locale(locale.to_sym)
end

.translations_for(locale) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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) ⇒ Object

Save a translation object



25
26
27
28
29
30
31
32
33
34
# 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

Instance Method Details

#all_translations_for(locale) ⇒ Object



44
45
46
47
48
# 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

#backendsObject



50
51
52
# File 'lib/i18n_admin/translations.rb', line 50

def backends
  @backends ||= I18n.backend.backends
end

#elegible_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# 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 = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



36
37
38
39
40
41
42
# 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) ⇒ Object



62
63
64
# 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) ⇒ Object



54
55
56
57
58
59
60
# 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) ⇒ Object



86
87
88
89
90
91
92
93
94
# 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