CrystalI18n::I18n
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.
Constructors
Instance methods
Returns self | Here for compatibility with Gettext::MOBackend and Gettext::POBackend
catalogue = CrystalI18n::I18n.new("locales") catalogue == catalogue.create() # => true
Set pluralization rules for the given locale.
See CrystalI18n.define_rule for more information
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"
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
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.