class Tml::Language

Public Class Methods

cache_key(locale) click to toggle source

Returns language cache key

# File lib/tml/language.rb, line 39
def self.cache_key(locale)
  File.join(locale, 'language')
end
normalize_locale(locale) click to toggle source

Supported locales must be of a form en, en-US, az-Cyrl-AZ

# File lib/tml/language.rb, line 44
def self.normalize_locale(locale)
  parts = locale.split(/[-_]/)
  return parts.first.downcase if parts.size == 1
  return [parts.first.downcase, parts.last.upcase].join('-') if parts.size == 2
  ([parts.first.downcase] + parts[1..-2].collect{|part| part.downcase.capitalize} + [parts.first.upcase]).join('-')
end

Public Instance Methods

align(dest) click to toggle source
# File lib/tml/language.rb, line 110
def align(dest)
  return dest unless right_to_left?
  dest.to_s == 'left' ? 'right' : 'left'
end
case_by_keyword(keyword) click to toggle source
# File lib/tml/language.rb, line 93
def case_by_keyword(keyword)
  cases[keyword]
end
context_by_keyword(keyword) click to toggle source

Returns context by keyword

# File lib/tml/language.rb, line 84
def context_by_keyword(keyword)
  hash_value(contexts, keyword)
end
context_by_token_name(token_name) click to toggle source

Returns context by token name

# File lib/tml/language.rb, line 89
def context_by_token_name(token_name)
  contexts.values.detect{|ctx| ctx.applies_to_token?(token_name)}
end
current_source(options) click to toggle source
# File lib/tml/language.rb, line 120
def current_source(options)
  return options[:source] if options and options[:source]
  (Tml.session.block_option(:source) || Tml.session.current_source || 'undefined').to_s
end
default?() click to toggle source
# File lib/tml/language.rb, line 101
def default?
  return true unless application
  application.default_locale == locale
end
dir() click to toggle source
# File lib/tml/language.rb, line 106
def dir
  right_to_left? ? 'rtl' : 'ltr'
end
fetch() click to toggle source

Loads language definition from the service

# File lib/tml/language.rb, line 52
def fetch
  update_attributes(application.api_client.get(
    "language/#{locale}/definition", {},
    {
        cache_key: self.class.cache_key(locale)
    }
  ))
rescue Tml::Exception => ex
  Tml.logger.error("Failed to load language: #{ex}")
  self
end
full_name() click to toggle source
# File lib/tml/language.rb, line 115
def full_name
  return english_name if english_name == native_name
  "#{english_name} - #{native_name}"
end
has_definition?() click to toggle source
# File lib/tml/language.rb, line 97
def has_definition?
  contexts.any?
end
source_path() click to toggle source

build source path to the block

# File lib/tml/language.rb, line 225
def source_path
  sp = []

  Tml.session.block_options_queue.each do |opts|
    next unless hash_value(opts, :source)
    sp << hash_value(opts, :source)
  end

  sp = sp.reverse
  sp.unshift(Tml.session.current_source)

  sp.join(Tml.config.source_separator)
end
tr(label, description = nil, tokens = {}, options = {})
Alias for: translate
translate(label, description = nil, tokens = {}, options = {}) click to toggle source

Translation Methods

Note - when inline translation mode is enable, cache will not be used and translators will always hit the live service to get the most recent translations

Some cache adapters cache by source, others by key. Some are read-only, some are built on the fly.

There are three ways to call the tr method

tr(label, description = “”, tokens = {}, options = {})

or

tr(label, tokens = {}, options = {})

or

tr(:label => label, :description => “”, :tokens => {}, :options => {})

# File lib/tml/language.rb, line 141
def translate(label, description = nil, tokens = {}, options = {})
  params = Tml::Utils.normalize_tr_params(label, description, tokens, options)
  return params[:label] if params[:label].to_s.strip == '' or params[:label].index('tml:label')
  return params[:label] if params[:label].tml_translated?

  translation_key = Tml::TranslationKey.new({
    :application  => application,
    :label        => params[:label],
    :description  => params[:description],
    :locale       => hash_value(params[:options], :locale) || Tml.session.block_option(:locale) || Tml.config.default_locale,
    :level        => hash_value(params[:options], :level) || Tml.session.block_option(:level) || Tml.config.default_level,
    :syntax       => hash_value(params[:options], :syntax),
    :translations => []
  })

  # pp "Translating #{params[:label]} from: #{translation_key.locale.inspect} to #{locale.inspect} with syntax #{translation_key.syntax}"
  # Tml.logger.info("Translating #{params[:label]} from: #{translation_key.locale.inspect} to #{locale.inspect}")

  params[:tokens] ||= {}
  if params[:tokens].is_a?(Hash)
    params[:tokens][:viewing_user] ||= Tml.session.current_user
  end

  if Tml.config.disabled? or application.nil?
    return translation_key.substitute_tokens(params[:label], params[:tokens], self, params[:options]).tml_translated
  end

  # check if key was ignored on the application level
  if application.ignored_key?(translation_key.key)
    return translation_key.substitute_tokens(params[:label], params[:tokens], self, params[:options]).tml_translated
  end

  # if translations have already been cached in the application, use them
  cached_translations = application.cached_translations(locale, translation_key.key)
  if cached_translations
    translation_key.set_translations(locale, cached_translations)
    return translation_key.translate(self, params[:tokens], params[:options]).tml_translated
  end

  # each key translations will be loaded directly from the API, and registered against "manual" source
  if Tml.session.block_option(:by_key)
    application.register_missing_key(:manual, translation_key)
    translation_key.fetch_translations(locale)
    return translation_key.translate(self, params[:tokens], params[:options]).tml_translated
  end

  # fetch translations grouped by source
  source_key = current_source(params[:options])
  current_source_path = source_path

  # Dynamic sources are never registered under the parent source for fast retrieval
  if Tml.session.block_option(:dynamic)
    current_source_path = source_key
  else
    application.verify_source_path(source_key, current_source_path)
  end

  # Tml.logger.debug("#{self.locale} :  #{params[:label]} : #{source_key}")

  source = application.source(source_key, locale)

  # check if a key was ignored on the source level
  if source.ignored_key?(translation_key.key)
    return translation_key.substitute_tokens(params[:label], params[:tokens], self, params[:options]).tml_translated
  end

  cached_translations = source.cached_translations(locale, translation_key.key)

  if cached_translations
    translation_key.set_translations(locale, cached_translations)
  else
    params[:options] ||= {}
    params[:options][:pending] = true
    application.register_missing_key(current_source_path, translation_key)
  end

  translation_key.translate(self, params[:tokens], params[:options]).tml_translated
rescue Exception => ex
  pp ex, ex.backtrace
  return params[:label]
end
Also aliased as: tr
update_attributes(attrs) click to toggle source

update language attributes

Calls superclass method Tml::Base#update_attributes
# File lib/tml/language.rb, line 65
def update_attributes(attrs)
  super

  self.attributes[:contexts] = {}
  if hash_value(attrs, :contexts)
    hash_value(attrs, :contexts).each do |key, context|
      self.attributes[:contexts][key] = Tml::LanguageContext.new(context.merge(:keyword => key, :language => self))
    end
  end

  self.attributes[:cases] = {}
  if hash_value(attrs, :cases)
    hash_value(attrs, :cases).each do |key, lcase|
      self.attributes[:cases][key] = Tml::LanguageCase.new(lcase.merge(:keyword => key, :language => self))
    end
  end
end