Module: RailsTranslateRoutes::DictionaryManagement
- Included in:
- RailsTranslateRoutes
- Defined in:
- lib/rails-translate-routes.rb
Instance Method Summary collapse
-
#add_dictionary_from_file(file_path) ⇒ Object
Add translations from another file to the dictionary (supports a single file or an array to have translations splited in several folders).
-
#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 (supports a single file or an array to have translations splited in several folders)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rails-translate-routes.rb', line 132 def add_dictionary_from_file file_path file_path.class == Array ? files = file_path : files = [ file_path ] yaml = Hash.new files.each do |file| yaml.merge! YAML.load_file(File.join(Rails.root, file)) end yaml.each_pair do |locale, translations| merge_translations locale, translations['routes'] 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.
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/rails-translate-routes.rb', line 159 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'
126 127 128 129 |
# File 'lib/rails-translate-routes.rb', line 126 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
148 149 150 151 152 153 154 155 |
# File 'lib/rails-translate-routes.rb', line 148 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 = RailsTranslateRoutes.new
route_translator.yield_dictionary do |dict|
dict['en'] = { 'people' => 'people' }
dict['de'] = { 'people' => 'personen' }
end
109 110 111 112 113 |
# File 'lib/rails-translate-routes.rb', line 109 def yield_dictionary &block reset_dictionary yield @dictionary set_available_locales_from_dictionary end |