Module: I18nTemplate::Translation

Defined in:
lib/i18n_template/translation.rb

Constant Summary collapse

CLOSE_TAG_BEGIN_PATTERN =
/
  (?=<\/)(.*?)$     # match any string that begins with <\/ characters as 1 
/x.freeze
WRAPPER_OR_VARIABLE_PATTERN =
/(
  \{\}              # empty braces
  |                 # or
    \{([^\}]+)\}    # match any data except close brace in braces as 2
  |                 # or
    \[(\/?)(\d+)\]  # optional match back-slash character as 3 and digits as 4 in brackets
)/x.freeze
T9N_CLEANUP_PATTERN =
/\s+(i18n_wrapper="\d+"|i18n="\w")/x.freeze

Class Method Summary collapse

Class Method Details

.translate(key, wrappers, vars) ⇒ Object

translate phrase and replace placeholders and variables



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/i18n_template/translation.rb', line 19

def translate(key, wrappers, vars)
  # map each wrapper to open and close tag
  # e.g '<a><b><c>bla</d></e>' -> [ '<a><b><c>bla', '</d></e>' ]
  wrappers = wrappers.collect do |w|
    w =~ CLOSE_TAG_BEGIN_PATTERN ? [w[0, w.size-$1.size], $1] : ['', '']
  end

  phrase = (I18nTemplate.translator.call(key) || key).dup

  # replaces {variable name} or [digits] or [\digits]
  # with wrappers and variables
  phrase.gsub!(WRAPPER_OR_VARIABLE_PATTERN) do |s|
    case s[0, 1]
    when '{' then s == '{}' ? vars[''] : vars[$2]
    when '[' then (wrappers[$4.to_i] || [])['/' == $3 ? 1 : 0]
    end
  end

  # replace nl with break line
  phrase.gsub!("[nl]", "<br />")

  # replace unescaped characters
  phrase.gsub!("[lsb]", "[")
  phrase.gsub!("[rsb]", "]")
  phrase.gsub!("[lcb]", "{")
  phrase.gsub!("[rcb]", "}")
  phrase.gsub!("[ns]",  "#")

  # remove i18n attributes. E.g:
  # i18n_wrapper='100'
  # i18n="i"
  phrase.gsub!(T9N_CLEANUP_PATTERN, '')

  if phrase.respond_to?(:html_safe)
    # return html_safe phrase
    phrase.html_safe
  else
    # return pure string
    phrase
  end
end