class Tr8n::TranslationKey

Public Class Methods

generate_key(label, desc = '') click to toggle source
# File lib/tr8n/translation_key.rb, line 62
def self.generate_key(label, desc = '')
  "#{Digest::MD5.hexdigest("#{label};;;#{desc}")}~"[0..-2].to_s
end
new(attrs = {}) click to toggle source
Calls superclass method Tr8n::Base::new
# File lib/tr8n/translation_key.rb, line 40
def initialize(attrs = {})
  super

  self.attributes[:key] ||= self.class.generate_key(label, description)
  self.attributes[:locale] ||= Tr8n.session.block_options[:locale] || (application ? application.default_locale : Tr8n.config.default_locale)
  self.attributes[:language] ||= application ? application.language(locale) : Tr8n.config.default_language
  self.attributes[:translations] = {}

  if hash_value(attrs, :translations)
    hash_value(attrs, :translations).each do |locale, translations|
      language = application.language(locale)

      self.attributes[:translations][locale] ||= []

      translations.each do |translation_hash|
        translation = Tr8n::Translation.new(translation_hash.merge(:translation_key => self, :locale => language.locale))
        self.attributes[:translations][locale] << translation
      end
    end
  end
end
substitute_tokens(label, token_values, language, options = {}) click to toggle source

if the translations engine is disabled

# File lib/tr8n/translation_key.rb, line 161
def self.substitute_tokens(label, token_values, language, options = {})
  return label.to_s if options[:skip_substitution] 
  Tr8n::TranslationKey.new(:label => label.to_s).substitute_tokens(label.to_s, token_values, language, options)
end

Public Instance Methods

data_tokens() click to toggle source

Returns an array of data tokens from the translation key

# File lib/tr8n/translation_key.rb, line 143
def data_tokens
  @data_tokens ||= begin
    dt = Tr8n::Tokenizers::Data.new(label)
    dt.tokens
  end
end
data_tokens_names_map() click to toggle source
# File lib/tr8n/translation_key.rb, line 150
def data_tokens_names_map
  @data_tokens_names_map ||= begin
    map = {}
    data_tokens.each do |token|
      map[token.name] = token
    end
    map
  end
end
decoration_tokens() click to toggle source

Returns an array of decoration tokens from the translation key

# File lib/tr8n/translation_key.rb, line 134
def decoration_tokens
  @decoration_tokens ||= begin
    dt = Tr8n::Tokenizers::Decoration.new(label)
    dt.parse
    dt.tokens
  end
end
find_first_valid_translation(language, token_values) click to toggle source
# File lib/tr8n/translation_key.rb, line 100
def find_first_valid_translation(language, token_values)
  translations = translations_for_language(language)

  translations.sort! { |x,y| x.precedence <=> y.precedence }

  translations.each do |translation|
    return translation if translation.matches_rules?(token_values)
  end
  
  nil
end
has_translations_for_language?(language) click to toggle source
# File lib/tr8n/translation_key.rb, line 66
def has_translations_for_language?(language)
  translations and translations[language.locale] and translations[language.locale].any?
end
set_application(app) click to toggle source

switches to a new application

# File lib/tr8n/translation_key.rb, line 80
def set_application(app)
  self.application = app
  translations.values.each do |locale_translations|
    locale_translations.each do |translation|
      translation.translation_key = self
      translation.language = self.application.language(translation.locale)
    end
  end
  self
end
set_translations(locale, translations) click to toggle source
# File lib/tr8n/translation_key.rb, line 70
def set_translations(locale, translations)
  translations.each do |translation|
    translation.locale ||= locale
    translation.translation_key = self
    translation.language = self.application.language(translation.locale)
  end
  self.translations[locale] = translations
end
substitute_tokens(translated_label, token_values, language, options = {}) click to toggle source
# File lib/tr8n/translation_key.rb, line 166
def substitute_tokens(translated_label, token_values, language, options = {})
  if Tr8n::Tokenizers::Data.required?(translated_label)
    translated_label = Tr8n::Tokenizers::Data.new(translated_label, token_values, :allowed_tokens => data_tokens_names_map).substitute(language, options)
  end

  if Tr8n::Tokenizers::Decoration.required?(translated_label)
    translated_label = Tr8n::Tokenizers::Decoration.new(translated_label, token_values, :allowed_tokens => decoration_tokens).substitute
  end

  translated_label
end
translate(language, token_values = {}, options = {}) click to toggle source
# File lib/tr8n/translation_key.rb, line 112
def translate(language, token_values = {}, options = {})
  if Tr8n.config.disabled? or language.locale == self.locale
    return substitute_tokens(label, token_values, language, options.merge(:fallback => false))
  end

  translation = find_first_valid_translation(language, token_values)
  decorator = Tr8n::Decorators::Base.decorator

  if translation
    translated_label = substitute_tokens(translation.label, token_values, translation.language, options)
    return decorator.decorate(translated_label, translation.language, language, self, options)
  end

  translated_label = substitute_tokens(label, token_values, self.language, options)
  decorator.decorate(translated_label, self.language, language, self, options)
end
translations_for_language(language) click to toggle source

Translation Methods

# File lib/tr8n/translation_key.rb, line 95
def translations_for_language(language)
  return [] unless self.translations
  self.translations[language.locale] || []
end