class CrystalI18n::I18n

Overview

Backend for the crystal-i18n format. This class contains methods to parse and interact with them

Note that this is still experimental, mainly in regards to plural-forms. Other than that, it should be fully usable and accurate.

EXPERIMENTAL

Defined in:

backend/crystal-i18n/crystal-i18n.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(locale_directory_path : String, reference_locale : String = "en") #

Creates a new crystal-i18n instance that reads from the given locale directory path.

CrystalI18n::I18n.new("locales")

[View source]

Instance Method Detail

def create #

Returns self | Here for compatibility with Gettext::MOBackend and Gettext::POBackend

catalogue = CrystalI18n::I18n.new("locales") catalogue == catalogue.create() # => true


[View source]
def define_rule(locale : String, value : Int32 | Int64 | Float64 -> String) #

Set pluralization rules for the given locale. See CrystalI18n.define_rule for more information


[View source]
def plural_rules : Hash(String, Int32 | Int64 | Float64 -> String) #

Returns all defined CLDR plural rules.


[View source]
def select(locale) #

Selects a locale to use for translations

Raises a KeyError when the selected locale isn't found.

If your application is monolingual then this along with #translate(key, count, iter) is your friend. However, if you need to switch languages on the fly then this method should be ignored.

Instead, you should use the #translate(locale, key, count, iter) overload, which allows for fetching messages from any locales without changing the state of the instance.

catalogue = CrystalI18n::I18n.new("locales")

catalogue.select("en")
catalogue.translate("translation") # => "Translated Message"

catalogue.select("example")
catalogue.translate("translation") # => "Some message in another language"

catalogue.select("doesn'texist")
catalogue.translate("translation") # => "Some message in another language"

[View source]
def translate(key : String, count : Int | Float? = nil, iter : Int? = nil, **kwargs) #

Fetches a translation from the selected locale with the given path (key).

Functionality is the same as CrystalI18n::I18n.translate(locale : String, key : String, count : Int | Float? = nil, iter : Int? = nil) but with the first argument removed


[View source]
def translate(locale : String, key : String, count : Int | Float? = nil, iter : Int? = nil, **kwargs) #

Fetches a translation from the given locale with the given path (key).

Basic usage is this:

catalogue = CrystalI18n::I18n.new("locales")
catalogue.translate("en", "translation") # => "Translated Message"

This method can also translate plural-forms through the count argument.

catalogue.translate("en", "possessions.fruits.apples", 50) # => "I have 50 apples"
catalogue.translate("en", "possessions.fruits.apples", 1)  # => "I have 1 apple"

Interpolation can be done through kwargs.

# message is 'Hello there, my name is %{name} and I'm a %{profession}`.
result = catalogue.translate("en", "introduction.messages", name: "Steve", profession: "programmer")
result # => "Hello there, my name is Steve and I'm a programmer"

If the value at the given path (key) turns out to be an array then you can pass in the iter argument to select a specific value at the given index

catalogue.translate("en", "items.foods", iter: 2) # => "Hamburger"

When a translation is not found LensExceptions::MissingTranslation would be raised.


[View source]