Class: Tr8n::Application
- Inherits:
-
Base
- Object
- Base
- Tr8n::Application
show all
- Defined in:
- lib/tr8n/application.rb
Instance Attribute Summary
Attributes inherited from Base
#attributes
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
Instance Method Details
#add_language(new_language) ⇒ Object
100
101
102
103
104
105
106
107
|
# File 'lib/tr8n/application.rb', line 100
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
233
234
235
|
# File 'lib/tr8n/application.rb', line 233
def api_client
@api_client ||= Tr8n::ApiClient.new(:application => self)
end
|
#cache_translation_key(tkey) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/tr8n/application.rb', line 155
def cache_translation_key(tkey)
cached_key = translation_key(tkey.key)
if 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
170
171
172
173
174
|
# File 'lib/tr8n/application.rb', line 170
def cache_translation_keys(tkeys)
tkeys.each do |tkey|
cache_translation_key(tkey)
end
end
|
#component(key, register = true) ⇒ Object
138
139
140
141
142
143
144
145
|
# File 'lib/tr8n/application.rb', line 138
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
221
222
223
|
# File 'lib/tr8n/application.rb', line 221
def default_data_token(token)
hash_value(tokens, "data.#{token.to_s}")
end
|
#default_decoration_token(token) ⇒ Object
217
218
219
|
# File 'lib/tr8n/application.rb', line 217
def default_decoration_token(token)
hash_value(tokens, "decoration.#{token.to_s}")
end
|
#feature_enabled?(key) ⇒ Boolean
225
226
227
|
# File 'lib/tr8n/application.rb', line 225
def feature_enabled?(key)
hash_value(features, key.to_s)
end
|
#featured_languages ⇒ Object
205
206
207
208
209
210
211
|
# File 'lib/tr8n/application.rb', line 205
def featured_languages
@featured_languages ||= begin
locales = api_client.get("application/featured_locales", {}, {:cache_key => "featured_locales"})
(locales.nil? or locales.empty?) ? [] : languages.select{|l| locales.include?(l.locale)}
end
end
|
#fetch ⇒ Object
39
40
41
42
43
44
|
# File 'lib/tr8n/application.rb', line 39
def fetch
update_attributes(api_client.get("application", {:definition => true}, {:cache_key => key}))
rescue Tr8n::Exception => ex
Tr8n.logger.error("Failed to load application: #{ex}")
self
end
|
#language(locale = nil) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/tr8n/application.rb', line 76
def language(locale = nil)
locale ||= default_locale || Tr8n.config.default_locale
@languages_by_locale ||= {}
unless Tr8n.session.current_translator and Tr8n.session.current_translator.inline?
cache_key = "#{locale}/language"
end
@languages_by_locale[locale] ||= api_client.get(
'language',
{:locale => locale},
{
:class => Tr8n::Language,
:attributes => {:locale => locale, :application => self},
:cache_key => cache_key
}
)
rescue Tr8n::Exception => e
Tr8n.logger.error(e)
Tr8n.logger.error(e.backtrace)
@languages_by_locale[locale] = Tr8n.config.default_language
end
|
#locales ⇒ Object
109
110
111
|
# File 'lib/tr8n/application.rb', line 109
def locales
@locales ||= languages.collect{|lang| lang.locale}
end
|
#register_keys(keys) ⇒ Object
189
190
191
192
193
194
195
196
197
|
# File 'lib/tr8n/application.rb', line 189
def register_keys(keys)
params = []
keys.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})
end
|
#register_missing_key(source_key, tkey) ⇒ Object
182
183
184
185
186
187
|
# File 'lib/tr8n/application.rb', line 182
def register_missing_key(source_key, tkey)
@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_cache ⇒ Object
176
177
178
179
180
|
# File 'lib/tr8n/application.rb', line 176
def reset_translation_cache
@translation_keys = {}
@sources_by_key = {}
@components_by_key = {}
end
|
#source(key, locale) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/tr8n/application.rb', line 117
def source(key, locale)
@sources_by_key ||= {}
return @sources_by_key[key] if @sources_by_key[key]
unless Tr8n.session.current_translator and Tr8n.session.current_translator.inline?
cache_key = Tr8n::Source.cache_key(key, locale)
end
@sources_by_key[key] ||= api_client.get(
"source",
{:source => key, :locale => locale, :translations => true},
{
:class => Tr8n::Source,
:attributes => {:application => self},
:cache_key => cache_key
}
)
rescue
@sources_by_key[key] = Tr8n::Source.new(:source => key)
end
|
#submit_missing_keys ⇒ Object
199
200
201
202
203
|
# File 'lib/tr8n/application.rb', line 199
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
|
#translation_key(key) ⇒ Object
151
152
153
|
# File 'lib/tr8n/application.rb', line 151
def translation_key(key)
translation_keys[key]
end
|
#translation_keys ⇒ Object
147
148
149
|
# File 'lib/tr8n/application.rb', line 147
def translation_keys
@translation_keys ||= {}
end
|
#translators ⇒ Object
213
214
215
|
# File 'lib/tr8n/application.rb', line 213
def translators
@translators ||= api_client.get("application/translators", {}, {:class => Tr8n::Translator, :attributes => {:application => self}})
end
|
#update_attributes(attrs) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/tr8n/application.rb', line 46
def update_attributes(attrs)
super
self.host ||= "https://translationexchange.com"
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
self
end
|
#url_for(path) ⇒ Object
113
114
115
|
# File 'lib/tr8n/application.rb', line 113
def url_for(path)
"#{host}#{path}"
end
|