Class: Tml::Application
Constant Summary collapse
- API_HOST =
'https://api.translationexchange.com'- CDN_HOST =
'https://cdn.translationexchange.com'
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.cache_key ⇒ Object
Returns application cache key.
-
.translations_cache_key(locale) ⇒ Object
Returns translations cache key.
Instance Method Summary collapse
-
#add_language(new_language) ⇒ Object
Adds a language to the application.
-
#allow_key_registration? ⇒ Boolean
checks if key registration is allowed currently it is based on user’s inline mode.
-
#api_client ⇒ Object
Create API client.
-
#cache_translations(locale, key, new_translations) ⇒ Object
Cache translations within application object.
-
#cached_translations(locale, key) ⇒ Object
Get application cached translations.
-
#cdn_host ⇒ Object
CDN host.
-
#current_language(locale) ⇒ Object
Normalizes and returns current language.
-
#debug_translations ⇒ Object
Debug translations.
-
#default_data_token(token) ⇒ Object
Get default data token.
-
#default_decoration_token(token) ⇒ Object
Get default decoration token.
-
#feature_enabled?(key) ⇒ Boolean
Check if feature is enabled.
-
#fetch ⇒ Object
Fetches application definition from the service.
-
#fetch_translations(locale) ⇒ Object
Fetch translations from API.
-
#host ⇒ Object
API host.
-
#ignored_key?(key) ⇒ Boolean
Check if a key is ignored.
-
#language(locale = nil) ⇒ Object
Returns language by locale.
-
#load_extensions(extensions) ⇒ Object
Loads application extensions, if any.
-
#locales ⇒ Object
Returns a list of application supported locales.
-
#register_keys(keys) ⇒ Object
Register keys TODO: make this async using a separate thread, like in: github.com/airbrake/airbrake-ruby/blob/master/lib/airbrake-ruby/async_sender.rb.
-
#register_missing_key(source_key, tkey) ⇒ Object
Register missing keys.
-
#reset_translation_cache ⇒ Object
Reset translation cache.
-
#source(key, locale) ⇒ Object
Returns source by key.
-
#submit_missing_keys ⇒ Object
Submit missing translation keys.
- #token ⇒ Object
-
#update_attributes(attrs) ⇒ Object
Updates application attributes.
-
#verify_source_path(source_key, source_path) ⇒ Object
Verify current source path.
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
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
170 171 172 173 174 175 176 177 |
# File 'lib/tml/application.rb', line 170 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 |
#allow_key_registration? ⇒ Boolean
checks if key registration is allowed currently it is based on user’s inline mode
195 196 197 198 |
# File 'lib/tml/application.rb', line 195 def allow_key_registration? return if token.nil? Tml.session.inline_mode? end |
#api_client ⇒ Object
Create API client
345 346 347 |
# File 'lib/tml/application.rb', line 345 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
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/tml/application.rb', line 299 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
312 313 314 315 |
# File 'lib/tml/application.rb', line 312 def cached_translations(locale, key) return unless self.translations and self.translations[locale] self.translations[locale][key] end |
#cdn_host ⇒ Object
CDN host
65 66 67 |
# File 'lib/tml/application.rb', line 65 def cdn_host super || CDN_HOST end |
#current_language(locale) ⇒ Object
Normalizes and returns current language
160 161 162 163 164 165 166 167 |
# File 'lib/tml/application.rb', line 160 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_translations ⇒ Object
Debug translations
318 319 320 321 322 323 324 325 326 327 |
# File 'lib/tml/application.rb', line 318 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
335 336 337 |
# File 'lib/tml/application.rb', line 335 def default_data_token(token) hash_value(tokens, "data.#{token.to_s}") end |
#default_decoration_token(token) ⇒ Object
Get default decoration token
330 331 332 |
# File 'lib/tml/application.rb', line 330 def default_decoration_token(token) hash_value(tokens, "decoration.#{token.to_s}") end |
#feature_enabled?(key) ⇒ Boolean
Check if feature is enabled
340 341 342 |
# File 'lib/tml/application.rb', line 340 def feature_enabled?(key) hash_value(features, key.to_s) end |
#fetch ⇒ Object
Fetches application definition from the service
70 71 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 70 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
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/tml/application.rb', line 264 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 |
#host ⇒ Object
API host
60 61 62 |
# File 'lib/tml/application.rb', line 60 def host super || API_HOST end |
#ignored_key?(key) ⇒ Boolean
Check if a key is ignored
258 259 260 261 |
# File 'lib/tml/application.rb', line 258 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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/tml/application.rb', line 140 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
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 136 137 |
# File 'lib/tml/application.rb', line 107 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 |
#locales ⇒ Object
Returns a list of application supported locales
180 181 182 |
# File 'lib/tml/application.rb', line 180 def locales @locales ||= languages.collect{|lang| lang.locale} end |
#register_keys(keys) ⇒ Object
Register keys TODO: make this async using a separate thread, like in: github.com/airbrake/airbrake-ruby/blob/master/lib/airbrake-ruby/async_sender.rb
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/tml/application.rb', line 224 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
211 212 213 214 215 216 217 218 219 |
# File 'lib/tml/application.rb', line 211 def register_missing_key(source_key, tkey) return unless allow_key_registration? 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_cache ⇒ Object
Reset translation cache
250 251 252 253 254 255 |
# File 'lib/tml/application.rb', line 250 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
185 186 187 188 189 190 191 |
# File 'lib/tml/application.rb', line 185 def source(key, locale) self.sources ||= {} self.sources[key] ||= Tml::Source.new( :application => self, :source => key ).fetch_translations(locale) end |
#submit_missing_keys ⇒ Object
Submit missing translation keys
243 244 245 246 247 |
# File 'lib/tml/application.rb', line 243 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 |
#token ⇒ Object
55 56 57 |
# File 'lib/tml/application.rb', line 55 def token access_token end |
#update_attributes(attrs) ⇒ Object
Updates application attributes
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/tml/application.rb', line 93 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
201 202 203 204 205 206 207 208 |
# File 'lib/tml/application.rb', line 201 def verify_source_path(source_key, source_path) return unless allow_key_registration? 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 |