Module: I18n::Backend::Tml::Implementation

Includes:
Base, Flatten
Included in:
I18n::Backend::Tml
Defined in:
lib/i18n/backend/tml.rb

Instance Method Summary collapse

Instance Method Details

#applicationObject

Returns current application



44
45
46
# File 'lib/i18n/backend/tml.rb', line 44

def application
  ::Tml.session.application
end

#available_localesObject

List of all application available locales



49
50
51
# File 'lib/i18n/backend/tml.rb', line 49

def available_locales
  application.locales
end

#convert_to_tml(str) ⇒ Object

TODO: this should be configurable. Our SDK supports both notations.



59
60
61
# File 'lib/i18n/backend/tml.rb', line 59

def convert_to_tml(str)
  str.gsub('%{', '{')
end

#interpolate(locale, string, values = {}) ⇒ Object

we will capture interpolation here - so we can process it ourselves using TML



54
55
56
# File 'lib/i18n/backend/tml.rb', line 54

def interpolate(locale, string, values = {})
  string
end

#translate(locale, key, options = {}) ⇒ Object

Translates a string



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/i18n/backend/tml.rb', line 77

def translate(locale, key, options = {})
  # TODO: we don't support this yet - but we should
  return super if I18n.locale != locale

  # look up the translation in default locale
  translation = super(application.default_locale, key, options)

  # pp [locale, key, options, translation]

  # if no translation is available, ignore it
  return translation if translation.nil? or translation == ''

  # if language is not available, return default value
  target_language = application.language(locale.to_s)

  if translation.is_a?(String)
    translation = target_language.translate(convert_to_tml(translation), options, options)
  elsif translation.is_a?(Hash)
    translation = translate_hash(target_language, translation, options)
  end

  translation
end

#translate_hash(target_language, hash, options) ⇒ Object

Translates a hash of values



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/i18n/backend/tml.rb', line 64

def translate_hash(target_language, hash, options)
  hash.each do |key, value|
    if value.is_a?(String)
      hash[key] = target_language.translate(convert_to_tml(value), options, options)
    elsif value.is_a?(Hash)
      translate_hash(target_language, value, options)
    end
  end

  hash
end