Module: Zena::Use::I18n::ZafuMethods

Includes:
RubyLess
Defined in:
lib/zena/use/i18n.rb

Overview

ViewMethods

Instance Method Summary collapse

Instance Method Details

#r_iconvObject



280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/zena/use/i18n.rb', line 280

def r_iconv
  return parser_error("missing 'to' parameter") unless to = @params[:to]
  begin
    Iconv.iconv(to, 'utf8', 'éaïl')
  rescue
    return parser_error("invalid encoding #{to.inspect}")
  end

  data_name = get_var_name('iconv', 'data')
  out "<% #{data_name} = capture do %>"
  out expand_with
  out "<% end %>"
  out "<%= Iconv.iconv(#{to.inspect}, 'utf8', #{data_name}) %>"
end

#r_loadObject



295
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/zena/use/i18n.rb', line 295

def r_load
  if dict = @params[:dictionary]
    # FIXME: replace @options[:base_path] by @options[:skin_id]
    dict_content, absolute_url, base_path, doc = @options[:helper].send(:get_template_text, dict, @options[:base_path], :ext => 'yml')
    if dict_content
      # Lazy dictionary used for literal resolution
      if doc
        dict = TranslationDict.new(doc.id)
      else
        if absolute_url =~ /^\$([a-zA-Z_]+)-([a-zA-Z_]+)\/(.+)$/
          static = {:brick_name => $1, :skin_name => $2, :path => $3}
        else
          return parser_error("Invalid absolute_url for static asset ('#{absolute_url}')")
        end
        dict = TranslationDict.new(nil, static)
      end

      if dict.load!(dict_content)
        # Save dictionary in template for dynamic uses
        dict_name = get_var_name('dictionary', 'dict')

        # This is to use in RubyLess translations and static translations in Zafu
        set_context_var('set_var', 'dictionary', RubyLess::TypedString.new(dict_name, :class => TranslationDict, :literal => dict))

        # Lazy loading (loads file on first request)
        if doc
          out "<% #{dict_name} = load_dictionary(#{doc.id}) %>"
        else
          out "<% #{dict_name} = load_dictionary(nil, #{static.inspect}) %>"
        end
      else
        return parser_error(dict.last_error)
      end
    else
      # not found
      if @params[:missing] == 'ignore'
        # ignore
      else
        return parser_error("dictionary #{dict.inspect} not found") unless base_path
      end
    end
  else
    return parser_error("missing 'dictionary'")
  end

  expand_with
end

#r_transObject Also known as: r_t



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/zena/use/i18n.rb', line 401

def r_trans
  # _1 ==> insert this param ==> trans(@params[:text])
  return nil unless method = get_attribute_or_eval
  klass = method.klass
  return parser_error("Cannot translate a '#{klass}'.") unless klass <= String

  dict = get_context_var('set_var', 'dictionary')

  if method.literal
    erb_escape trans(method.literal)
  elsif dict && dict.klass <= TranslationDict
    "<%= #{dict}.get(#{method}) %>"
  else
    "<%= trans(#{method}) %>"
  end
end

#r_wrong_lang(params = @params) ⇒ Object

Show a little [xx] next to the title if the desired language could not be found. You can use a :text => ‘(lang)’ option. The word ‘lang’ will be replaced by the real value.



271
272
273
274
275
276
277
278
# File 'lib/zena/use/i18n.rb', line 271

def r_wrong_lang(params = @params)
  if @blocks.empty? || @method != 'wrong_lang'
    text = params[:text] || %q{<span class='wrong_lang'>[#{v.lang}]</span> }
    "<%=  #{node}.version.lang == lang ? '' : #{::RubyLess.translate_string(self, text)} %>"
  else
    expand_if("#{node}.version.lang != lang")
  end
end

#trans(text, use_global = true) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/zena/use/i18n.rb', line 372

def trans(text, use_global = true)
  dictionary = get_context_var('set_var', 'dictionary')

  if dictionary && dictionary.klass <= TranslationDict && dict = dictionary.literal
    # will call ApplicationController(:_) if key is not found
    dict.get(text, use_global)
  elsif use_global
    helper.send(:_, text)
  else
    nil
  end
end

#translate(signature, receiver = nil) ⇒ Object

Resolve RubyLess ‘t’ and ‘trans’ methods



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/zena/use/i18n.rb', line 344

def translate(signature, receiver = nil)
  return nil unless signature.size == 2 && signature[1] <= String

  dict = get_context_var('set_var', 'dictionary')

  if dict && dict.klass <= TranslationDict
    { :class  => String,
      :method => "#{dict}.get",
      :accept_nil => true,
      :html_safe  => true,
      :pre_processor => Proc.new {|this, str| 
        t = erb_escape(trans(str))
        RubyLess::TypedString.new(t.inspect, :html_safe => true, :literal => t, :class => String)
      }
    }
  else
    { :class  => String,
      :method => 'trans',
      :accept_nil => true,
      :html_safe  => true,
      :pre_processor => Proc.new {|this, str| 
        t = erb_escape(trans(str))
        RubyLess::TypedString.new(t.inspect, :html_safe => true, :literal => t, :class => String)
      }
    }
  end
end

#translate_list(str, prefix = nil) ⇒ Object

Translate a string representing a list of values separated with a comma (‘dog,cat,house’) to a list of strings.



387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/zena/use/i18n.rb', line 387

def translate_list(str, prefix = nil)
  if trad = trans(str, false)
    trad.split(',').map(&:strip)
  elsif prefix
    str.split(',').map(&:strip).map do |v|
      k = "#{prefix}_#{v}"
      d = trans(k)
      d == k ? v : d
    end
  else
    str.split(',').map(&:strip).map{|v| trans(v)}
  end
end