Module: CouchI18n
- Defined in:
- lib/couch_i18n.rb,
lib/couch_i18n/store.rb,
lib/couch_i18n/engine.rb,
lib/couch_i18n/backend.rb,
lib/couch_i18n/version.rb,
app/models/couch_i18n/translation.rb,
app/helpers/couch_i18n/application_helper.rb,
app/controllers/couch_i18n/application_controller.rb,
app/controllers/couch_i18n/translations_controller.rb
Overview
Ugly fix for the updated json gem changes module JSON
class << self
alias :old_parse :parse
def parse(json, args = {})
args[:create_additions] = true
old_parse(json, args)
end
end
end
Defined Under Namespace
Modules: ApplicationHelper Classes: ApplicationController, Backend, Engine, Store, Translation, TranslationsController
Constant Summary collapse
- VERSION =
"0.3.1"- @@__missing_key_handler =
nil
Class Method Summary collapse
- .handle_missing_key(key) ⇒ Object
-
.import_from_yaml(options = {}) ⇒ Object
This method imports yaml translations to the couchdb version.
-
.indent_keys(selection = CouchI18n::Translation.all) ⇒ Object
This will return an indented strucutre of a collection of stores.
- .missing_key(&blk) ⇒ Object
- .missing_key_handler ⇒ Object
-
.traverse_flatten_keys(obj, store = {}, path = nil) ⇒ Object
Recursive flattening.
-
.traverse_indent_keys(ary, h = {}) ⇒ Object
Traversing helper for indent_keys.
Class Method Details
.handle_missing_key(key) ⇒ Object
83 84 85 |
# File 'lib/couch_i18n.rb', line 83 def self.handle_missing_key(key) missing_key_handler.call(key) if missing_key_handler end |
.import_from_yaml(options = {}) ⇒ Object
This method imports yaml translations to the couchdb version. When run again new ones will be added. Translations already stored in the couchdb database are not overwritten if true or ovveride_existing: true is given
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/couch_i18n.rb', line 22 def self.import_from_yaml( = {}) = {:override_existing => true} if .is_a?(TrueClass) = {:override_existing => false} if .is_a?(FalseClass) = {:override_existing => false}.merge!() raise "I18.backend not a I18n::Backend::Chain" unless I18n.backend.is_a?(I18n::Backend::Chain) # yml_backend = I18n.backend.backends.last raise "Last backend not a I18n::Backend::Simple" unless yml_backend.is_a?(I18n::Backend::Simple) yml_backend.load_translations flattened_hash = traverse_flatten_keys(yml_backend.send(:translations)) available_translations = CouchI18n::Translation.all flattened_hash.each do |key, value| available_translation = available_translations.find{|t| t.translation_key == key} if available_translation && [:override_existing] available_translation.translation_value = value available_translation.translated = true available_translation.save else available_translation = CouchI18n::Translation.create translation_key: key, translation_value: value end end end |
.indent_keys(selection = CouchI18n::Translation.all) ⇒ Object
This will return an indented strucutre of a collection of stores. If no argument is given by default all the translations are indented. So a command:
CouchI18n.indent_keys.to_yaml will return one big yaml string of the translations
65 66 67 |
# File 'lib/couch_i18n.rb', line 65 def self.indent_keys(selection = CouchI18n::Translation.all) traverse_indent_keys(selection.map{|kv| [kv.translation_key.split('.'), kv.translation_value]}) end |
.missing_key(&blk) ⇒ Object
91 92 93 94 95 |
# File 'lib/couch_i18n.rb', line 91 def self.missing_key(&blk) raise "Assign a block to handle a missing key" unless blk.present? raise "The missing key proc should handle the key as argument CouchI18n.missing_key{ |key| .... }" unless blk.arity == 1 @@__missing_key_handler = blk end |
.missing_key_handler ⇒ Object
87 88 89 |
# File 'lib/couch_i18n.rb', line 87 def self.missing_key_handler @@__missing_key_handler end |
.traverse_flatten_keys(obj, store = {}, path = nil) ⇒ Object
Recursive flattening.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/couch_i18n.rb', line 47 def self.traverse_flatten_keys(obj, store = {}, path = nil) case obj when Hash obj.each{|k, v| traverse_flatten_keys(v, store, [path, k].compact.join('.'))} when Array # Do not store array for now. There is no good solution yet store[path] = obj # Keeyp arrays intact # store[path] = obj.to_json # obj.each_with_index{|v, i| traverse_flatten_keys(store, v, [path, i].compact.join('.'))} else store[path] = obj end return store end |
.traverse_indent_keys(ary, h = {}) ⇒ Object
Traversing helper for indent_keys
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/couch_i18n.rb', line 70 def self.traverse_indent_keys(ary, h = {}) for pair in ary if pair.first.size == 1 h[pair.first.first] = pair.last else h[pair.first.first] ||= {} next unless h[pair.first.first].is_a?(Hash) # In case there is a translation en.a: A, en.a.b: B this is invalid traverse_indent_keys([[pair.first[1..-1], pair.last]], h[pair.first.first]) end end h end |