Class: Tr8n::Tokens::Data
- Inherits:
-
Object
- Object
- Tr8n::Tokens::Data
- Defined in:
- lib/tr8n/tokens/data.rb
Instance Attribute Summary collapse
-
#case_keys ⇒ Object
readonly
Returns the value of attribute case_keys.
-
#context_keys ⇒ Object
readonly
Returns the value of attribute context_keys.
-
#full_name ⇒ Object
readonly
Returns the value of attribute full_name.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#short_name ⇒ Object
readonly
Returns the value of attribute short_name.
Class Method Summary collapse
- .expression ⇒ Object
- .parse(label, opts = {}) ⇒ Object
-
.token_object(token_values, token_name) ⇒ Object
returns token object from tokens param.
Instance Method Summary collapse
-
#apply_case(key, value, object, language, options) ⇒ Object
chooses the appropriate case for the token value.
- #apply_language_cases(value, object, language, options) ⇒ Object
- #context_for_language(language, opts = {}) ⇒ Object
-
#initialize(label, token) ⇒ Data
constructor
A new instance of Data.
- #key ⇒ Object
- #language_cases_enabled? ⇒ Boolean
- #name(opts = {}) ⇒ Object
-
#name_for_case_keys(keys) ⇒ Object
used by the translator submit dialog.
- #parse_elements ⇒ Object
- #sanitize(value, object, language, options) ⇒ Object
- #sanitized_name ⇒ Object
- #substitute(label, context, language, options = {}) ⇒ Object
- #to_s ⇒ Object
-
#token_value(object, language, options = {}) ⇒ Object
evaluate all possible methods for the token value and return sanitized result.
-
#token_value_from_array(params, language, options) ⇒ Object
tr(“Hello user_list!”, “”, => [[user1, user2, user3], :name]}.
-
#token_value_from_param_array(array, language, options) ⇒ Object
gets the value based on various evaluation methods.
-
#token_value_from_param_hash(hash, language, options) ⇒ Object
examples:.
Constructor Details
#initialize(label, token) ⇒ Data
Returns a new instance of Data.
43 44 45 46 47 |
# File 'lib/tr8n/tokens/data.rb', line 43 def initialize(label, token) @label = label @full_name = token parse_elements end |
Instance Attribute Details
#case_keys ⇒ Object (readonly)
Returns the value of attribute case_keys.
29 30 31 |
# File 'lib/tr8n/tokens/data.rb', line 29 def case_keys @case_keys end |
#context_keys ⇒ Object (readonly)
Returns the value of attribute context_keys.
29 30 31 |
# File 'lib/tr8n/tokens/data.rb', line 29 def context_keys @context_keys end |
#full_name ⇒ Object (readonly)
Returns the value of attribute full_name.
29 30 31 |
# File 'lib/tr8n/tokens/data.rb', line 29 def full_name @full_name end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
29 30 31 |
# File 'lib/tr8n/tokens/data.rb', line 29 def label @label end |
#short_name ⇒ Object (readonly)
Returns the value of attribute short_name.
29 30 31 |
# File 'lib/tr8n/tokens/data.rb', line 29 def short_name @short_name end |
Class Method Details
.expression ⇒ Object
31 32 33 |
# File 'lib/tr8n/tokens/data.rb', line 31 def self.expression /(\{[^_:][\w]*(:[\w]+)*(::[\w]+)*\})/ end |
.parse(label, opts = {}) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/tr8n/tokens/data.rb', line 35 def self.parse(label, opts = {}) tokens = [] label.scan(expression).uniq.each do |token_array| tokens << self.new(label, token_array.first) end tokens end |
.token_object(token_values, token_name) ⇒ Object
returns token object from tokens param
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/tr8n/tokens/data.rb', line 96 def self.token_object(token_values, token_name) return nil if token_values.nil? token_object = Tr8n::Utils.hash_value(token_values, token_name) return token_object.first if token_object.is_a?(Array) if token_object.is_a?(Hash) object = Tr8n::Utils.hash_value(token_object, :object) return object if object end token_object end |
Instance Method Details
#apply_case(key, value, object, language, options) ⇒ Object
- chooses the appropriate case for the token value. case is identified with
-
examples:
tr(“Hello Tr8n::Tokens::Data.useruser::nom”, “”, :user => current_user) tr(“actor gave Tr8n::Tokens::Data.targettarget::dat a present”, “”, :actor => user1, :target => user2) tr(“This is Tr8n::Tokens::Data.useruser::pos toy”, “”, :user => current_user)
358 359 360 361 362 |
# File 'lib/tr8n/tokens/data.rb', line 358 def apply_case(key, value, object, language, ) lcase = language.case_by_keyword(key) return value unless lcase lcase.apply(value, object, ) end |
#apply_language_cases(value, object, language, options) ⇒ Object
364 365 366 367 368 369 370 |
# File 'lib/tr8n/tokens/data.rb', line 364 def apply_language_cases(value, object, language, ) case_keys.each do |key| value = apply_case(key, value, object, language, ) end value end |
#context_for_language(language, opts = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/tr8n/tokens/data.rb', line 76 def context_for_language(language, opts = {}) if context_keys.any? ctx = language.context_by_keyword(context_keys.first) else ctx = language.context_by_token_name(short_name) end unless opts[:silent] raise Tr8n::Exception.new("Unknown context for a token: #{full_name} in #{language.locale}") unless ctx end ctx end |
#key ⇒ Object
66 67 68 |
# File 'lib/tr8n/tokens/data.rb', line 66 def key short_name.to_sym end |
#language_cases_enabled? ⇒ Boolean
343 344 345 |
# File 'lib/tr8n/tokens/data.rb', line 343 def language_cases_enabled? Tr8n.session.application and Tr8n.session.application.feature_enabled?(:language_cases) end |
#name(opts = {}) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/tr8n/tokens/data.rb', line 58 def name(opts = {}) val = short_name val = "#{val}:#{context_keys.join(':')}" if opts[:context_keys] and context_keys.any? val = "#{val}::#{case_keys.join('::')}" if opts[:case_keys] and case_keys.any? val = "{#{val}}" if opts[:parens] val end |
#name_for_case_keys(keys) ⇒ Object
used by the translator submit dialog
71 72 73 74 |
# File 'lib/tr8n/tokens/data.rb', line 71 def name_for_case_keys(keys) keys = [keys] unless keys.is_a?(Array) "#{name}::#{keys.join('::')}" end |
#parse_elements ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/tr8n/tokens/data.rb', line 49 def parse_elements name_without_parens = @full_name[1..-2] name_without_case_keys = name_without_parens.split('::').first.strip @short_name = name_without_parens.split(':').first.strip @case_keys = name_without_parens.scan(/(::\w+)/).flatten.uniq.collect{|c| c.gsub('::', '')} @context_keys = name_without_case_keys.scan(/(:\w+)/).flatten.uniq.collect{|c| c.gsub(':', '')} end |
#sanitize(value, object, language, options) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/tr8n/tokens/data.rb', line 330 def sanitize(value, object, language, ) value = value.to_s unless Tr8n.session.[:skip_html_escaping] if [:safe] == false value = ERB::Util.html_escape(value) end end return value unless language_cases_enabled? apply_language_cases(value, object, language, ) end |
#sanitized_name ⇒ Object
395 396 397 |
# File 'lib/tr8n/tokens/data.rb', line 395 def sanitized_name name(:parens => true) end |
#substitute(label, context, language, options = {}) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/tr8n/tokens/data.rb', line 372 def substitute(label, context, language, = {}) # get the object from the values object = Tr8n::Utils.hash_value(context, key) # see if the token is a default html token object = Tr8n.config.default_token_value(key) if object.nil? if object.nil? and not context.key?(key) Tr8n.logger.error("Missing value for #{full_name} in #{label}") return label end if object.nil? and not Tr8n::Config.allow_nil_token_values? Tr8n.logger.error("Token value is nil for #{full_name} in #{label}") return label end return label.gsub(full_name, "") if object.nil? value = token_value(object, language, ) label.gsub(full_name, value) end |
#to_s ⇒ Object
399 400 401 |
# File 'lib/tr8n/tokens/data.rb', line 399 def to_s full_name end |
#token_value(object, language, options = {}) ⇒ Object
evaluate all possible methods for the token value and return sanitized result
324 325 326 327 328 |
# File 'lib/tr8n/tokens/data.rb', line 324 def token_value(object, language, = {}) return token_value_from_param_array(object, language, ) if object.is_a?(Array) return token_value_from_param_hash(object, language, ) if object.is_a?(Hash) sanitize(object, object, language, ) end |
#token_value_from_array(params, language, options) ⇒ Object
tr(“Hello user_list!”, “”, => [[user1, user2, user3], :name]}
first element is an array, the rest of the elements are similar to the regular tokens lambda, symbol, string, with parameters that follow
if you want to pass options, then make the second parameter an array as well
tr(“users joined the site”, => [[user1, user2, user3], :name])
tr(“users joined the site”, => [[user1, user2, user3], lambda{|user| user.name]})
tr(“users joined the site”, => [[user1, user2, user3], {:attribute => :name)
tr(“users joined the site”, => [[user1, user2, user3], {:attribute => :name, :value => “<strong>{$0</strong>”})
tr(“users joined the site”, => [[user1, user2, user3], “<strong>{$0</strong>”)
tr(“users joined the site”, => [[user1, user2, user3], :name, {
:limit => 4,
:separator => ', ',
:joiner => 'and',
:remainder => lambda{|elements| tr("#{count||other", :count => elements.size)},
:expandable => true,
:collapsable => true
})
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/tr8n/tokens/data.rb', line 137 def token_value_from_array(params, language, ) = { :description => "List joiner", :limit => 4, :separator => ", ", :joiner => 'and', :less => '{laquo} less', :expandable => true, :collapsable => true } objects = params[0] method = params[1] .merge!(params[2]) if params.size > 2 [:expandable] = false if [:skip_decorations] values = objects.collect do |obj| if method.is_a?(String) method.gsub("{$0}", sanitize(obj.to_s, obj, language, .merge(:safe => false))) elsif method.is_a?(Symbol) if obj.is_a?(Hash) value = Tr8n::Utils.hash_value(obj, method) else value = obj.send(method) end sanitize(value, obj, language, .merge(:safe => false)) elsif method.is_a?(Hash) attr = Tr8n::Utils.hash_value(method, :attribute) || Tr8n::Utils.hash_value(method, :property) if obj.is_a?(Hash) value = Tr8n::Utils.hash_value(obj, attr) else value = obj.send(method) end hash_value = Tr8n::Utils.hash_value(method, :value) if hash_value hash_value.gsub("{$0}", sanitize(value, obj, language, .merge(:safe => false))) else sanitize(value, obj, language, .merge(:safe => false)) end elsif method.is_a?(Proc) sanitize(method.call(obj), obj, language, .merge(:safe => true)) end end return values.first if objects.size == 1 return values.join([:separator]) unless [:joiner] if values.size <= [:limit] return "#{values[0..-2].join(list_options[:separator])} #{language.translate(list_options[:joiner], list_options[:description], {}, options)} #{values.last}" end display_ary = values[0..([:limit]-1)] remaining_ary = values[[:limit]..-1] result = "#{display_ary.join(list_options[:separator])}" unless [:expandable] result << " " << language.translate([:joiner], [:description], {}, ) << " " if [:remainder] and [:remainder].is_a?(Proc) result << [:remainder].call(remaining_ary) else result << language.translate("{count||other}", [:description], {:count => remaining_ary.size}, ) end return result end joiner = language.translate([:joiner], [:description], {}, ) uniq_id = Tr8n::TranslationKey.generate_key(label, values.join(",")) result << "<span id=\"tr8n_other_link_#{uniq_id}\">" result << " #{joiner} " result << "<a href='#' onClick=\"Tr8n.Utils.Effects.hide('tr8n_other_link_#{uniq_id}'); Tr8n.Utils.Effects.show('tr8n_other_elements_#{uniq_id}'); return false;\">" if [:remainder] and [:remainder].is_a?(Proc) result << [:remainder].call(remaining_ary) else result << language.translate("{count||other}", [:description], {:count => remaining_ary.size}, ) end result << "</a></span>" result << "<span id=\"tr8n_other_elements_#{uniq_id}\" style='display:none'>" result << [:separator] << " " result << remaining_ary[0..-2].join([:separator]) result << " #{joiner} " result << remaining_ary.last if [:collapsable] result << "<a href='#' style='font-size:smaller;white-space:nowrap' onClick=\"Tr8n.Utils.Effects.show('tr8n_other_link_#{uniq_id}'); Tr8n.Utils.Effects.hide('tr8n_other_elements_#{uniq_id}'); return false;\"> " result << language.translate([:less], [:description], {}, ) result << "</a>" end result << "</span>" end |
#token_value_from_param_array(array, language, options) ⇒ Object
gets the value based on various evaluation methods
examples:
tr(“Hello user”, => [current_user, current_user.name]} tr(“Hello user”, => [current_user, :name]}
tr(“Hello user”, => [{:name => “Michael”, :gender => :male, current_user.name]}} tr(“Hello user”, => [{:name => “Michael”, :gender => :male, :name]}}
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/tr8n/tokens/data.rb', line 246 def token_value_from_param_array(array, language, ) # if you provided an array, it better have some values if array.size < 2 Tr8n.logger.error("Invalid value for array token #{full_name} in #{label}") return full_name end # if the first value of an array is an array handle it here if array[0].is_a?(Array) return token_value_from_array(array, language, ) end if array[1].is_a?(String) return sanitize(array[1], array[0], language, .merge(:safe => true)) end if array[1].is_a?(Hash) if array[1].is_a?(Symbol) return sanitize(Tr8n::Utils.hash_value(array[0], array[1]), array[0], language, .merge(:safe => false)) end Tr8n.logger.error("Invalid value for array token #{full_name} in #{label}") return full_name end # if second param is symbol, invoke the method on the object with the remaining values if array[1].is_a?(Symbol) return sanitize(array[0].send(array[1]), array[0], language, .merge(:safe => false)) end Tr8n.logger.error("Invalid value for array token #{full_name} in #{label}") full_name end |
#token_value_from_param_hash(hash, language, options) ⇒ Object
examples:
tr(“Hello user”, => {:value => “Michael”, :gender => :male}}
tr(“Hello user”, => {:object => {:gender => :male, :value => “Michael”}}} tr(“Hello user”, => {:object => {:name => “Michael”, :gender => :male, :property => :name}}} tr(“Hello user”, => {:object => {:name => “Michael”, :gender => :male, :attribute => :name}}}
tr(“Hello user”, => {:object => user, :value => “Michael”}} tr(“Hello user”, => {:object => user, :property => :name}} tr(“Hello user”, => {:object => user, :attribute => :name}}
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/tr8n/tokens/data.rb', line 296 def token_value_from_param_hash(hash, language, ) value = Tr8n::Utils.hash_value(hash, :value) object = Tr8n::Utils.hash_value(hash, :object) unless value.nil? return sanitize(value, object || hash, language, .merge(:safe => true)) end if object.nil? Tr8n.logger.error("Missing value for hash token #{full_name} in #{label}") return full_name end attr = Tr8n::Utils.hash_value(hash, :attribute) || Tr8n::Utils.hash_value(hash, :property) if object.is_a?(Hash) unless attr.nil? return sanitize(Tr8n::Utils.hash_value(object, attr), object, language, .merge(:safe => false)) end Tr8n.logger.error("Missing value for hash token #{full_name} in #{label}") return full_name end sanitize(object.send(attr), object, language, .merge(:safe => false)) end |