I18n Functions
Internationalization with translations, pluralization, and locale-aware formatting.
Locale Management
I18n.locale()
Get the current locale.
Returns
String - The current locale code (e.g., "en", "fr", "de")
println(I18n.locale()) // "en"
I18n.set_locale(locale)
Set the current locale.
Parameters
locale : String - The locale code to set
I18n.set_locale("fr")
I18n.set_locale("de")
Translations
I18n.translate(key, locale?, translations?)
Translate a key to the specified locale.
Parameters
key : String - The translation keylocale : String? - Optional locale (uses current if not specified)translations : Hash? - Optional translations hashlet translations = {
"en.greeting": "Hello",
"fr.greeting": "Bonjour",
"de.greeting": "Hallo",
"en.welcome": "Welcome, {name}!"
}
I18n.translate("greeting", "fr", translations) // "Bonjour"
I18n.translate("greeting", "en", translations) // "Hello"
I18n.plural(key, count, locale?, translations?)
Handle pluralization based on count.
Plural Forms
Use suffixes:
_zero, _one, _other
let translations = {
"en.items_zero": "No items",
"en.items_one": "1 item",
"en.items_other": "{count} items"
}
I18n.plural("items", 0, "en", translations) // "No items"
I18n.plural("items", 1, "en", translations) // "1 item"
I18n.plural("items", 5, "en", translations) // "5 items"
Locale-Aware Formatting
I18n.format_number(number, locale?)
Format a number according to locale conventions.
I18n.format_number(1234.56, "en") // "1,234.56"
I18n.format_number(1234.56, "fr") // "1 234,56"
I18n.format_number(1234.56, "de") // "1.234,56"
I18n.format_currency(amount, currency, locale?)
Format a currency amount.
Parameters
amount : Float - The amountcurrency : String - Currency code (USD, EUR, etc.)locale : String? - Optional localeI18n.format_currency(1234.56, "USD", "en") // "$1,234.56"
I18n.format_currency(1234.56, "EUR", "de") // "1.234,56 €"
I18n.format_currency(1234.56, "GBP", "en") // "£1,234.56"
I18n.format_date(timestamp, locale?)
Format a date according to locale conventions.
let ts = __datetime_parse("2024-01-15T10:30:00Z")
I18n.format_date(ts, "en") // "01/15/2024"
I18n.format_date(ts, "fr") // "15/01/2024"
I18n.format_date(ts, "de") // "15.01.2024"
Complete Example
// Define translations
let translations = {
"en.welcome": "Welcome!",
"en.cart_items_zero": "Your cart is empty",
"en.cart_items_one": "You have 1 item in your cart",
"en.cart_items_other": "You have {count} items in your cart",
"en.total": "Total: {amount}",
"fr.welcome": "Bienvenue!",
"fr.cart_items_zero": "Votre panier est vide",
"fr.cart_items_one": "Vous avez 1 article",
"fr.cart_items_other": "Vous avez {count} articles",
"fr.total": "Total: {amount}"
}
// Set locale based on user preference
I18n.set_locale(user["preferred_locale"] ?? "en")
// Use translations
let welcome = I18n.translate("welcome", null, translations)
let cart_msg = I18n.plural("cart_items", cart_count, null, translations)
let total = I18n.format_currency(cart_total, "USD")