Module: CouchI18n

Defined in:
lib/couch_i18n.rb,
lib/couch_i18n/store.rb,
lib/couch_i18n/engine.rb,
lib/couch_i18n/backend.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

Defined Under Namespace

Modules: ApplicationHelper Classes: ApplicationController, Backend, Engine, Store, Translation, TranslationsController

Class Method Summary collapse

Class Method Details

.cache_allObject

Add all translations to the cache to avoid one by one loading and caching



70
71
72
73
74
# File 'lib/couch_i18n.rb', line 70

def self.cache_all
  CouchI18n::Translation.all.each do |t|
    Rails.cache.write("couch_i18n-#{t.key}", t.value)
  end
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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/couch_i18n.rb', line 8

def self.import_from_yaml(options = {})
  options = {:override_existing => true} if options.is_a?(TrueClass)
  options = {:override_existing => false} if options.is_a?(FalseClass)
  options = {:override_existing => false}.merge!(options)
  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.key == key}
    if available_translation && options[:override_existing]
      available_translation.value = value
      available_translation.translated = true
      available_translation.save
    else
      available_translation = CouchI18n::Translation.create :key => key, :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


51
52
53
# File 'lib/couch_i18n.rb', line 51

def self.indent_keys(selection = CouchI18n::Translation.all)
  traverse_indent_keys(selection.map{|kv| [kv.key.split('.'), kv.value]})
end

.traverse_flatten_keys(obj, store = {}, path = nil) ⇒ Object

Recursive flattening.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/couch_i18n.rb', line 33

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



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/couch_i18n.rb', line 56

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