Class: Tr8n::Application

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



39
40
41
# File 'lib/tr8n/application.rb', line 39

def self.cache_key
  'application'
end

.translations_cache_key(locale) ⇒ Object



43
44
45
# File 'lib/tr8n/application.rb', line 43

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

Instance Method Details

#add_language(new_language) ⇒ Object

Mostly used for testing



85
86
87
88
89
90
91
92
# File 'lib/tr8n/application.rb', line 85

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

#api_clientObject



246
247
248
# File 'lib/tr8n/application.rb', line 246

def api_client
  @api_client ||= Tr8n::Api::Client.new(:application => self)
end

#cache_translations(locale, key, new_translations) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/tr8n/application.rb', line 206

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

#cached_translations(locale, key) ⇒ Object



218
219
220
221
# File 'lib/tr8n/application.rb', line 218

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

#component(key, register = true) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/tr8n/application.rb', line 114

def component(key, register = true)
  key = key.key if key.is_a?(Tr8n::Component)

  return self.components[key] if self.components[key]
  return nil unless register

  self.components[key] ||= api_client.post('components/register', {:component => key}, {:class => Tr8n::Component, :attributes => {:application => self}})
end

#debug_translationsObject



223
224
225
226
227
228
229
230
231
232
# File 'lib/tr8n/application.rb', line 223

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



238
239
240
# File 'lib/tr8n/application.rb', line 238

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

#default_decoration_token(token) ⇒ Object



234
235
236
# File 'lib/tr8n/application.rb', line 234

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

#feature_enabled?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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


154
155
156
157
158
159
160
161
# File 'lib/tr8n/application.rb', line 154

def featured_languages
  @featured_languages ||= begin
    locales = api_client.get('applications/current/featured_locales', {}, {:cache_key => 'featured_locales'})
    (locales.nil? or locales.empty?) ? [] : languages.select{|l| locales.include?(l.locale)}
  end
rescue
  []
end

#fetchObject



47
48
49
50
51
52
# File 'lib/tr8n/application.rb', line 47

def fetch
  update_attributes(api_client.get('applications/current', {:definition => true}, {:cache_key => self.class.cache_key}))
rescue Tr8n::Exception => ex
  Tr8n.logger.error("Failed to load application: #{ex}")
  self
end

#fetch_translations(locale) ⇒ Object



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
204
# File 'lib/tr8n/application.rb', line 176

def fetch_translations(locale)
  self.translations ||= {}
  self.translations[locale] ||= begin
    results = Tr8n.cache.fetch(Tr8n::Application.translations_cache_key(locale)) do
      data = {}
      unless Tr8n.cache.read_only?
        api_client.paginate('applications/current/translations', :per_page => 1000) do |translations|
          data.merge!(translations)
        end
      end
      data
    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|
        Tr8n::Translation.new(
          :locale => t['locale'] || locale,
          :label => t['label'],
          :context => t['context']
        )
      end
    end
    translations_by_key
  end
rescue Tr8n::Exception => ex
  {}
end

#language(locale = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tr8n/application.rb', line 65

def language(locale = nil)
  locale = nil if locale.strip == ''
  locale ||= default_locale || Tr8n.config.default_locale
  @languages_by_locale ||= {}
  @languages_by_locale[locale] ||= api_client.get(
    "languages/#{locale}",
    {:definition => true},
    {
      :class => Tr8n::Language,
      :attributes => {:locale => locale, :application => self},
      :cache_key => Tr8n::Language.cache_key(locale)
    }
  )
rescue Tr8n::Exception => e
  Tr8n.logger.error(e)
  Tr8n.logger.error(e.backtrace)
  @languages_by_locale[locale] = Tr8n.config.default_language
end

#localesObject



94
95
96
# File 'lib/tr8n/application.rb', line 94

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

#postofficeObject



250
251
252
# File 'lib/tr8n/application.rb', line 250

def postoffice
  @postoffice ||= Tr8n::Api::PostOffice.new(:application => self)
end

#register_keys(keys) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/tr8n/application.rb', line 132

def register_keys(keys)
  params = []
  keys.each do |source_key, keys|
    next unless keys.values.any?
    source = Tr8n::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 Tr8n::Exception => e
  Tr8n.logger.error('Failed to register missing translation keys...')
  Tr8n.logger.error(e)
  Tr8n.logger.error(e.backtrace)
end

#register_missing_key(source_key, tkey) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/tr8n/application.rb', line 123

def register_missing_key(source_key, tkey)
  return if Tr8n.cache.read_only? and not Tr8n.session.inline_mode?

  @missing_keys_by_sources ||= {}
  @missing_keys_by_sources[source_key] ||= {}
  @missing_keys_by_sources[source_key][tkey.key] ||= tkey
  submit_missing_keys if Tr8n.config.submit_missing_keys_realtime
end

#reset_translation_cacheObject



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

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

#source(source, locale) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/tr8n/application.rb', line 106

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

#submit_missing_keysObject



148
149
150
151
152
# File 'lib/tr8n/application.rb', line 148

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

#toolsObject



98
99
100
# File 'lib/tr8n/application.rb', line 98

def tools
  @attributes[:tools] || {}
end

#translatorsObject



163
164
165
166
167
# File 'lib/tr8n/application.rb', line 163

def translators
  @translators ||= api_client.get('applications/current/translators', {}, {:class => Tr8n::Translator, :attributes => {:application => self}})
rescue
  []
end

#update_attributes(attrs) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/tr8n/application.rb', line 54

def update_attributes(attrs)
  super

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

  self
end

#url_for(path) ⇒ Object



102
103
104
# File 'lib/tr8n/application.rb', line 102

def url_for(path)
  "#{tools['assets']}#{path}"
end