Class: SimpleI18n::Language

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_i18n/language.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(abbr, full_name) ⇒ Language

Returns a new instance of Language.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/simple_i18n/language.rb', line 13

def initialize(abbr, full_name)
  @translations = {}
  @abbreviation = abbr
  @full_name = full_name

  self.class.table ||= {}
  self.class.default ||= self
  self.class.current ||= self

  if self.class.table.has_key?(abbr)
    warn 'Redefining %s language.' % abbr
  end

  self.class.table[abbr] = self
end

Class Attribute Details

.currentObject

Returns the value of attribute current.



6
7
8
# File 'lib/simple_i18n/language.rb', line 6

def current
  @current
end

.defaultObject

Returns the value of attribute default.



6
7
8
# File 'lib/simple_i18n/language.rb', line 6

def default
  @default
end

.tableObject

Returns the value of attribute table.



7
8
9
# File 'lib/simple_i18n/language.rb', line 7

def table
  @table
end

Instance Attribute Details

#abbreviationObject (readonly)

Returns the value of attribute abbreviation.



10
11
12
# File 'lib/simple_i18n/language.rb', line 10

def abbreviation
  @abbreviation
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



10
11
12
# File 'lib/simple_i18n/language.rb', line 10

def full_name
  @full_name
end

#translationsObject (readonly)

Returns the value of attribute translations.



11
12
13
# File 'lib/simple_i18n/language.rb', line 11

def translations
  @translations
end

Instance Method Details

#[](token) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/simple_i18n/language.rb', line 33

def [](token)
  tran = @translations[token]
  return tran if tran

  default_lang = self.class.default

  if default_lang == self or (not default_lang.token_defined? token)
    warn 'No token [%s] defined.' % token
    return token
  end

  warn 'No token [%s] defined in %s, take from %s.' % \
       [token, @abbreviation, default_lang.abbreviation]
  return default_lang[token]
end

#[]=(token, translation) ⇒ Object



49
50
51
52
53
54
# File 'lib/simple_i18n/language.rb', line 49

def []=(token, translation)
  if translations.has_key?(token)
    warn 'Token [%s] redefined in language %s.' % [token, @abbreviation]
  end
  @translations[token] = translation
end

#token_defined?(token_name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/simple_i18n/language.rb', line 29

def token_defined?(token_name)
  return @translations.has_key?(token_name)
end