Module: RouteTranslator::RouteSet::DictionaryManagement

Included in:
RouteTranslator::RouteSet
Defined in:
lib/route_translator/route_set/dictionary_management.rb

Instance Method Summary collapse

Instance Method Details

#add_dictionary_from_file(file_path) ⇒ Object

Add translations from another file to the dictionary.



34
35
36
37
38
39
40
# File 'lib/route_translator/route_set/dictionary_management.rb', line 34

def add_dictionary_from_file file_path
  yaml = YAML.load_file(file_path)
  yaml.each_pair do |locale, translations|
    merge_translations locale, translations
  end
  set_available_locales_from_dictionary
end

#init_i18n_dictionary(*wanted_locales) ⇒ Object

Init dictionary to use I18n to translate route parts. Creates a hash with a block for each locale to lookup keys in I18n dynamically.



54
55
56
57
58
59
60
61
62
63
# File 'lib/route_translator/route_set/dictionary_management.rb', line 54

def init_i18n_dictionary *wanted_locales
  wanted_locales = available_locales if wanted_locales.blank?
  reset_dictionary
  wanted_locales.each do |locale|
    @dictionary[locale] = Hash.new do |hsh, key|
      hsh[key] = I18n.translate key, :locale => locale #DISCUSS: caching or no caching (store key and translation in dictionary?)
    end
  end
  @available_locales = @dictionary.keys.map &:to_s
end

#load_dictionary_from_file(file_path) ⇒ Object

Resets dictionary and loads translations from specified file config/locales/routes.yml:

en:
  people: people
de:
  people: personen

routes.rb:

... your routes ...
ActionDispatch::Routing::Translator.translate_from_file

or, to specify a custom file

ActionDispatch::Routing::Translator.translate_from_file 'config', 'locales', 'routes.yml'


28
29
30
31
# File 'lib/route_translator/route_set/dictionary_management.rb', line 28

def load_dictionary_from_file file_path
  reset_dictionary
  add_dictionary_from_file file_path
end

#merge_translations(locale, translations) ⇒ Object

Merge translations for a specified locale into the dictionary



43
44
45
46
47
48
49
50
# File 'lib/route_translator/route_set/dictionary_management.rb', line 43

def merge_translations locale, translations
  locale = locale.to_s
  if translations.blank?
    @dictionary[locale] ||= {}
    return
  end
  @dictionary[locale] = (@dictionary[locale] || {}).merge(translations)
end

#yield_dictionary {|@dictionary| ... } ⇒ Object

Resets dictionary and yields the block wich can be used to manually fill the dictionary with translations e.g.

route_translator = RouteTranslator.new
route_translator.yield_dictionary do |dict|
  dict['en'] = { 'people' => 'people' }
  dict['de'] = { 'people' => 'personen' }
end

Yields:



11
12
13
14
15
# File 'lib/route_translator/route_set/dictionary_management.rb', line 11

def yield_dictionary &block
  reset_dictionary
  yield @dictionary
  set_available_locales_from_dictionary
end