abstract class I18n::Backend

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.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.quantity_key_procs : Hash(String, QuantityKeyProc) #

A list of QuantityKeyProcs 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 }})

[View source]

Instance Method Detail

def data : JSON::Any | YAML::Any | Nil #

Storage for translations


[View source]
abstract def load #

Loads data from #load_paths.


[View source]
def load_paths : Array(String) #

Where to load locale files from.

backend.load_paths << Dir.current + "/locales"

[View source]
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"

[View source]