module R18n::Translated::Base
Module with class methods, which be added after R18n::Translated
include.
Public Instance Methods
translation(name, options = {})
click to toggle source
Add proxy-method `name`. See `R18n::Translated` for description. It's more useful to set options.
translation :description, type: 'markdown'
# File lib/r18n-core/translated.rb, line 112 def translation(name, options = {}) if options[:methods] @unlocalized_getters[name] = options[:methods].map { |l, i| [l.to_s, i.to_s] }.to_h unless options[:no_write] @unlocalized_setters[name] = options[:methods].map { |l, i| [l.to_s, "#{i}="] }.to_h end end translation_types[name] = options[:type] define_method name do |*params| unlocalized = self.class.unlocalized_getters(name) result = nil r18n.locales.each do |locale| code = locale.downcased_code next unless unlocalized.key? code result = send( unlocalized[code], *(params unless options[:no_params]) ) next unless result path = "#{self.class.name}##{name}" type = self.class.translation_types[name] if type return r18n.filter_list.process( :all, type, result, locale, path, params ) elsif result.is_a? String result = TranslatedString.new(result, locale, path) return r18n.filter_list.process_string( :all, result, path, params ) else return result end end result end return if options[:no_write] define_method "#{name}=" do |*params| unlocalized = self.class.unlocalized_setters(name) r18n.locales.each do |locale| code = locale.code next unless unlocalized.key? code return send unlocalized[code], *params end end end
translation_types()
click to toggle source
`Hash` of translation method names to it type for filters.
# File lib/r18n-core/translated.rb, line 96 def translation_types @translation_types ||= {} end
translations(*names)
click to toggle source
Add several proxy methods. See `R18n::Translated` for description. It's more compact, that `translation`.
translations :title, :keywords, [:desciption, { type: 'markdown' }]
# File lib/r18n-core/translated.rb, line 104 def translations(*names) names.each { |name| translation(*name) } end
unlocalized_getters(method)
click to toggle source
Return `Hash` of locale code to getter method for proxy `method`. If you didn't set map in `translation` option `methods`, it will be detect automatically.
# File lib/r18n-core/translated.rb, line 178 def unlocalized_getters(method) matcher = Regexp.new("^#{Regexp.escape(method.to_s)}_(\\w+)$") unless @unlocalized_getters.key? method @unlocalized_getters[method] = {} unlocalized_methods.select { |i| i =~ matcher }.each do |i| @unlocalized_getters[method][i.to_s.match(matcher)[1]] = i.to_s end end @unlocalized_getters[method] end
unlocalized_methods()
click to toggle source
Return array of methods to find `unlocalized_getters` or `unlocalized_setters`.
# File lib/r18n-core/translated.rb, line 171 def unlocalized_methods instance_methods end
unlocalized_setters(method)
click to toggle source
Return `Hash` of locale code to setter method for proxy `method`. If you didn't set map in `translation` option `methods`, it will be detect automatically.
# File lib/r18n-core/translated.rb, line 192 def unlocalized_setters(method) matcher = Regexp.new("^#{Regexp.escape(method.to_s)}_(\\w+)=$") unless @unlocalized_setters.key? method @unlocalized_setters[method] = {} unlocalized_methods.select { |i| i =~ matcher }.each do |i| @unlocalized_setters[method][i.to_s.match(matcher)[1]] = i.to_s end end @unlocalized_setters[method] end