Class: Tr8n::Language

Inherits:
Base
  • Object
show all
Defined in:
lib/tr8n/language.rb

Overview

– Copyright © 2014 Michael Berkovich, TranslationExchange.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

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, #initialize, #method_missing, #to_hash

Constructor Details

This class inherits a constructor from Tr8n::Base

Dynamic Method Handling

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

Class Method Details

.cache_key(locale) ⇒ Object



229
230
231
# File 'lib/tr8n/language.rb', line 229

def self.cache_key(locale)
  "#{cache_prefix}_[#{locale}]"
end

.cache_prefixObject

Cache Methods



225
226
227
# File 'lib/tr8n/language.rb', line 225

def self.cache_prefix
  'l@'
end

Instance Method Details

#align(dest) ⇒ Object



78
79
80
81
# File 'lib/tr8n/language.rb', line 78

def align(dest)
  return dest unless right_to_left?
  dest.to_s == 'left' ? 'right' : 'left'
end

#case_by_keyword(keyword) ⇒ Object



61
62
63
# File 'lib/tr8n/language.rb', line 61

def case_by_keyword(keyword)
  cases[keyword]
end

#context_by_keyword(keyword) ⇒ Object



53
54
55
# File 'lib/tr8n/language.rb', line 53

def context_by_keyword(keyword)
  hash_value(contexts, keyword)
end

#context_by_token_name(token_name) ⇒ Object



57
58
59
# File 'lib/tr8n/language.rb', line 57

def context_by_token_name(token_name)
  contexts.values.detect{|ctx| ctx.applies_to_token?(token_name)}
end

#current_source(options) ⇒ Object



88
89
90
# File 'lib/tr8n/language.rb', line 88

def current_source(options)
  options[:source] || Tr8n.session.block_options[:source] || Tr8n.session.current_source || 'undefined'
end

#default?Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/tr8n/language.rb', line 69

def default?
  return true unless application
  application.default_locale == locale
end

#dirObject



74
75
76
# File 'lib/tr8n/language.rb', line 74

def dir
  right_to_left? ? "rtl" : "ltr"
end

#fetchObject



30
31
32
33
# File 'lib/tr8n/language.rb', line 30

def fetch
  update_attributes(application.api_client.get("language", {:locale => locale}))
  self
end

#full_nameObject



83
84
85
86
# File 'lib/tr8n/language.rb', line 83

def full_name
  return english_name if english_name == native_name
  "#{english_name} - #{native_name}"
end

#has_definition?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/tr8n/language.rb', line 65

def has_definition?
  contexts.any?
end

#to_cache_hashObject



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/tr8n/language.rb', line 233

def to_cache_hash
  hash = to_hash(:locale, :name, :english_name, :native_name, :right_to_left, :flag_url)
  hash[:contexts] = {}
  contexts.each do |name, value|
    hash[:contexts][name] = value.to_cache_hash
  end
  hash[:cases] = {}
  cases.each do |name, value|
    hash[:cases][name] = value.to_cache_hash
  end
  hash
end

#translate(label, description = nil, tokens = {}, options = {}) ⇒ Object Also known as: tr

Translation Methods

Note - when inline translation mode is enable, cache will not be used and translators will always hit the live service to get the most recent translations

Some cache adapters cache by source, others by key. Some are read-only, some are built on the fly.

There are three ways to call the tr method

tr(label, description = “”, tokens = {}, options = {}) or tr(label, tokens = {}, options = {}) or tr(:label => label, :description => “”, :tokens => {}, :options => {})



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/tr8n/language.rb', line 109

def translate(label, description = nil, tokens = {}, options = {})
  return label if label.tr8n_translated?

  params = Tr8n::Utils.normalize_tr_params(label, description, tokens, options)
  key_locale = hash_value(params[:options], :locale) || hash_value(Tr8n.session.block_options, :locale) || Tr8n.config.default_locale
  key_level = hash_value(params[:options], :level) || hash_value(Tr8n.session.block_options, :level) || Tr8n.config.default_level

  temp_key = Tr8n::TranslationKey.new({
      :application  => application,
      :label        => params[:label],
      :description  => params[:description],
      :locale       => key_locale,
      :level        => key_level,
      :translations => []
  })

  #Tr8n.logger.info("Translating " + params[:label] + " from: " + key_locale.inspect + " to " + locale.inspect)

  params[:tokens].merge!(:viewing_user => Tr8n.session.current_user)

  if Tr8n.config.disabled? or locale == key_locale
    return temp_key.substitute_tokens(params[:label], params[:tokens], self, params[:options]).tr8n_translated
  end

  translation_key = application.translation_key(temp_key.key)
  if translation_key
    return translation_key.translate(self, params[:tokens], params[:options])
  end

  # no cache or translator is using inline mode - use service, otherwise load from cache
  if not Tr8n.config.cache[:enabled] or (Tr8n.session.current_translator and Tr8n.session.current_translator.inline?)
    return translate_from_service(temp_key, params[:tokens], params[:options]).tr8n_translated  
  end

  translate_from_cache(temp_key, params[:tokens], params[:options]).tr8n_translated
#rescue Exception => ex
#  Tr8n.logger.error("Failed to translate: #{params[:label]} : #{ex.message}")
#  Tr8n.logger.error(ex.backtrace)
#  params[:label]
end

#translate_from_cache(translation_key, tokens, options) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/tr8n/language.rb', line 151

def translate_from_cache(translation_key, tokens, options)
  # In most scenarios translations should be cached by source
  if Tr8n.cache.cached_by_source?
    source_key = current_source(options)
    cache_key = Tr8n::Source.cache_key(source_key, locale);
    source = Tr8n.cache.fetch(cache_key)

    # if it came from cache, it will be full of translation keys with translations for the locale
    if source
      translation_keys = source.translation_keys || {}
    elsif Tr8n.cache.read_only?
      translation_keys = {}
    else
      # get the source info from the application
      source = application.source(source_key)
      translation_keys = source.fetch_translations_for_language(self, options)
      Tr8n.cache.store(cache_key, source)
    end

    if translation_keys[translation_key.key]
      translation_key = translation_keys[translation_key.key]
      return translation_key.translate(self, tokens, options)
    end

    translation_key.translations = {locale => []}
    application.cache_translation_key(translation_key)
    return translation_key.translate(self, tokens, options)
  end

  # CDB allows for caching by key
  cache_key = Tr8n::TranslationKey.cache_key(translation_key.label, translation_key.description, locale)
  translations = Tr8n.cache.fetch(cache_key)

  if translations.nil?
    if Tr8n.cache.read_only?
      translation_key.translations = {locale => []}
      application.cache_translation_key(translation_key)
      return translation_key.translate(self, tokens, options)
    end

    translation_key = translation_key.fetch_translations(self, options)
    Tr8n.cache.store(cache_key, translation_key.translations(self))
    return translation_key.translate(self, tokens, options)
  end

  unless translations.is_a?(Array)
    translations = [translations]
  end

  translation_key.translations = {locale => translations}
  application.cache_translation_key(translation_key)
  translation_key.translate(self, tokens, options)
end

#translate_from_service(translation_key, tokens, options) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/tr8n/language.rb', line 205

def translate_from_service(translation_key, tokens, options)
  source_key = current_source(options)

  source = application.source(source_key)
  source_translation_keys = source.fetch_translations_for_language(self, options)
  if source_translation_keys[translation_key.key]
    translation_key = source_translation_keys[translation_key.key]
  else
    application.register_missing_key(translation_key, source)
    memory_cached_translation_key = application.translation_key(translation_key.key)
    translation_key = memory_cached_translation_key if memory_cached_translation_key
  end

  translation_key.translate(self, tokens, options)
end

#update_attributes(attrs) ⇒ Object



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

def update_attributes(attrs)
  super

  self.attributes[:contexts] = {}
  if hash_value(attrs, :contexts)
    hash_value(attrs, :contexts).each do |key, context|
      self.attributes[:contexts][key] = Tr8n::LanguageContext.new(context.merge(:language => self))
    end
  end

  self.attributes[:cases] = {}
  if hash_value(attrs, :cases)
    hash_value(attrs, :cases).each do |key, lcase|
      self.attributes[:cases][key] = Tr8n::LanguageCase.new(lcase.merge(:language => self))
    end
  end
end