Class: Tr8n::TranslationKey

Inherits:
Base
  • Object
show all
Defined in:
lib/tr8n/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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tr8n/translation_key.rb', line 32

def initialize(attrs = {})
  super

  self.attributes[:key] ||= self.class.generate_key(label, description)
  self.attributes[:locale] ||= Tr8n.session.block_options[:locale] || (application ? application.default_locale : Tr8n.config.default_locale)
  self.attributes[:language] ||= application ? application.language(locale) : Tr8n.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 = Tr8n::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 Tr8n::Base

Class Method Details

.cache_key(label, description, locale) ⇒ Object



192
193
194
# File 'lib/tr8n/translation_key.rb', line 192

def self.cache_key(label, description, locale)
  "#{cache_prefix}_[#{locale}]_[#{generate_key(label, description)}]";
end

.cache_prefixObject

Cache Methods



188
189
190
# File 'lib/tr8n/translation_key.rb', line 188

def self.cache_prefix
  't@'
end

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



54
55
56
# File 'lib/tr8n/translation_key.rb', line 54

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



167
168
169
170
# File 'lib/tr8n/translation_key.rb', line 167

def self.substitute_tokens(label, token_values, language, options = {})
  return label.to_s if options[:skip_substitution] 
  Tr8n::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



149
150
151
152
153
154
# File 'lib/tr8n/translation_key.rb', line 149

def data_tokens
  @data_tokens ||= begin
    dt = Tr8n::Tokens::DataTokenizer.new(label)
    dt.tokens
  end
end

#data_tokens_names_mapObject



156
157
158
159
160
161
162
163
164
# File 'lib/tr8n/translation_key.rb', line 156

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



140
141
142
143
144
145
146
# File 'lib/tr8n/translation_key.rb', line 140

def decoration_tokens
  @decoration_tokens ||= begin
    dt = Tr8n::Tokens::DecorationTokenizer.new(label)
    dt.parse
    dt.tokens
  end
end

#fetch_translations(language, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tr8n/translation_key.rb', line 62

def fetch_translations(language, options = {})
  return self if self.id and has_translations_for_language?(language)

  if options[:dry] or Tr8n.session.block_options[:dry]
    return application.cache_translation_key(self)
  end

  tkey = application.post("translation_key/translations",
                          {:key => key, :label => label, :description => description, :locale => language.locale},
                          {:class => Tr8n::TranslationKey, :attributes => {:application => application}})

  application.cache_translation_key(tkey)
end

#find_first_valid_translation(language, token_values) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tr8n/translation_key.rb', line 104

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



58
59
60
# File 'lib/tr8n/translation_key.rb', line 58

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



77
78
79
80
81
82
83
84
85
# File 'lib/tr8n/translation_key.rb', line 77

def set_application(app)
  self.application = app
  translations.values.each do |locale_translations|
    locale_translations.each do |t|
      t.set_translation_key(self)
    end
  end
  self
end

#set_language_translations(language, translations) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/tr8n/translation_key.rb', line 87

def set_language_translations(language, translations)
  translations.each do |translation|
    translation.locale = language.locale
    translation.set_translation_key(self)
  end
  self.translations[language.locale] = translations
end

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



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/tr8n/translation_key.rb', line 172

def substitute_tokens(translated_label, token_values, language, options = {})
  if Tr8n::Tokens::DataTokenizer.required?(translated_label)
    translated_label = Tr8n::Tokens::DataTokenizer.new(translated_label, token_values, :allowed_tokens => data_tokens_names_map).substitute(language, options)
  end

  if Tr8n::Tokens::DecorationTokenizer.required?(translated_label)
    translated_label = Tr8n::Tokens::DecorationTokenizer.new(translated_label, token_values, :allowed_tokens => decoration_tokens).substitute
  end

  translated_label
end

#to_cache_hashObject



196
197
198
199
200
201
202
203
204
205
# File 'lib/tr8n/translation_key.rb', line 196

def to_cache_hash
  hash = to_hash(:id, :key, :label, :description, :locale, :level, :locked)
  if translations and translations.any?
    hash["translations"] = {}
    translations.each do |locale, locale_translations|
      hash["translations"][locale] = locale_translations.collect{|t| t.to_cache_hash}
    end
  end
  hash
end

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/tr8n/translation_key.rb', line 116

def translate(language, token_values = {}, options = {})
  if Tr8n.config.disabled? or language.locale == self.attributes[:locale]
    return substitute_tokens(label, token_values, language, options.merge(:fallback => false))
  end

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

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

  #Tr8n.logger.info("Translating: #{label}")

  translated_label = substitute_tokens(label, token_values, self.attributes[:language], options)
  decorator.decorate(translated_label, self.attributes[:language], language, self, options)
end

#translations_for_language(language) ⇒ Object

Translation Methods



99
100
101
102
# File 'lib/tr8n/translation_key.rb', line 99

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