Class: Tml::Application
- Inherits:
-
Base
- Object
- Base
- Tml::Application
show all
- Defined in:
- lib/tml/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 Tml::Base
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Tml::Base
Class Method Details
.cache_key ⇒ Object
39
40
41
|
# File 'lib/tml/application.rb', line 39
def self.cache_key
'application'
end
|
.translations_cache_key(locale) ⇒ Object
43
44
45
|
# File 'lib/tml/application.rb', line 43
def self.translations_cache_key(locale)
"#{locale}/translations"
end
|
Instance Method Details
#add_language(new_language) ⇒ Object
101
102
103
104
105
106
107
108
|
# File 'lib/tml/application.rb', line 101
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_client ⇒ Object
262
263
264
|
# File 'lib/tml/application.rb', line 262
def api_client
@api_client ||= Tml::Api::Client.new(:application => self)
end
|
#cache_translations(locale, key, new_translations) ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/tml/application.rb', line 222
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
234
235
236
237
|
# File 'lib/tml/application.rb', line 234
def cached_translations(locale, key)
return unless self.translations and self.translations[locale]
self.translations[locale][key]
end
|
#component(key, register = true) ⇒ Object
130
131
132
133
134
135
136
137
|
# File 'lib/tml/application.rb', line 130
def component(key, register = true)
key = key.key if key.is_a?(Tml::Component)
return self.components[key] if self.components[key]
return nil unless register
self.components[key] ||= api_client.post('components/register', {:component => key}, {:class => Tml::Component, :attributes => {:application => self}})
end
|
#current_language(locale) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/tml/application.rb', line 92
def current_language(locale)
locale = locale.gsub('_', '-')
lang = language(locale)
lang ||= language(locale.split('-').first) if locale.index('-')
lang ||= Tml.config.default_language
lang
end
|
#debug_translations ⇒ Object
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/tml/application.rb', line 239
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
254
255
256
|
# File 'lib/tml/application.rb', line 254
def default_data_token(token)
hash_value(tokens, "data.#{token.to_s}")
end
|
#default_decoration_token(token) ⇒ Object
250
251
252
|
# File 'lib/tml/application.rb', line 250
def default_decoration_token(token)
hash_value(tokens, "decoration.#{token.to_s}")
end
|
#feature_enabled?(key) ⇒ Boolean
258
259
260
|
# File 'lib/tml/application.rb', line 258
def feature_enabled?(key)
hash_value(features, key.to_s)
end
|
#featured_languages ⇒ Object
170
171
172
173
174
175
176
177
|
# File 'lib/tml/application.rb', line 170
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
|
#fetch ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/tml/application.rb', line 47
def fetch
data = api_client.get('applications/current', {:definition => true}, {:cache_key => self.class.cache_key})
if data
update_attributes(data)
else
Tml.logger.debug('Cache enabled but no data is provided.')
end
self
rescue Tml::Exception => ex
Tml.logger.error("Failed to load application: #{ex}")
self
end
|
#fetch_translations(locale) ⇒ Object
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/tml/application.rb', line 192
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?
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|
Tml::Translation.new(
:locale => t['locale'] || locale,
:label => t['label'],
:context => t['context']
)
end
end
translations_by_key
end
rescue Tml::Exception => ex
{}
end
|
#language(locale = nil) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/tml/application.rb', line 72
def language(locale = nil)
locale = nil if locale.strip == ''
locale ||= default_locale || Tml.config.default_locale
@languages_by_locale ||= {}
@languages_by_locale[locale] ||= api_client.get(
"languages/#{locale}",
{:definition => true},
{
: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)
@languages_by_locale[locale] = Tml.config.default_language
end
|
#locales ⇒ Object
110
111
112
|
# File 'lib/tml/application.rb', line 110
def locales
@locales ||= languages.collect{|lang| lang.locale}
end
|
#postoffice ⇒ Object
266
267
268
|
# File 'lib/tml/application.rb', line 266
def postoffice
@postoffice ||= Tml::Api::PostOffice.new(:application => self)
end
|
#register_keys(keys) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/tml/application.rb', line 148
def register_keys(keys)
params = []
keys.each do |source_key, keys|
next unless keys.values.any?
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
139
140
141
142
143
144
145
146
|
# File 'lib/tml/application.rb', line 139
def register_missing_key(source_key, tkey)
return if Tml.cache.read_only? and not Tml.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 Tml.config.submit_missing_keys_realtime
end
|
#reset_translation_cache ⇒ Object
185
186
187
188
189
190
|
# File 'lib/tml/application.rb', line 185
def reset_translation_cache
self.sources = {}
self.translations = {}
@languages_by_locale = nil
@missing_keys_by_sources = nil
end
|
#source(source, locale) ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/tml/application.rb', line 122
def source(source, locale)
self.sources ||= {}
self.sources[source] ||= Tml::Source.new(
:application => self,
:source => source
).fetch_translations(locale)
end
|
#submit_missing_keys ⇒ Object
164
165
166
167
168
|
# File 'lib/tml/application.rb', line 164
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
|
114
115
116
|
# File 'lib/tml/application.rb', line 114
def tools
@attributes[:tools] || {}
end
|
#translators ⇒ Object
179
180
181
182
183
|
# File 'lib/tml/application.rb', line 179
def translators
@translators ||= api_client.get('applications/current/translators', {}, {:class => Tml::Translator, :attributes => {:application => self}})
rescue
[]
end
|
#update_attributes(attrs) ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/tml/application.rb', line 61
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
self
end
|
#url_for(path) ⇒ Object
118
119
120
|
# File 'lib/tml/application.rb', line 118
def url_for(path)
"#{tools['assets']}#{path}"
end
|