Raxol.Core.I18n (Raxol v0.2.0)

View Source

Internationalization framework for Raxol terminal UI applications.

This module provides comprehensive internationalization (i18n) support:

  • Translation of UI text to multiple languages
  • Right-to-left (RTL) language support
  • Integration with accessibility features
  • Language detection and selection
  • Translation fallbacks
  • Dynamic language switching

The framework works seamlessly with the accessibility module to provide screen reader announcements in the user's preferred language.

Usage

# Initialize with default locale
I18n.init(default_locale: "en")

# Get a translated string
message = I18n.t("welcome_message")

# Get a translated string with variables
greeting = I18n.t("hello_name", %{name: "John"})

# Switch to a different locale
I18n.set_locale("fr")

# Check if the current locale is RTL
is_rtl = I18n.rtl?()

Summary

Functions

Format a screen reader announcement in the current locale.

Get a list of available locales.

Get the current locale.

Handle locale changed events.

Initialize the internationalization system.

Register translations for a specific locale.

Check if the current locale is right-to-left (RTL).

Set the current locale.

Get a translated string for the current locale.

Functions

announce(key, bindings \\ %{}, opts \\ [])

Format a screen reader announcement in the current locale.

This integrates with the accessibility module to provide screen reader announcements in the user's preferred language.

Parameters

  • key - The translation key for the announcement
  • bindings - Map of variable bindings (default: %{})
  • opts - Additional options

Examples

iex> I18n.announce("file_saved")
:ok

iex> I18n.announce("item_selected", %{item: "Document"})
:ok

available_locales()

Get a list of available locales.

Examples

iex> I18n.available_locales()
["en", "fr", "es"]

get_locale()

Get the current locale.

Examples

iex> I18n.get_locale()
"en"

handle_locale_changed(arg)

Handle locale changed events.

init(opts \\ [])

Initialize the internationalization system.

Options

  • :default_locale - The default locale to use (default: "en")
  • :available_locales - List of available locales (default: ["en"])
  • :fallback_locale - Fallback locale when translation is missing (default: "en")
  • :translations_dir - Directory containing translation files (default: "./priv/locales")

Examples

iex> I18n.init()
:ok

iex> I18n.init(default_locale: "fr", available_locales: ["en", "fr", "es"])
:ok

register_translations(locale, translations)

Register translations for a specific locale.

Parameters

  • locale - The locale for these translations
  • translations - Map of translation keys to translated strings

Examples

iex> I18n.register_translations("fr", %{
...>   "welcome_message" => "Bienvenue dans l'application",
...>   "hello_name" => "Bonjour, {{name}}!"
...> })
:ok

rtl?(opts \\ [])

Check if the current locale is right-to-left (RTL).

Options

  • :locale - Check a specific locale instead of the current one

Examples

iex> I18n.rtl?()
false

iex> I18n.rtl?(locale: "ar")
true

set_locale(locale)

Set the current locale.

Parameters

  • locale - The locale to set as current

Examples

iex> I18n.set_locale("fr")
:ok

iex> I18n.set_locale("invalid")
{:error, :invalid_locale}

t(key, bindings \\ %{}, opts \\ [])

Get a translated string for the current locale.

Parameters

  • key - The translation key
  • bindings - Map of variable bindings (default: %{})
  • opts - Additional options

Options

  • :locale - Override the current locale
  • :default - Default text if translation is missing

Examples

iex> I18n.t("welcome_message")
"Welcome to the application"

iex> I18n.t("hello_name", %{name: "John"})
"Hello, John!"

iex> I18n.t("missing_key", %{}, default: "Missing translation")
"Missing translation"