Interface to translations of human text used in a project.
Methods
- #
-
- L
-
- M
-
- N
-
- P
-
Attributes
[R]
|
locale |
The locale into which the #[] method will translate phrases.
|
Class Public methods
Source: show
| on GitHub
def initialize project_install_dir
lang_dir = File.join(project_install_dir, 'lang')
@phrases_by_language = Hash.new do |cache, language|
phrases = Hash.new {|h,k| h[k] = nil }
lang_file = File.join(lang_dir, "#{language}.yaml")
lang_data = YAML.load_file(lang_file) rescue nil
phrases.merge! lang_data if lang_data
cache[language] = phrases
end
self.locale = ENV['LC_ALL'] || ENV['LC_MESSAGES'] || ENV['LANG']
end
Instance Public methods
Translates the given phrase into the target locale (see locale and locale=) and then substitutes the given
placeholder arguments into the translation (see Kernel#sprintf).
If a translation is not available for the given phrase, then the given
phrase will be used as-is, untranslated.
Source: show
| on GitHub
def [] phrase, *words
translate @language, phrase, *words
end
Source: show
| on GitHub
def locale= locale
@locale = locale.to_s
language = @locale[/^[[:alpha:]]+/].to_s
@language = language =~ /^(C|POSIX)?$/i ? :en : language.downcase.to_sym
end
method_missing(meth, *args)
Provides access to translations in any language, regardless of the target
locale (see locale and locale=).
For example, you can access Japanese translations via the jp method even if
the target locale is French.
Source: show
| on GitHub
def method_missing meth, *args
if meth.to_s =~ /^[a-z]{2,3}$/
translate meth, *args
else
super
end
end
Returns all phrases that underwent (or attempted) translation via this
object.
Source: show
| on GitHub
def phrases
@phrases_by_language.values.map {|h| h.keys }.flatten.uniq.sort
end