Module: Olelo::Locale

Defined in:
lib/olelo/locale.rb

Overview

Simple localization implementation

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.localeObject

Returns the value of attribute locale.



8
9
10
# File 'lib/olelo/locale.rb', line 8

def locale
  @locale
end

Class Method Details

.add(locale) ⇒ void

This method returns an undefined value.

Add locale hash

A locale is a hash which maps keys to strings.

Parameters:

  • Locale (Hash)

    hash



18
19
20
21
22
# File 'lib/olelo/locale.rb', line 18

def add(locale)
  @translations.update(locale[$1] || {}) if @locale =~ /^(\w+)(_|-)/
  @translations.update(locale[@locale] || {})
  @translations.each_value(&:freeze)
end

.translate(key, args = {}) ⇒ String

Return translated string for key

A translated string can contain variables which are substituted in this method. You have to pass an arguments hash.

Parameters:

  • key (Symbol, String)

    which identifies string in locale

  • args (Hash) (defaults to: {})

    Arguments hash for string interpolation

Options Hash (args):

  • :count (Integer)

    if count is not 1, the key #key_plural is looked up instead

  • :fallback (String)

    Fallback string if key is not found in the locale

Returns:

  • (String)

    translated string



35
36
37
38
39
40
41
# File 'lib/olelo/locale.rb', line 35

def translate(key, args = {})
  if @translations[key]
    @translations[key] % args
  else
    args[:fallback] || "##{key}"
  end
end