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_key(key) ⇒ Object



216
217
218
# File 'lib/tr8n/application.rb', line 216

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

.cache_prefixObject

Cache Methods



212
213
214
# File 'lib/tr8n/application.rb', line 212

def self.cache_prefix
  'a@'
end

Instance Method Details

#add_language(new_language) ⇒ Object

Mostly used for testing



97
98
99
100
101
102
103
104
105
# File 'lib/tr8n/application.rb', line 97

def add_language(new_language)
  lang = language(new_language.locale, false)
  return lang if lang

  new_language.application = self
  self.languages << new_language
  @languages_by_locale[new_language.locale] = new_language
  new_language
end

#api_clientObject

API Methods



236
237
238
# File 'lib/tr8n/application.rb', line 236

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

#cache_translation_key(tkey) ⇒ Object



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

def cache_translation_key(tkey)
  cached_key = translation_key(tkey.key)

  if cached_key
    # move translations from tkey to the cached key
    tkey.translations.each do |locale, translations|
      cached_key.set_language_translations(language(locale), translations)
    end
    return cached_key
  end

  tkey.set_application(self)
  @translation_keys[tkey.key] = tkey
end

#cache_translation_keys(tkeys) ⇒ Object



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

def cache_translation_keys(tkeys)
  tkeys.each do |tkey|
    cache_translation_key(tkey)
  end
end

#component(key, register = true) ⇒ Object



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

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

  return @components_by_key[key] if @components_by_key[key]
  return nil unless register

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

#default_data_token(token) ⇒ Object



196
197
198
# File 'lib/tr8n/application.rb', line 196

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

#default_decoration_token(token) ⇒ Object



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

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

#feature_enabled?(key) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/tr8n/application.rb', line 204

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


178
179
180
181
182
183
184
185
186
# File 'lib/tr8n/application.rb', line 178

def featured_languages
  @featured_languages ||= begin
    locales = Tr8n.cache.fetch("featured_locales") do
      api_client.get("application/featured_locales")
    end
    # use app languages, there is no need for rules for this call
    (locales.nil? or locales.empty?) ? [] : languages.select{|l| locales.include?(l.locale)}
  end
end

#fetchObject



31
32
33
34
# File 'lib/tr8n/application.rb', line 31

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

#js_boot_urlObject



200
201
202
# File 'lib/tr8n/application.rb', line 200

def js_boot_url
  "#{host}/tr8n/api/proxy/boot.js?client_id=#{key}"
end

#language(locale = nil, fetch = true) ⇒ Object



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

def language(locale = nil, fetch = true)
  locale ||= default_locale || Tr8n.config.default_locale

  @languages_by_locale ||= {}
  return @languages_by_locale[locale] if @languages_by_locale[locale]

  if Tr8n.config.cache[:enabled]
    language = Tr8n.cache.fetch(Tr8n::Language.cache_key(locale))
    if language
      language.application = self
      @languages_by_locale[locale] = language
      return language
    end
  end

  return nil unless fetch

  @languages_by_locale[locale] = Tr8n::Language.new(:locale => locale, :application => self).fetch

  if Tr8n.config.cache[:enabled] and not Tr8n.cache.read_only?
    Tr8n.cache.store(Tr8n::Language.cache_key(locale), @languages_by_locale[locale])
  end

  @languages_by_locale[locale]
end

#localesObject



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

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

#register_missing_key(tkey, source) ⇒ Object



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

def register_missing_key(tkey, source)
  @missing_keys_by_sources ||= {}
  @missing_keys_by_sources[source.source] ||= {}
  @missing_keys_by_sources[source.source][tkey.key] ||= tkey
  submit_missing_keys if Tr8n.config.submit_missing_keys_realtime
end

#reset_translation_cacheObject



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

def reset_translation_cache
  @translation_keys = {}
  @sources_by_key = {}
  @components_by_key = {}
end

#source(key, register = true) ⇒ Object



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

def source(key, register = true)
  key = key.source if key.is_a?(Tr8n::Source)

  return @sources_by_key[key] if @sources_by_key[key]
  return nil unless register

  @sources_by_key[key] ||= api_client.post("source/register", {:source => key}, {:class => Tr8n::Source, :attributes => {:application => self}})
end

#submit_missing_keysObject



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

def submit_missing_keys
  return if @missing_keys_by_sources.nil? or @missing_keys_by_sources.empty?
  params = []
  @missing_keys_by_sources.each do |source, keys|
    next unless keys.values.any?
    params << {:source => source, :keys => keys.values.collect{|tkey| tkey.to_hash(:label, :description, :locale, :level)}}
  end
  api_client.post('source/register_keys', {:source_keys => params.to_json})
  @missing_keys_by_sources = nil
end

#to_cache_hashObject



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/tr8n/application.rb', line 220

def to_cache_hash
  hash = to_hash(:host, :key, :secret, :access_token,
                 :name, :description, :threshold,
                 :default_locale, :default_level, :features,
                 :tokens, :css, :shortcuts)
  hash["languages"] = []
  languages.each do |lang|
    hash["languages"] << lang.to_hash(:locale, :name, :english_name, :native_name, :right_to_left, :flag_url)
  end
  hash
end

#translation_key(key) ⇒ Object



129
130
131
# File 'lib/tr8n/application.rb', line 129

def translation_key(key)
  translation_keys[key]
end

#translation_keysObject



125
126
127
# File 'lib/tr8n/application.rb', line 125

def translation_keys
  @translation_keys ||= {}
end

#translatorsObject



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

def translators
  api_client.get("application/translators", {}, {:class => Tr8n::Translator, :attributes => {:application => self}})
end

#update_attributes(attrs) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tr8n/application.rb', line 36

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.attributes[:sources] = []
  if hash_value(attrs, :sources)
    self.attributes[:sources] = hash_value(attrs, :sources).collect{ |l| Tr8n::Source.new(l.merge(:application => self)) }
  end

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

  @translation_keys         = {}
  @sources_by_key           = {}
  @components_by_key        = {}

  @languages_by_locale      = nil
  @missing_keys_by_sources  = nil
end

#url_for(path) ⇒ Object



92
93
94
# File 'lib/tr8n/application.rb', line 92

def url_for(path)
  "#{host}#{path}"
end