Azu::Helpers::I18n
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
- 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"
- 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
configure
Configure I18n settings.
Azu::Helpers::I18n.configure do |config|
config.default_locale = "en"
config.load_path = ["locales"]
end
Localize a date or number.
I18n.l(Time.utc, format: "date.short") # => "Jan 15"
I18n.l(Time.utc, format: "date.long") # => "January 15, 2024"
Get the display name for a locale.
I18n.locale_name("en") # => "English"
I18n.locale_name("es") # => "Spanish"
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.
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"