Module: RouteTranslator::DictionaryManagement
- Included in:
- RouteTranslator
- Defined in:
- lib/route_translator.rb
Instance Method Summary collapse
-
#add_dictionary_from_file(file_path) ⇒ Object
Add translations from another file to the dictionary.
-
#init_i18n_dictionary(*wanted_locales) ⇒ Object
Init dictionary to use I18n to translate route parts.
-
#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: …
-
#merge_translations(locale, translations) ⇒ Object
Merge translations for a specified locale into the dictionary.
-
#yield_dictionary {|@dictionary| ... } ⇒ Object
Resets dictionary and yields the block wich can be used to manually fill the dictionary with translations e.g.
Instance Method Details
#add_dictionary_from_file(file_path) ⇒ Object
Add translations from another file to the dictionary.
102 103 104 105 106 107 108 |
# File 'lib/route_translator.rb', line 102 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.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/route_translator.rb', line 122 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'
96 97 98 99 |
# File 'lib/route_translator.rb', line 96 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
111 112 113 114 115 116 117 118 |
# File 'lib/route_translator.rb', line 111 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
79 80 81 82 83 |
# File 'lib/route_translator.rb', line 79 def yield_dictionary &block reset_dictionary yield @dictionary set_available_locales_from_dictionary end |