Class: Carmen::I18n::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/carmen/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.



16
17
18
19
20
21
22
23
# File 'lib/carmen/i18n.rb', line 16

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.



12
13
14
# File 'lib/carmen/i18n.rb', line 12

def cache
  @cache
end

#fallback_localeObject (readonly)

Returns the value of attribute fallback_locale.



13
14
15
# File 'lib/carmen/i18n.rb', line 13

def fallback_locale
  @fallback_locale
end

#locale_pathsObject (readonly)

Returns the value of attribute locale_paths.



14
15
16
# File 'lib/carmen/i18n.rb', line 14

def locale_paths
  @locale_paths
end

Instance Method Details

#append_locale_path(path) ⇒ Object



25
26
27
28
# File 'lib/carmen/i18n.rb', line 25

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

#available_localesObject



64
65
66
67
# File 'lib/carmen/i18n.rb', line 64

def available_locales
  load_cache_if_needed
  @cache.keys.sort
end

#inspectObject



60
61
62
# File 'lib/carmen/i18n.rb', line 60

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

#localeObject



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

def locale
  Thread.current[:carmen_locale]
end

#locale=(locale) ⇒ Object

Set a new locale

Calling this method will clear the cache.



33
34
35
# File 'lib/carmen/i18n.rb', line 33

def locale=(locale)
  Thread.current[:carmen_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.



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

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’

This will attempt to find the key in the current locale, and if nothing is found, a value found in the fallback locale will be used instead.



45
46
47
# File 'lib/carmen/i18n.rb', line 45

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