Class: G11n::Translator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/g11n/translator.rb

Overview

Manages the configuration of the G11n library

Instance Method Summary collapse

Constructor Details

#initializeTranslator

Returns a new instance of Translator.



6
7
8
# File 'lib/g11n/translator.rb', line 6

def initialize
  reset
end

Instance Method Details

#dictionary_for(the_locale) ⇒ Object

Looks and returns the right dictionary for the given locale



37
38
39
40
41
42
43
44
45
46
# File 'lib/g11n/translator.rb', line 37

def dictionary_for the_locale
  return @dictionaries[the_locale] if @dictionaries.has_key? the_locale # In case is already loaded
  if File.exists? "#{@translations_path}#{the_locale}.yaml" 
    @dictionaries[the_locale] = SymbolMatrix.new "#{@translations_path}#{the_locale}.yaml"
  elsif File.exists? "#{@translations_path}#{the_locale}.yml"
    @dictionaries[the_locale] = SymbolMatrix.new "#{@translations_path}#{the_locale}.yml"
  else
    raise NoTranslationAvailable, "There is no translation file available for the '#{the_locale}' locale, check the tranlations source directory configuration"
  end
end

#localeSymbol

Returns the current locale.

Returns:

  • (Symbol)

    the current locale



19
20
21
# File 'lib/g11n/translator.rb', line 19

def locale
  @locale
end

#locale=(the_locale) ⇒ Object

Sets the locale. Fails if the locale matches no available file



11
12
13
14
15
16
# File 'lib/g11n/translator.rb', line 11

def locale= the_locale
  unless locale_file_exists_for? the_locale
    raise NoTranslationAvailable, "There is no translation file available for the '#{the_locale}' locale, check the tranlations source directory configuration"
  end      
  @locale = the_locale
end

#translateObject

Forwards the method call to the right “dictionary” (a SymbolTable object) mjijackson.com/2010/04/config-revisited What it truely does the #translate method is to retrieve the right SymbolTable object from the dictionary



32
33
34
# File 'lib/g11n/translator.rb', line 32

def translate
  dictionary_for @locale
end

#translations_path=(the_path) ⇒ Object

Sets the path where translations will be looked for



24
25
26
# File 'lib/g11n/translator.rb', line 24

def translations_path= the_path
  @translations_path = the_path
end