module RailsTranslateRoutes::DictionaryManagement
Public Instance Methods
add_dictionary_from_file(file_path)
click to toggle source
Add translations from another file to the dictionary (supports a single file or an array to have translations splited in several folders)
# File lib/rails-translate-routes.rb, line 140 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)
click to toggle source
Init dictionary to use I18n to translate route parts. Creates a hash with a block for each locale to lookup keys in I18n dynamically.
# File lib/rails-translate-routes.rb, line 167 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)
click to toggle source
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'
# File lib/rails-translate-routes.rb, line 134 def load_dictionary_from_file file_path reset_dictionary add_dictionary_from_file file_path end
merge_translations(locale, translations)
click to toggle source
Merge translations for a specified locale into the dictionary
# File lib/rails-translate-routes.rb, line 156 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| ... }
click to toggle source
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
# File lib/rails-translate-routes.rb, line 117 def yield_dictionary &block reset_dictionary yield @dictionary set_available_locales_from_dictionary end
Private Instance Methods
reset_dictionary()
click to toggle source
Resets dictionary
# File lib/rails-translate-routes.rb, line 184 def reset_dictionary @dictionary = { default_locale => {}} end
set_available_locales_from_dictionary()
click to toggle source
# File lib/rails-translate-routes.rb, line 179 def set_available_locales_from_dictionary @available_locales = @dictionary.keys.map &:to_s end