Class: InplaceI18n::Translation
- Inherits:
-
Object
- Object
- InplaceI18n::Translation
- Defined in:
- app/model/inplace_i18n/translation.rb
Class Method Summary collapse
- .all(locale) ⇒ Object
- .merge_locales(locales_array, only_missing = false) ⇒ Object
- .update_by_path(locale, path, value) ⇒ Object
Class Method Details
.all(locale) ⇒ Object
7 8 9 10 11 |
# File 'app/model/inplace_i18n/translation.rb', line 7 def self.all(locale) locale = locale.to_s file = File.join(Rails.root, "config/locales","#{locale}.yml") hash = YAML::load_file file end |
.merge_locales(locales_array, only_missing = false) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/model/inplace_i18n/translation.rb', line 25 def self.merge_locales locales_array, only_missing = false #available_locales = I18n.available_locales.map{|loc| loc.to_s} #locales_array.each{|loc| raise "#{loc} locale not found" if not available_locales.include? loc.to_s } paths = [] translations = {} locales_array.each do |locale| translations[locale] = translation = Translation.all(locale) paths = paths + translation.paths.map{|path| path.gsub(/^#{locale.to_s}\./,"")} end paths = paths.uniq aggreg = {} paths.each do |path| aggreg[path] = {} locales_array.each do |locale| aggreg[path][locale] = translations[locale].find_by_path(locale.to_s + '.' + path) end end if only_missing return aggreg.delete_if do |path,translation| ret = true translation.each do |language, value| ret = false if value.blank? end ret end else return aggreg end end |
.update_by_path(locale, path, value) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/model/inplace_i18n/translation.rb', line 13 def self.update_by_path(locale,path,value) locale = locale.to_s file = File.join(Rails.root, "config/locales","#{locale}.yml") hash = YAML::load_file file hash.update_by_path(path,value) File.open(file + ".new",'w') do |f| f.puts hash.ya2yaml end FileUtils.mv(file + ".new", file) end |