Internationalization (I18n)

Crystal I18n is an internationalization library for the Crystal programming language a unified interface allowing to leverage translations and localized contents in a Crystal project.


Simply add the following entry to your project's shard.yml:

    github: crystal-i18n/i18

And run shards install afterwards.

Quick usage

Assuming that a config/locales relative folder exists in your project, with the following en.yml file in it:

    translation: "This is a simple translation"
    interpolation: "Hello, %{name}!"
      one: "One item"
      other: "%{count} items"

The following setup could be performed in order to initialize I18n properly:

require "i18n"

I18n.config.loaders <<"config/locales")
I18n.config.default_locale = :en

Here a translation loader is configured to load the previous translation file while also configuring the default locale (en) and initializing the I18n module.

Translations lookups can now be performed using the #translate method (or the shorter version #t) as follows:

I18n.t("simple.translation")                     # outputs "This is a simple translation"
I18n.t("simple.interpolation", name: "John Doe") # outputs "Hello, John Doe!"
I18n.t("simple.pluralization", count: 42)        # outputs "42 items"

Read More About the I18n Shard

Last updated