Class: Translate::Keys
- Inherits:
-
Object
- Object
- Translate::Keys
- Defined in:
- lib/translate/keys.rb
Class Method Summary collapse
-
.arraylize(input_hash) ⇒ Object
Convert something like:.
-
.contains_key?(hash, key) ⇒ Boolean
Checks if a nested hash contains the keys in dot separated I18n key.
-
.deep_merge!(hash1, hash2) ⇒ Object
deep_merge by Stefan Rusterholz, see www.ruby-forum.com/topic/142809.
-
.files ⇒ Object
Allows keys extracted from lookups in files to be cached.
-
.files=(files) ⇒ Object
Allows flushing of the files cache.
-
.to_deep_hash(hash) ⇒ Object
Convert something like:.
-
.to_shallow_hash(hash) ⇒ Object
Convert something like:.
- .translated_locales ⇒ Object
Instance Method Summary collapse
- #duplicate_keys ⇒ Object
- #files ⇒ Object (also: #to_hash)
- #i18n_keys(locale) ⇒ Object
- #keys ⇒ Object (also: #to_a)
- #missing_keys ⇒ Object
- #untranslated_keys ⇒ Object
Class Method Details
.arraylize(input_hash) ⇒ Object
Convert something like:
=> “elem 1”, ‘1’ => “elem 2”
to:
- “elem 1”, “elem 2”
148 149 150 151 152 153 |
# File 'lib/translate/keys.rb', line 148 def self.arraylize(input_hash) input_hash.inject([]) do |constructed_array, (key, value)| constructed_array << value constructed_array end end |
.contains_key?(hash, key) ⇒ Boolean
Checks if a nested hash contains the keys in dot separated I18n key.
Example:
hash = {
:foo => {
:bar => {
:baz => 1
}
}
}
contains_key?(“foo”, key) # => true contains_key?(“foo.bar”, key) # => true contains_key?(“foo.bar.baz”, key) # => true contains_key?(“foo.bar.baz.bla”, key) # => false
76 77 78 79 80 81 82 |
# File 'lib/translate/keys.rb', line 76 def self.contains_key?(hash, key) keys = key.to_s.split(".") return false if keys.empty? !keys.inject(HashWithIndifferentAccess.new(hash)) do |memo, key| memo.is_a?(Hash) ? memo.try(:[], key) : nil end.nil? end |
.deep_merge!(hash1, hash2) ⇒ Object
deep_merge by Stefan Rusterholz, see www.ruby-forum.com/topic/142809
135 136 137 138 |
# File 'lib/translate/keys.rb', line 135 def self.deep_merge!(hash1, hash2) merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 } hash1.merge!(hash2, &merger) end |
.files ⇒ Object
Allows keys extracted from lookups in files to be cached
5 6 7 |
# File 'lib/translate/keys.rb', line 5 def self.files @@files ||= Translate::Keys.new.files end |
.files=(files) ⇒ Object
Allows flushing of the files cache
10 11 12 |
# File 'lib/translate/keys.rb', line 10 def self.files=(files) @@files = files end |
.to_deep_hash(hash) ⇒ Object
Convert something like:
{'pressrelease.label.one' => "Pressmeddelande"}
to:
{
:pressrelease => {
:label => {
:one => "Pressmeddelande"
}
}
}
124 125 126 127 128 129 130 131 132 |
# File 'lib/translate/keys.rb', line 124 def self.to_deep_hash(hash) hash.inject({}) do |deep_hash, (key, value)| keys = key.to_s.split('.').reverse leaf_key = keys.shift key_hash = keys.inject({leaf_key.to_sym => value}) { |hash, key| {key.to_sym => hash} } deep_merge!(deep_hash, key_hash) deep_hash end end |
.to_shallow_hash(hash) ⇒ Object
Convert something like:
{
:pressrelease => {
:label => {
:one => "Pressmeddelande"
}
}
}
to:
{'pressrelease.label.one' => "Pressmeddelande"}
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/translate/keys.rb', line 98 def self.to_shallow_hash(hash) hash.inject({}) do |shallow_hash, (key, value)| if value.is_a?(Hash) to_shallow_hash(value).each do |sub_key, sub_value| shallow_hash[[key, sub_key].join(".")] = sub_value end else shallow_hash[key.to_s] = value end shallow_hash end end |
.translated_locales ⇒ Object
55 56 57 |
# File 'lib/translate/keys.rb', line 55 def self.translated_locales I18n.available_locales.reject { |locale| [:root, I18n.default_locale.to_sym].include?(locale) } end |
Instance Method Details
#duplicate_keys ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/translate/keys.rb', line 29 def duplicate_keys Translate::Keys.translated_locales.inject({}) do |missing, locale| missing[locale] = i18n_keys(I18n.default_locale).map do |key| I18n.backend.send(:lookup, locale, key) == I18n.backend.send(:lookup, I18n.default_locale, key) ? key : nil end.compact missing end end |
#files ⇒ Object Also known as: to_hash
14 15 16 |
# File 'lib/translate/keys.rb', line 14 def files @files ||= extract_files end |
#i18n_keys(locale) ⇒ Object
24 25 26 27 |
# File 'lib/translate/keys.rb', line 24 def i18n_keys(locale) I18n.backend.send(:init_translations) unless I18n.backend.initialized? Translate::Keys.to_shallow_hash(I18n.backend.send(:translations)[locale.to_sym]).keys.sort end |
#keys ⇒ Object Also known as: to_a
19 20 21 |
# File 'lib/translate/keys.rb', line 19 def keys files.keys end |
#missing_keys ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/translate/keys.rb', line 47 def missing_keys locale = I18n.default_locale; yaml_keys = {} yaml_keys = Translate::Storage.file_paths(locale).inject({}) do |keys, path| keys = keys.deep_merge(Translate::File.new(path).read[locale.to_s]) end files.reject { |key, file| self.class.contains_key?(yaml_keys, key) } end |
#untranslated_keys ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/translate/keys.rb', line 38 def untranslated_keys Translate::Keys.translated_locales.inject({}) do |missing, locale| missing[locale] = i18n_keys(I18n.default_locale).map do |key| I18n.backend.send(:lookup, locale, key).nil? ? key : nil end.compact missing end end |