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



272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/zena/use/i18n.rb', line 272

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



287
288
289
290
291
292
293
294
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
# File 'lib/zena/use/i18n.rb', line 287

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])
    if base_path
      # Lazy dictionary used for literal resolution
      dict = TranslationDict.new(doc.id)

      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)
        out "<% #{dict_name} = load_dictionary(#{doc.id}) %>"
      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



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/zena/use/i18n.rb', line 366

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.



263
264
265
266
267
268
269
270
# File 'lib/zena/use/i18n.rb', line 263

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



343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/zena/use/i18n.rb', line 343

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



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 323

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,
      :pre_processor => Proc.new {|this, str| trans(str)}
    }
  else
    { :class  => String,
      :method => 'trans',
      :accept_nil => true,
      :pre_processor => Proc.new {|this, str| trans(str)}
    }
  end
end

#translate_list(str) ⇒ Object

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



358
359
360
361
362
363
364
# File 'lib/zena/use/i18n.rb', line 358

def translate_list(str)
  if trad = trans(str, false)
    trad.split(',').map(&:strip)
  else
    str.split(',').map(&:strip).map{|v| trans(v)}
  end
end