Module with class methods, which be added after R18n::Translated include.
Add proxy-method name. See R18n::Translated for description. It’s more useful to set options.
translation :desciption, :type => 'markdown'
# File lib/r18n-core/translated.rb, line 113 def translation(name, options = {}) if options[:methods] @unlocalized_getters[name] = R18n::Utils. hash_map(options[:methods]) { |l, i| [ l.to_s, i.to_s ] } unless options[:no_write] @unlocalized_setters[name] = R18n::Utils. hash_map(options[:methods]) { |l, i| [ l.to_s, i.to_s + '=' ] } end end @translation_types[name] = options[:type] params = options[:no_params] ? '' : ', *params' class_eval def #{name}(*params) unlocalized = self.class.unlocalized_getters(#{name.inspect}) r18n.locales.each do |locale| code = locale.code next unless unlocalized.has_key? code result = send unlocalized[code]#{params} next unless result path = "\#{self.class.name}##{name}" type = self.class.translation_types[#{name.inspect}] 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 R18n::Untranslated.new("\#{self.class.name}\#", '#{name}', r18n.locale, r18n.filter_list) end, __FILE__, __LINE__ unless options[:no_write] class_eval def #{name}=(*params) unlocalized = self.class.unlocalized_setters(#{name.inspect}) r18n.locales.each do |locale| code = locale.code next unless unlocalized.has_key? code return send unlocalized[code], *params end end, __FILE__, __LINE__ end end
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 103 def translations(*methods) methods.each do |method| translation(*method) end end
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 177 def unlocalized_getters(method) matcher = Regexp.new('^' + Regexp.escape(method.to_s) + '_(\w+)$') unless @unlocalized_getters.has_key? method @unlocalized_getters[method] = {} self.unlocalized_methods.reject { |i| not i =~ matcher }.each do |i| @unlocalized_getters[method][i.to_s.match(matcher)[1]] = i.to_s end end @unlocalized_getters[method] end
Return array of methods to find unlocalized_getters or unlocalized_setters.
# File lib/r18n-core/translated.rb, line 170 def unlocalized_methods self.instance_methods end
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 191 def unlocalized_setters(method) matcher = Regexp.new('^' + Regexp.escape(method.to_s) + '_(\w+)=$') unless @unlocalized_setters.has_key? method @unlocalized_setters[method] = {} self.unlocalized_methods.reject { |i| not i =~ matcher }.each do |i| @unlocalized_setters[method][i.to_s.match(matcher)[1]] = i.to_s end end @unlocalized_setters[method] end
Generated with the Darkfish Rdoc Generator 2.