Module: R18n::Translated::Base

Defined in:
lib/r18n-core/translated.rb

Overview

Module with class methods, which be added after R18n::Translated include.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#translation_typesObject (readonly)

Hash of translation method names to it type for filters.



95
96
97
# File 'lib/r18n-core/translated.rb', line 95

def translation_types
  @translation_types
end

Instance Method Details

#translation(name, options = {}) ⇒ Object

Add proxy-method name. See R18n::Translated for description. It’s more useful to set options.

translation :desciption, :type => 'markdown'


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/r18n-core/translated.rb', line 111

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]
  call = options[:no_params] ? 'call' : 'call(*params)'
  
  class_eval <<-EOS, __FILE__, __LINE__
    def #{name}(*params)
      unlocalized = self.class.unlocalized_getters(#{name.inspect})
      R18n.get.locales.each do |locale|
        code = locale.code
        next unless unlocalized.has_key? code
        result = method(unlocalized[code]).#{call}
        next unless result
        
        path = "\#{self.class.name}##{name}"
        type = self.class.translation_types[#{name.inspect}]
        if type
          return R18n::Filters.process(R18n::Filters.enabled,
                   type, result, locale, path, params)
        else
          result = TranslatedString.new(result, locale, path)
          return R18n::Filters.process_string(R18n::Filters.enabled,
                   result, path, params)
        end
      end
      
      R18n::Untranslated.new("\#{self.class.name}\#", '#{name}',
                             R18n.get.locale)
    end
  EOS
  
  unless options[:no_write]
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(*params)
        unlocalized = self.class.unlocalized_setters(#{name.inspect})
        R18n.get.locales.each do |locale|
          code = locale.code
          next unless unlocalized.has_key? code
          return method(unlocalized[code]).call(*params)
        end
      end
    EOS
  end
end

#translations(*methods) ⇒ Object

Add several proxy methods. See R18n::Translated for description. It’s more compact, that translation.

translations :title, :keywords, [:desciption, {:type => 'markdown'}]


101
102
103
104
105
# File 'lib/r18n-core/translated.rb', line 101

def translations(*methods)
  methods.each do |method|
    translation(*method)
  end
end

#unlocalized_getters(method) ⇒ Object

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.



165
166
167
168
169
170
171
172
173
174
# File 'lib/r18n-core/translated.rb', line 165

def unlocalized_getters(method)
  matcher = Regexp.new('^' + Regexp.escape(method.to_s) + '_(.*[^=])$')
  unless @unlocalized_getters.has_key? method
    @unlocalized_getters[method] = {}
    self.instance_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

#unlocalized_setters(method) ⇒ Object

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.



179
180
181
182
183
184
185
186
187
188
# File 'lib/r18n-core/translated.rb', line 179

def unlocalized_setters(method)
  matcher = Regexp.new('^' + Regexp.escape(method.to_s) + '_(.*)=$')
  unless @unlocalized_setters.has_key? method
    @unlocalized_setters[method] = {}
    self.instance_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