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



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

def application
  ::Tml.session.application
end

#available_localesObject



47
48
49
# File 'lib/i18n/backend/tml.rb', line 47

def available_locales
  application.locales
end

#convert_to_tml(str) ⇒ Object



56
57
58
# File 'lib/i18n/backend/tml.rb', line 56

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

#lookup(locale, key, scope = [], options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/i18n/backend/tml.rb', line 60

def lookup(locale, key, scope = [], options = {})
  # pp [locale, key, scope, options]

  if key.to_s.match(/^(support|i18n|number|human|distance)/)
    return super(locale, key, scope, options)
  end

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

  unless target_language
    return super(locale, key, scope, options)
  end

  default_key = super(application.default_locale, key, scope, options)
  default_key ||= key.to_s.split('.').last.gsub('_', ' ').capitalize

  if default_key.is_a?(String)
    translated_key = target_language.translate(convert_to_tml(default_key), options, options)
  elsif default_key.is_a?(Hash)
    translated_key = {}

    default_key.each do |key, value|
      if value.is_a?(String)
        value = target_language.translate(convert_to_tml(value), options, options)
      end
      translated_key[key] = value
    end
  end

  translated_key
end

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



51
52
53
54
# File 'lib/i18n/backend/tml.rb', line 51

def translate(locale, key, options = {})
  translation = super(locale, key, options)
  translation.is_a?(String) ? translation.html_safe : translation
end