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 if locale is passed as nil, default locale will be used.
-
#debug_translations ⇒ Object
Debug translations.
-
#default_data_token(token) ⇒ Object
Get default data token.
-
#default_decoration_token(token) ⇒ Object
Get default decoration token.
-
#default_locale ⇒ Object
Application or configuration default locale.
-
#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.
-
#supported_locale(locale) ⇒ Object
Returns supported locale or fallback locale.
-
#token ⇒ Object
Returns application token.
-
#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
186 187 188 189 190 191 192 193 |
# File 'lib/tml/application.rb', line 186 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
207 208 209 210 |
# File 'lib/tml/application.rb', line 207 def allow_key_registration? return if token.nil? Tml.session.inline_mode? end |
#api_client ⇒ Object
Create API client
359 360 361 |
# File 'lib/tml/application.rb', line 359 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
311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/tml/application.rb', line 311 def cache_translations(locale, key, new_translations) return if new_translations.nil? 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
326 327 328 329 |
# File 'lib/tml/application.rb', line 326 def cached_translations(locale, key) return unless self.translations and self.translations[locale] self.translations[locale][key] end |
#cdn_host ⇒ Object
CDN host
66 67 68 |
# File 'lib/tml/application.rb', line 66 def cdn_host super || CDN_HOST end |
#current_language(locale) ⇒ Object
Normalizes and returns current language if locale is passed as nil, default locale will be used
180 181 182 183 |
# File 'lib/tml/application.rb', line 180 def current_language(locale) return Tml.config.default_language unless locale language(supported_locale(locale)) || Tml.config.default_language end |
#debug_translations ⇒ Object
Debug translations
332 333 334 335 336 337 338 339 340 341 |
# File 'lib/tml/application.rb', line 332 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
349 350 351 |
# File 'lib/tml/application.rb', line 349 def default_data_token(token) hash_value(tokens, "data.#{token.to_s}") end |
#default_decoration_token(token) ⇒ Object
Get default decoration token
344 345 346 |
# File 'lib/tml/application.rb', line 344 def default_decoration_token(token) hash_value(tokens, "decoration.#{token.to_s}") end |
#default_locale ⇒ Object
Application or configuration default locale
147 148 149 |
# File 'lib/tml/application.rb', line 147 def default_locale self.attributes[:default_locale] || Tml.config.default_locale end |
#feature_enabled?(key) ⇒ Boolean
Check if feature is enabled
354 355 356 |
# File 'lib/tml/application.rb', line 354 def feature_enabled?(key) hash_value(features, key.to_s) end |
#fetch ⇒ Object
Fetches application definition from the service
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/tml/application.rb', line 71 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
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/tml/application.rb', line 276 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
61 62 63 |
# File 'lib/tml/application.rb', line 61 def host super || API_HOST end |
#ignored_key?(key) ⇒ Boolean
Check if a key is ignored
270 271 272 273 |
# File 'lib/tml/application.rb', line 270 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
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/tml/application.rb', line 162 def language(locale = nil) locale = supported_locale(locale) 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
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 138 139 |
# File 'lib/tml/application.rb', line 108 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_by_key ||= {} hash_value(extensions, :sources).each do |source, data| cache.store(Tml::Source.cache_key(source_locale, source), data) if cache source_key = "#{source_locale}/#{source}" self.sources_by_key[source_key] ||= Tml::Source.new( application: self, source: source ) self.sources_by_key[source_key].update_translations(source_locale, data) end end end |
#locales ⇒ Object
Returns a list of application supported locales
142 143 144 |
# File 'lib/tml/application.rb', line 142 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
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/tml/application.rb', line 236 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, :syntax)} } 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
223 224 225 226 227 228 229 230 231 |
# File 'lib/tml/application.rb', line 223 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
262 263 264 265 266 267 |
# File 'lib/tml/application.rb', line 262 def reset_translation_cache self.sources_by_key = {} self.translations = {} @languages_by_locale = nil @missing_keys_by_sources = nil end |
#source(key, locale) ⇒ Object
Returns source by key
196 197 198 199 200 201 202 203 |
# File 'lib/tml/application.rb', line 196 def source(key, locale) self.sources_by_key ||= {} source_key = "#{locale}/#{key}" self.sources_by_key[source_key] ||= Tml::Source.new( :application => self, :source => key ).fetch_translations(locale) end |
#submit_missing_keys ⇒ Object
Submit missing translation keys
255 256 257 258 259 |
# File 'lib/tml/application.rb', line 255 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 |
#supported_locale(locale) ⇒ Object
Returns supported locale or fallback locale
152 153 154 155 156 157 158 159 |
# File 'lib/tml/application.rb', line 152 def supported_locale(locale) return default_locale if locale.to_s == '' locale = Tml::Language.normalize_locale(locale) unless locales.include?(locale) locale = locale.split('-').first end locales.include?(locale) ? locale : default_locale end |
#token ⇒ Object
Returns application token
56 57 58 |
# File 'lib/tml/application.rb', line 56 def token access_token end |
#update_attributes(attrs) ⇒ Object
Updates application attributes
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/tml/application.rb', line 94 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
213 214 215 216 217 218 219 220 |
# File 'lib/tml/application.rb', line 213 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 |