Class: Tml::TranslationKey

Inherits:
Base
  • Object
show all
Defined in:
lib/tml/translation_key.rb

Instance Attribute Summary

Attributes inherited from Base

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attributes, belongs_to, has_many, hash_value, #hash_value, #method_missing, #to_hash, #update_attributes

Constructor Details

#initialize(attrs = {}) ⇒ TranslationKey

Returns a new instance of TranslationKey.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tml/translation_key.rb', line 40

def initialize(attrs = {})
  super

  self.attributes[:key] ||= self.class.generate_key(label, description)
  self.attributes[:locale] ||= Tml.session.block_option(:locale) || (application ? application.default_locale : Tml.config.default_locale)
  self.attributes[:language] ||= application ? application.language(locale) : Tml.config.default_language
  self.attributes[:translations] = {}

  if hash_value(attrs, :translations)
    hash_value(attrs, :translations).each do |locale, translations|
      language = application.language(locale)

      self.attributes[:translations][locale] ||= []

      translations.each do |translation_hash|
        translation = Tml::Translation.new(translation_hash.merge(:translation_key => self, :locale => language.locale))
        self.attributes[:translations][locale] << translation
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Tml::Base

Class Method Details

.cache_key(locale, key) ⇒ Object



92
93
94
# File 'lib/tml/translation_key.rb', line 92

def self.cache_key(locale, key)
  File.join(locale, 'keys', key)
end

.generate_key(label, desc = '') ⇒ Object



62
63
64
# File 'lib/tml/translation_key.rb', line 62

def self.generate_key(label, desc = '')
  "#{Digest::MD5.hexdigest("#{label};;;#{desc}")}~"[0..-2].to_s
end

.substitute_tokens(label, token_values, language, options = {}) ⇒ Object

if the translations engine is disabled



197
198
199
200
# File 'lib/tml/translation_key.rb', line 197

def self.substitute_tokens(label, token_values, language, options = {})
  return label.to_s if options[:skip_substitution]
  Tml::TranslationKey.new(:label => label.to_s).substitute_tokens(label.to_s, token_values, language, options)
end

Instance Method Details

#data_tokensObject

Returns an array of data tokens from the translation key



179
180
181
182
183
184
# File 'lib/tml/translation_key.rb', line 179

def data_tokens
  @data_tokens ||= begin
    dt = Tml::Tokenizers::Data.new(label)
    dt.tokens
  end
end

#data_tokens_names_mapObject



186
187
188
189
190
191
192
193
194
# File 'lib/tml/translation_key.rb', line 186

def data_tokens_names_map
  @data_tokens_names_map ||= begin
    map = {}
    data_tokens.each do |token|
      map[token.name] = token
    end
    map
  end
end

#decoration_tokensObject

Returns an array of decoration tokens from the translation key



170
171
172
173
174
175
176
# File 'lib/tml/translation_key.rb', line 170

def decoration_tokens
  @decoration_tokens ||= begin
    dt = Tml::Tokenizers::Decoration.new(label)
    dt.parse
    dt.tokens
  end
end

#fetch_translations(locale) ⇒ Object

fetch translations for a specific translation key



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tml/translation_key.rb', line 106

def fetch_translations(locale)
  self.translations ||= {}
  return if self.translations[locale]

  # Tml.logger.debug("Fetching translations for #{label}")

  results = self.application.api_client.get(
      "translation_keys/#{self.key}/translations",
      {:locale => locale, :per_page => 10000},
      {:cache_key => Tml::TranslationKey.cache_key(locale, self.key)}
  ) || []

  update_translations(locale, results)

  self
rescue Tml::Exception => ex
  self.translations = {}
  self
end

#find_first_valid_translation(language, token_values) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/tml/translation_key.rb', line 135

def find_first_valid_translation(language, token_values)
  translations = translations_for_language(language)

  translations.sort! { |x, y| x.precedence <=> y.precedence }

  translations.each do |translation|
    return translation if translation.matches_rules?(token_values)
  end

  nil
end

#has_translations_for_language?(language) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/tml/translation_key.rb', line 66

def has_translations_for_language?(language)
  translations and translations[language.locale] and translations[language.locale].any?
end

#set_application(app) ⇒ Object

switches to a new application



81
82
83
84
85
86
87
88
89
90
# File 'lib/tml/translation_key.rb', line 81

def set_application(app)
  self.application = app
  translations.values.each do |locale_translations|
    locale_translations.each do |translation|
      translation.translation_key = self
      translation.language = self.application.language(translation.locale)
    end
  end
  self
end

#set_translations(locale, translations) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/tml/translation_key.rb', line 70

def set_translations(locale, translations)
  return unless translations
  translations.each do |translation|
    translation.locale ||= locale
    translation.translation_key = self
    translation.language = self.application.language(translation.locale)
  end
  self.translations[locale] = translations
end

#substitute_tokens(translated_label, token_values, language, options = {}) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/tml/translation_key.rb', line 202

def substitute_tokens(translated_label, token_values, language, options = {})
  if options[:syntax] == 'xmessage'
    # pp "Translating #{translated_label} using xmessage syntax"
    tokenizer = Tml::Tokenizers::XMessage.new(translated_label)
    return tokenizer.substitute(language, token_values, options)
  end

  if Tml::Tokenizers::Decoration.required?(translated_label)
    translated_label = Tml::Tokenizers::Decoration.new(translated_label, token_values, :allowed_tokens => decoration_tokens).substitute
  end

  if Tml::Tokenizers::Data.required?(translated_label)
    translated_label = Tml::Tokenizers::Data.new(translated_label, token_values, :allowed_tokens => data_tokens_names_map).substitute(language, options)
  end

  translated_label
end

#translate(language, token_values = {}, options = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/tml/translation_key.rb', line 147

def translate(language, token_values = {}, options = {})
  if Tml.config.disabled?
    return substitute_tokens(label, token_values, language, options.merge(:fallback => false))
  end

  translation = find_first_valid_translation(language, token_values)
  decorator = Tml::Decorators::Base.decorator(options)

  if translation
    options[:locked] = translation.locked
    translated_label = substitute_tokens(translation.label, token_values, translation.language, options)
    return decorator.decorate(translated_label, translation.language, language, self, options)
  end

  translated_label = substitute_tokens(label, token_values, self.language, options)
  decorator.decorate(translated_label, self.language, language, self, options)
end

#translations_for_language(language) ⇒ Object

Translation Methods



130
131
132
133
# File 'lib/tml/translation_key.rb', line 130

def translations_for_language(language)
  return [] unless self.translations
  self.translations[language.locale] || []
end

#update_translations(locale, data) ⇒ Object

update translations in the key



97
98
99
100
101
102
103
# File 'lib/tml/translation_key.rb', line 97

def update_translations(locale, data)
  set_translations(locale, application.cache_translations(
                             locale,
                             key,
                             data.is_a?(Hash) ? data['translations'] : data
                         ))
end