class

Azu::Helpers::I18n

Inherits Reference < Object

Internationalization (i18n) system for template translations.

I18n provides a simple and flexible way to internationalize your Azu application with support for:

  • YAML and JSON translation files
  • Interpolation with named parameters
  • Pluralization rules
  • Date and number localization
  • Locale detection from requests

Setup

  1. Create locale files in your locales directory:
# locales/en.yml
en:
  welcome:
    title: "Welcome!"
    greeting: "Hello, %{name}!"
  users:
    count:
      zero: "No users"
      one: "1 user"
      other: "%{count} users"
  1. Configure I18n in your application:
Azu::Helpers::I18n.configure do |config|
  config.load_path = ["locales"]
  config.default_locale = "en"
  config.available_locales = ["en", "es", "fr"]
end

Template Usage

{{ t("welcome.title") }}
{{ t("welcome.greeting", name=user.name) }}
{{ t("users.count", count=users.size) }}

Constants

LOCALE_NAMES = {"en" => "English", "es" => "Spanish", "fr" => "French", "de" => "German", "it" => "Italian", "pt" => "Portuguese", "zh" => "Chinese", "ja" => "Japanese", "ko" => "Korean", "ar" => "Arabic", "ru" => "Russian", "nl" => "Dutch", "sv" => "Swedish", "pl" => "Polish", "tr" => "Turkish", "th" => "Thai", "vi" => "Vietnamese", "id" => "Indonesian", "hi" => "Hindi", "he" => "Hebrew", "en-US" => "English (US)", "en-GB" => "English (UK)", "es-ES" => "Spanish (Spain)", "es-MX" => "Spanish (Mexico)", "pt-BR" => "Portuguese (Brazil)", "pt-PT" => "Portuguese (Portugal)", "zh-CN" => "Chinese (Simplified)", "zh-TW" => "Chinese (Traditional)"}

Common locale display names

Class methods

available_locales

Get available locales.

Source
config

Get the configuration.

Source
configure

Configure I18n settings.

Azu::Helpers::I18n.configure do |config|
  config.default_locale = "en"
  config.load_path = ["locales"]
end
Source
default_locale

Get the default locale.

Source
default_locale=(value : String) : String
Source
exists?(key : String, locale : String | Nil = nil) : Bool

Check if a key exists.

Source
fallback_locale

Get fallback locale.

Source
fallback_locale=(value : String | Nil) : String | Nil

Set fallback locale.

Source
l(value : Time, format : String = "date.default", locale : String | Nil = nil) : String

Localize a date or number.

I18n.l(Time.utc, format: "date.short") # => "Jan 15"
I18n.l(Time.utc, format: "date.long")  # => "January 15, 2024"
Source
load_path=(paths : Array(String)) : Nil

Set load path.

Source
load_translations

Load translations from configured paths.

Source
locale

Get/set the current locale for the current fiber.

Source
locale=(value : String) : String
Source
locale_name(locale : String) : String

Get the display name for a locale.

I18n.locale_name("en") # => "English"
I18n.locale_name("es") # => "Spanish"
Source
missing_key_format

Get missing key format.

Source
missing_key_format=(value : String) : String

Set missing key format.

Source
raise_on_missing=(value : Bool) : Bool

Set raise on missing.

Source
raise_on_missing?

Get raise on missing setting.

Source
reload!

Reload translations from files.

Source
reset!

Reset I18n state.

Source
t(key : String, default : String | Nil, count : Int32 | Nil, interpolations : Hash(String, String)) : String

Translate a key with explicit interpolation hash. Used internally and by template helpers.

Source
t(key : String, locale : String | Nil = nil, default : String | Nil = nil, count : Int32 | Nil = nil, **options) : String

Translate a key.

I18n.t("welcome.title")                    # => "Welcome!"
I18n.t("welcome.greeting", name: "John")   # => "Hello, John!"
I18n.t("users.count", count: 5)            # => "5 users"
I18n.t("missing.key", default: "Fallback") # => "Fallback"
Source
with_locale(locale : String, &)

Execute a block with a specific locale.

Source

Nested types