module I18n::Backend::Tml::Implementation

Public Instance Methods

application() click to toggle source

Returns current application

# File lib/i18n/backend/tml.rb, line 44
def application
  ::Tml.session.application
end
available_locales() click to toggle source

List of all application available locales

# File lib/i18n/backend/tml.rb, line 49
def available_locales
  application.locales
end
convert_to_tml(str) click to toggle source

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

# File lib/i18n/backend/tml.rb, line 59
def convert_to_tml(str)
  str.gsub('%{', '{')
end
interpolate(locale, string, values = {}) click to toggle source

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

# File lib/i18n/backend/tml.rb, line 54
def interpolate(locale, string, values = {})
  string
end
translate(locale, key, options = {}) click to toggle source

Translates a string

Calls superclass method
# 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) click to toggle source

Translates a hash of values

# 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