abstract class I18n::Backend
- I18n::Backend
- Reference
- Object
Overview
Backend is a storage for translations. All translations are stored in #data
.
backend = I18n::Backends::YAML.new
backend.load_paths << Dir.current + "/locales"
backend.load
backend.translate(["hello", "world"], "en") # => "Hello world!"
Direct Known Subclasses
Defined in:
i18n/backend.crClass Method Summary
-
.quantity_key_procs : Hash(String, QuantityKeyProc)
A list of
QuantityKeyProc
s associated with their locales.
Instance Method Summary
-
#data : JSON::Any | YAML::Any | Nil
Storage for translations
-
#load
Loads data from
#load_paths
. -
#load_paths : Array(String)
Where to load locale files from.
-
#translate(keys : Array(String), locale : String, count : Int32 | Nil = nil)
Find a translation by keys and locale.
Class Method Detail
A list of QuantityKeyProc
s associated with their locales.
Defaults with "en"
key:
"en" => QuantityKeyProc.new do |count|
if count == 0
"zero"
elsif count == 1
"one"
else
"other"
end
end
Extend it with your own locales if you need:
I18n::Backend.quantity_key_procs.merge!({"ru" => QuantityKeyProc.new { |count| your_code }})
Instance Method Detail
def load_paths : Array(String)
#
Where to load locale files from.
backend.load_paths << Dir.current + "/locales"
def translate(keys : Array(String), locale : String, count : Int32 | Nil = nil)
#
Find a translation by keys and locale.
locale is prepended to the list of keys when searching for a translation.
# Searches for "en => hello => world"
backend.translate(["hello", "world"], "en") # => "Hello world!"
# Searches for "en => apples => other"
backend.translate(["apples"], "en", count: 3) # => "3 apples"