class Tr8n::Language
Public Class Methods
cache_key(locale)
click to toggle source
# File lib/tr8n/language.rb, line 38 def self.cache_key(locale) File.join(locale, 'language') end
Public Instance Methods
align(dest)
click to toggle source
# File lib/tr8n/language.rb, line 92 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/tr8n/language.rb, line 75 def case_by_keyword(keyword) cases[keyword] end
context_by_keyword(keyword)
click to toggle source
# File lib/tr8n/language.rb, line 67 def context_by_keyword(keyword) hash_value(contexts, keyword) end
context_by_token_name(token_name)
click to toggle source
# File lib/tr8n/language.rb, line 71 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/tr8n/language.rb, line 102 def current_source(options) (options[:source] || Tr8n.session.block_options[:source] || Tr8n.session.current_source || 'undefined').to_s end
default?()
click to toggle source
# File lib/tr8n/language.rb, line 83 def default? return true unless application application.default_locale == locale end
dir()
click to toggle source
# File lib/tr8n/language.rb, line 88 def dir right_to_left? ? 'rtl' : 'ltr' end
fetch()
click to toggle source
# File lib/tr8n/language.rb, line 42 def fetch update_attributes(application.api_client.get("language/#{locale}", {}, {:cache_key => self.class.cache_key(locale)})) rescue Tr8n::Exception => ex Tr8n.logger.error("Failed to load language: #{ex}") self end
full_name()
click to toggle source
# File lib/tr8n/language.rb, line 97 def full_name return english_name if english_name == native_name "#{english_name} - #{native_name}" end
has_definition?()
click to toggle source
# File lib/tr8n/language.rb, line 79 def has_definition? contexts.any? end
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/tr8n/language.rb, line 123 def translate(label, description = nil, tokens = {}, options = {}) params = Tr8n::Utils.normalize_tr_params(label, description, tokens, options) return params[:label] if params[:label].tr8n_translated? translation_key = Tr8n::TranslationKey.new({ :application => application, :label => params[:label], :description => params[:description], :locale => hash_value(params[:options], :locale) || hash_value(Tr8n.session.block_options, :locale) || Tr8n.config.default_locale, :level => hash_value(params[:options], :level) || hash_value(Tr8n.session.block_options, :level) || Tr8n.config.default_level, :translations => [] }) #Tr8n.logger.info("Translating " + params[:label] + " from: " + translation_key.locale.inspect + " to " + locale.inspect) params[:tokens] ||= {} params[:tokens][:viewing_user] ||= Tr8n.session.current_user if Tr8n.config.disabled? or self.locale == translation_key.locale return translation_key.substitute_tokens( params[:label], params[:tokens], self, params[:options] ).tr8n_translated end 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]).tr8n_translated end source_key = current_source(options) if Tr8n.cache.segmented? or Tr8n.session.inline_mode? source = application.source(source_key, locale) cached_translations = source.cached_translations(locale, translation_key.key) else cached_translations = application.fetch_translations(locale)[translation_key.key] end if cached_translations translation_key.set_translations(locale, cached_translations) else application.register_missing_key(source_key, translation_key) end translation_key.translate(self, params[:tokens], params[:options]).tr8n_translated end
Also aliased as: tr
update_attributes(attrs)
click to toggle source
Calls superclass method
Tr8n::Base#update_attributes
# File lib/tr8n/language.rb, line 49 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] = Tr8n::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] = Tr8n::LanguageCase.new(lcase.merge(:keyword => key, :language => self)) end end end