class Tml::Application

Constants

API_HOST
CDN_HOST

Public Class Methods

cache_key() click to toggle source

Returns application cache key

# File lib/tml/application.rb, line 46
def self.cache_key
  'application'
end
translations_cache_key(locale) click to toggle source

Returns translations cache key

# File lib/tml/application.rb, line 51
def self.translations_cache_key(locale)
  "#{locale}/translations"
end

Public Instance Methods

add_language(new_language) click to toggle source

Adds a language to the application

# 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?() click to toggle source

checks if key registration is allowed currently it is based on user's inline mode

# File lib/tml/application.rb, line 207
def allow_key_registration?
  return if token.nil?
  Tml.session.inline_mode?
end
api_client() click to toggle source

Create API client

# 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) click to toggle source

Cache translations within application object

# 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) click to toggle source

Get application cached translations

# 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() click to toggle source

CDN host

Calls superclass method
# File lib/tml/application.rb, line 66
def cdn_host
  super || CDN_HOST
end
current_language(locale) click to toggle source

Normalizes and returns current language if locale is passed as nil, default locale will be used

# 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() click to toggle source

Debug translations

# 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) click to toggle source

Get default data token

# File lib/tml/application.rb, line 349
def default_data_token(token)
  hash_value(tokens, "data.#{token.to_s}")
end
default_decoration_token(token) click to toggle source

Get default decoration token

# File lib/tml/application.rb, line 344
def default_decoration_token(token)
  hash_value(tokens, "decoration.#{token.to_s}")
end
default_locale() click to toggle source

Application or configuration default locale

# File lib/tml/application.rb, line 147
def default_locale
  self.attributes[:default_locale] || Tml.config.default_locale
end
feature_enabled?(key) click to toggle source

Check if feature is enabled

# File lib/tml/application.rb, line 354
def feature_enabled?(key)
  hash_value(features, key.to_s)
end
fetch() click to toggle source

Fetches application definition from the service

# 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) click to toggle source

Fetch translations from API

# 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() click to toggle source

API host

Calls superclass method
# File lib/tml/application.rb, line 61
def host
  super || API_HOST
end
ignored_key?(key) click to toggle source

Check if a key is ignored

# 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) click to toggle source

Returns language by locale

# 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) click to toggle source

Loads application extensions, if any

# 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() click to toggle source

Returns a list of application supported locales

# File lib/tml/application.rb, line 142
def locales
  @locales ||= languages.collect{|lang| lang.locale}
end
register_keys(keys) click to toggle source

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

# 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) click to toggle source

Register missing keys

# 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() click to toggle source

Reset translation cache

# 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) click to toggle source

Returns source by key

# 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() click to toggle source

Submit missing translation keys

# 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) click to toggle source

Returns supported locale or fallback locale

# 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() click to toggle source

Returns application token

# File lib/tml/application.rb, line 56
def token
  access_token
end
update_attributes(attrs) click to toggle source

Updates application attributes

Calls superclass method Tml::Base#update_attributes
# 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) click to toggle source

Verify current source path

# 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