Class: Tml::Application

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

Constant Summary collapse

API_HOST =
'https://api.translationexchange.com'
CDN_HOST =
'https://cdn.translationexchange.com'

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 Tml::Base

Dynamic Method Handling

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

Class Method Details

.cache_keyObject

Returns application cache key



46
47
48
# File 'lib/tml/application.rb', line 46

def self.cache_key
  'application'
end

.translations_cache_key(locale) ⇒ Object

Returns translations cache key



51
52
53
# File 'lib/tml/application.rb', line 51

def self.translations_cache_key(locale)
  "#{locale}/translations"
end

Instance Method Details

#add_language(new_language) ⇒ Object

Adds a language to the application



168
169
170
171
172
173
174
175
# File 'lib/tml/application.rb', line 168

def add_language(new_language)
  self.languages_by_locale ||= {}
  return self.languages_by_locale[new_language.locale] if self.languages_by_locale[new_language.locale]
  new_language.application = self
  self.languages << new_language
  self.languages_by_locale[new_language.locale] = new_language
  new_language
end

#api_clientObject

Create API client



329
330
331
# File 'lib/tml/application.rb', line 329

def api_client
  @api_client ||= Tml.config.api_client[:class].new(application: self)
end

#cache_translations(locale, key, new_translations) ⇒ Object

Cache translations within application object



283
284
285
286
287
288
289
290
291
292
293
# File 'lib/tml/application.rb', line 283

def cache_translations(locale, key, new_translations)
  self.translations ||= {}
  self.translations[locale] ||= {}
  self.translations[locale][key] = new_translations.collect do |t|
    Tml::Translation.new(
      :locale => t['locale'] || locale,
      :label => t['label'],
      :context => t['context']
    )
  end
end

#cached_translations(locale, key) ⇒ Object

Get application cached translations



296
297
298
299
# File 'lib/tml/application.rb', line 296

def cached_translations(locale, key)
  return unless self.translations and self.translations[locale]
  self.translations[locale][key]
end

#cdn_hostObject



63
64
65
# File 'lib/tml/application.rb', line 63

def cdn_host
  super || CDN_HOST
end

#current_language(locale) ⇒ Object

Normalizes and returns current language



158
159
160
161
162
163
164
165
# File 'lib/tml/application.rb', line 158

def current_language(locale)
  return Tml.config.default_language unless locale
  locale = locale.gsub('_', '-') if locale
  lang = language(locale)
  lang ||= language(locale.split('-').first) if locale.index('-')
  lang ||= Tml.config.default_language
  lang
end

#debug_translationsObject

Debug translations



302
303
304
305
306
307
308
309
310
311
# File 'lib/tml/application.rb', line 302

def debug_translations
  return 'no translations' unless self.translations
  self.translations.each do |locale, keys|
    pp [locale, keys.collect{|key, translations|
      [key, translations.collect{|t|
        [t.label, t.context]
      }]
    }]
  end
end

#default_data_token(token) ⇒ Object

Get default data token



319
320
321
# File 'lib/tml/application.rb', line 319

def default_data_token(token)
  hash_value(tokens, "data.#{token.to_s}")
end

#default_decoration_token(token) ⇒ Object

Get default decoration token



314
315
316
# File 'lib/tml/application.rb', line 314

def default_decoration_token(token)
  hash_value(tokens, "decoration.#{token.to_s}")
end

#feature_enabled?(key) ⇒ Boolean

Check if feature is enabled

Returns:

  • (Boolean)


324
325
326
# File 'lib/tml/application.rb', line 324

def feature_enabled?(key)
  hash_value(features, key.to_s)
end

#fetchObject

Fetches application definition from the service



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tml/application.rb', line 68

def fetch
  data = api_client.get("projects/#{key}/definition",{
    locale: Tml.session.current_locale,
    source: Tml.session.current_source,
    ignored: true
  }, {
    cache_key: self.class.cache_key
  })

  if data
    update_attributes(data)
  else
    add_language(Tml.config.default_language)
    Tml.logger.debug('Cache is disabled or no data has been cached.')
  end

  self
rescue Tml::Exception => ex
  Tml.logger.error("Failed to load application: #{ex}")
  self
end

#fetch_translations(locale) ⇒ Object

Fetch translations from API



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/tml/application.rb', line 248

def fetch_translations(locale)
  self.translations ||= {}
  self.translations[locale] ||= begin
    results = Tml.cache.fetch(Tml::Application.translations_cache_key(locale)) do
      data = {}
      unless Tml.cache.read_only?
        data = api_client.get("projects/#{key}/translations", :all => true, :ignored => true, :raw_json => true)
      end
      data
    end

    if results.is_a?(Hash) and results['results']
      results = results['results']
      self.ignored_keys = results['ignored_keys'] || []
    end

    translations_by_key = {}
    results.each do |key, data|
      translations_data = data.is_a?(Hash) ? data['translations'] : data
      translations_by_key[key] = translations_data.collect do |t|
        Tml::Translation.new(
          :locale => t['locale'] || locale,
          :label => t['label'],
          :locked => t['locked'],
          :context => t['context']
        )
      end
    end
    translations_by_key
  end
rescue Tml::Exception => ex
  {}
end

#hostObject



59
60
61
# File 'lib/tml/application.rb', line 59

def host
  super || API_HOST
end

#ignored_key?(key) ⇒ Boolean

Check if a key is ignored

Returns:

  • (Boolean)


242
243
244
245
# File 'lib/tml/application.rb', line 242

def ignored_key?(key)
  return false if ignored_keys.nil?
  not ignored_keys.index(key).nil?
end

#language(locale = nil) ⇒ Object

Returns language by locale



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/tml/application.rb', line 138

def language(locale = nil)
  locale = nil if locale and locale.strip == ''

  locale ||= default_locale || Tml.config.default_locale
  locale = locale.to_s

  self.languages_by_locale ||= {}
  self.languages_by_locale[locale] ||= api_client.get("languages/#{locale}/definition", {
  }, {
    class:      Tml::Language,
    attributes: {locale: locale, application: self},
    cache_key:  Tml::Language.cache_key(locale)
  })
rescue Tml::Exception => e
  Tml.logger.error(e)
  Tml.logger.error(e.backtrace)
  self.languages_by_locale[locale] = Tml.config.default_language
end

#load_extensions(extensions) ⇒ Object

Loads application extensions, if any



105
106
107
108
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
# File 'lib/tml/application.rb', line 105

def load_extensions(extensions)
  return if extensions.nil?
  source_locale = default_locale

  cache = Tml.cache
  cache = nil if not Tml.cache.enabled? or Tml.session.inline_mode?

  if hash_value(extensions, :languages)
    self.languages_by_locale ||= {}
    hash_value(extensions, :languages).each do |locale, data|
      source_locale = locale if locale != source_locale
      cache.store(Tml::Language.cache_key(locale), data) if cache
      self.languages_by_locale[locale] = Tml::Language.new(data.merge(
        locale: locale,
        application: self
      ))
    end
  end

  if hash_value(extensions, :sources)
    self.sources ||= {}
    hash_value(extensions, :sources).each do |source, data|
      cache.store(Tml::Source.cache_key(source_locale, source), data) if cache
      self.sources[source] ||= Tml::Source.new(
        application:  self,
        source:       source
      )
      self.sources[source].update_translations(source_locale, data)
    end
  end
end

#localesObject

Returns a list of application supported locales



178
179
180
# File 'lib/tml/application.rb', line 178

def locales
  @locales ||= languages.collect{|lang| lang.locale}
end

#register_keys(keys) ⇒ Object

Register keys



211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/tml/application.rb', line 211

def register_keys(keys)
  params = []
  keys.each do |source_key, keys|
    source = Tml::Source.new(:source => source_key, :application => self)
    params << {:source => source_key, :keys => keys.values.collect{|tkey| tkey.to_hash(:label, :description, :locale, :level)}}
    source.reset_cache
  end

  api_client.post('sources/register_keys', {:source_keys => params.to_json})
rescue Tml::Exception => e
  Tml.logger.error('Failed to register missing translation keys...')
  Tml.logger.error(e)
  Tml.logger.error(e.backtrace)
end

#register_missing_key(source_key, tkey) ⇒ Object

Register missing keys



201
202
203
204
205
206
207
208
# File 'lib/tml/application.rb', line 201

def register_missing_key(source_key, tkey)
  return if Tml.cache.enabled? and not Tml.session.inline_mode?
  source_key = source_key.to_s
  @missing_keys_by_sources ||= {}
  @missing_keys_by_sources[source_key] ||= {}
  @missing_keys_by_sources[source_key][tkey.key] ||= tkey
  submit_missing_keys if Tml.config.submit_missing_keys_realtime
end

#reset_translation_cacheObject

Reset translation cache



234
235
236
237
238
239
# File 'lib/tml/application.rb', line 234

def reset_translation_cache
  self.sources = {}
  self.translations = {}
  @languages_by_locale      = nil
  @missing_keys_by_sources  = nil
end

#source(key, locale) ⇒ Object

Returns source by key



183
184
185
186
187
188
189
# File 'lib/tml/application.rb', line 183

def source(key, locale)
  self.sources ||= {}
  self.sources[key] ||= Tml::Source.new(
    :application  => self,
    :source       => key
  ).fetch_translations(locale)
end

#submit_missing_keysObject

Submit missing translation keys



227
228
229
230
231
# File 'lib/tml/application.rb', line 227

def submit_missing_keys
  return if @missing_keys_by_sources.nil? or @missing_keys_by_sources.empty?
  register_keys(@missing_keys_by_sources)
  @missing_keys_by_sources = nil
end

#tokenObject



55
56
57
# File 'lib/tml/application.rb', line 55

def token
  access_token
end

#update_attributes(attrs) ⇒ Object

Updates application attributes



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tml/application.rb', line 91

def update_attributes(attrs)
  super

  self.attributes[:languages] = []
  if hash_value(attrs, :languages)
    self.attributes[:languages] = hash_value(attrs, :languages).collect{ |l| Tml::Language.new(l.merge(:application => self)) }
  end

  load_extensions(hash_value(attrs, :extensions))

  self
end

#verify_source_path(source_key, source_path) ⇒ Object

Verify current source path



192
193
194
195
196
197
198
# File 'lib/tml/application.rb', line 192

def verify_source_path(source_key, source_path)
  return if Tml.cache.enabled? and not Tml.session.inline_mode?
  return if extensions.nil? or extensions['sources'].nil?
  return unless extensions['sources'][source_key].nil?
  @missing_keys_by_sources ||= {}
  @missing_keys_by_sources[source_path] ||= {}
end