Class: Thailand::I18n::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/thailand/i18n.rb

Overview

A simple object to handle I18n translation in simple situations.

Constant Summary collapse

DEFAULT_LOCALE =
'en'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*initial_locale_paths) ⇒ Simple

Returns a new instance of Simple.



13
14
15
16
17
18
19
20
# File 'lib/thailand/i18n.rb', line 13

def initialize(*initial_locale_paths)
  self.locale = DEFAULT_LOCALE
  @fallback_locale = DEFAULT_LOCALE
  @locale_paths = []
  initial_locale_paths.each do |path|
    append_locale_path(path)
  end
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



10
11
12
# File 'lib/thailand/i18n.rb', line 10

def cache
  @cache
end

#fallback_localeObject (readonly)

Returns the value of attribute fallback_locale.



11
12
13
# File 'lib/thailand/i18n.rb', line 11

def fallback_locale
  @fallback_locale
end

#locale_pathsObject (readonly)

Returns the value of attribute locale_paths.



11
12
13
# File 'lib/thailand/i18n.rb', line 11

def locale_paths
  @locale_paths
end

Instance Method Details

#append_locale_path(path) ⇒ Object



22
23
24
25
# File 'lib/thailand/i18n.rb', line 22

def append_locale_path(path)
  reset!
  @locale_paths << Pathname.new(path)
end

#available_localesObject



56
57
58
59
# File 'lib/thailand/i18n.rb', line 56

def available_locales
  load_cache_if_needed
  @cache.keys.sort
end

#inspectObject



52
53
54
# File 'lib/thailand/i18n.rb', line 52

def inspect
  "<##{self.class} locale=#{locale}>"
end

#localeObject



32
33
34
# File 'lib/thailand/i18n.rb', line 32

def locale
  Thread.current[:locale]
end

#locale=(locale) ⇒ Object

Set a new locale



28
29
30
# File 'lib/thailand/i18n.rb', line 28

def locale=(locale)
  Thread.current[:locale] = locale.to_s
end

#reset!Object

Clear the cache. Should be called after appending a new locale path manually (in case lookups have already occurred.)

When adding a locale path, it’s best to use #append_locale_path, which resets the cache automatically.



48
49
50
# File 'lib/thailand/i18n.rb', line 48

def reset!
  @cache = nil
end

#translate(key) ⇒ Object Also known as: t

Retrieve a translation for a key in the following format: ‘a.b.c’



37
38
39
# File 'lib/thailand/i18n.rb', line 37

def translate(key)
  read(key.to_s)
end