class Tr8n::Application

Public Class Methods

cache_key() click to toggle source
# File lib/tr8n/application.rb, line 39
def self.cache_key
  'application'
end
translations_cache_key(locale) click to toggle source
# File lib/tr8n/application.rb, line 43
def self.translations_cache_key(locale)
  "#{locale}/translations"
end

Public Instance Methods

add_language(new_language) click to toggle source

Mostly used for testing

# File lib/tr8n/application.rb, line 85
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() click to toggle source
# File lib/tr8n/application.rb, line 246
def api_client
  @api_client ||= Tr8n::Api::Client.new(:application => self)
end
cache_translations(locale, key, new_translations) click to toggle source
# File lib/tr8n/application.rb, line 206
def cache_translations(locale, key, new_translations)
  self.translations ||= {}
  self.translations[locale] ||= {}
  self.translations[locale][key] = new_translations.collect do |t|
    Tr8n::Translation.new(
      :locale => t['locale'] || locale,
      :label => t['label'],
      :context => t['context']
    )
  end
end
cached_translations(locale, key) click to toggle source
# File lib/tr8n/application.rb, line 218
def cached_translations(locale, key)
  return unless self.translations and self.translations[locale]
  self.translations[locale][key]
end
component(key, register = true) click to toggle source
# File lib/tr8n/application.rb, line 114
def component(key, register = true)
  key = key.key if key.is_a?(Tr8n::Component)

  return self.components[key] if self.components[key]
  return nil unless register

  self.components[key] ||= api_client.post('components/register', {:component => key}, {:class => Tr8n::Component, :attributes => {:application => self}})
end
debug_translations() click to toggle source
# File lib/tr8n/application.rb, line 223
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
# File lib/tr8n/application.rb, line 238
def default_data_token(token)
  hash_value(tokens, "data.#{token.to_s}")
end
default_decoration_token(token) click to toggle source
# File lib/tr8n/application.rb, line 234
def default_decoration_token(token)
  hash_value(tokens, "decoration.#{token.to_s}")
end
feature_enabled?(key) click to toggle source
# File lib/tr8n/application.rb, line 242
def feature_enabled?(key)
  hash_value(features, key.to_s)
end
fetch() click to toggle source
# File lib/tr8n/application.rb, line 47
def fetch
  update_attributes(api_client.get('applications/current', {:definition => true}, {:cache_key => self.class.cache_key}))
rescue Tr8n::Exception => ex
  Tr8n.logger.error("Failed to load application: #{ex}")
  self
end
fetch_translations(locale) click to toggle source
# File lib/tr8n/application.rb, line 176
def fetch_translations(locale)
  self.translations ||= {}
  self.translations[locale] ||= begin
    results = Tr8n.cache.fetch(Tr8n::Application.translations_cache_key(locale)) do
      data = {}
      unless Tr8n.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|
        Tr8n::Translation.new(
          :locale => t['locale'] || locale,
          :label => t['label'],
          :context => t['context']
        )
      end
    end
    translations_by_key
  end
rescue Tr8n::Exception => ex
  {}
end
language(locale = nil) click to toggle source
# File lib/tr8n/application.rb, line 65
def language(locale = nil)
  locale = nil if locale.strip == ''
  locale ||= default_locale || Tr8n.config.default_locale
  @languages_by_locale ||= {}
  @languages_by_locale[locale] ||= api_client.get(
    "languages/#{locale}",
    {:definition => true},
    {
      :class => Tr8n::Language,
      :attributes => {:locale => locale, :application => self},
      :cache_key => Tr8n::Language.cache_key(locale)
    }
  )
rescue Tr8n::Exception => e
  Tr8n.logger.error(e)
  Tr8n.logger.error(e.backtrace)
  @languages_by_locale[locale] = Tr8n.config.default_language
end
locales() click to toggle source
# File lib/tr8n/application.rb, line 94
def locales
  @locales ||= languages.collect{|lang| lang.locale}
end
postoffice() click to toggle source
# File lib/tr8n/application.rb, line 250
def postoffice
  @postoffice ||= Tr8n::Api::PostOffice.new(:application => self)
end
register_keys(keys) click to toggle source
# File lib/tr8n/application.rb, line 132
def register_keys(keys)
  params = []
  keys.each do |source_key, keys|
    next unless keys.values.any?
    source = Tr8n::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 Tr8n::Exception => e
  Tr8n.logger.error('Failed to register missing translation keys...')
  Tr8n.logger.error(e)
  Tr8n.logger.error(e.backtrace)
end
register_missing_key(source_key, tkey) click to toggle source
# File lib/tr8n/application.rb, line 123
def register_missing_key(source_key, tkey)
  return if Tr8n.cache.read_only? and not Tr8n.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 Tr8n.config.submit_missing_keys_realtime
end
reset_translation_cache() click to toggle source
# File lib/tr8n/application.rb, line 169
def reset_translation_cache
  self.sources = {}
  self.translations = {}
  @languages_by_locale      = nil
  @missing_keys_by_sources  = nil
end
source(source, locale) click to toggle source
# File lib/tr8n/application.rb, line 106
def source(source, locale)
  self.sources ||= {}
  self.sources[source] ||= Tr8n::Source.new(
    :application => self,
    :source => source
  ).fetch_translations(locale)
end
submit_missing_keys() click to toggle source
# File lib/tr8n/application.rb, line 148
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
tools() click to toggle source
# File lib/tr8n/application.rb, line 98
def tools
  @attributes[:tools] || {}
end
translators() click to toggle source
# File lib/tr8n/application.rb, line 163
def translators
  @translators ||= api_client.get('applications/current/translators', {}, {:class => Tr8n::Translator, :attributes => {:application => self}})
rescue
  []
end
update_attributes(attrs) click to toggle source
Calls superclass method Tr8n::Base#update_attributes
# File lib/tr8n/application.rb, line 54
def update_attributes(attrs)
  super

  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
end
url_for(path) click to toggle source
# File lib/tr8n/application.rb, line 102
def url_for(path)
  "#{tools['assets']}#{path}"
end