g18n

Types

Named values used to replace {name} placeholders in translations.

Construct parameters with new_format_params and add_param.

pub type FormatParams =
  dict.Dict(String, String)

Optional context used to select key@context translations.

pub type TranslationContext {
  NoContext
  Context(String)
}

Constructors

  • NoContext

    Use the unqualified translation key.

  • Context(String)

    Append the supplied context to the key after @.

Translation strings indexed by dotted keys.

Construct values with new_translations, add_translation, or one of the JSON import functions.

pub opaque type Translations

Primary translations with an optional fallback collection.

Construct translators with new_translator and add a fallback with with_fallback.

pub opaque type Translator

Values

pub fn add_context_translation(
  translations: Translations,
  key: String,
  context: String,
  value: String,
) -> Translations

Add a context-specific translation stored under the exact key@context convention.

pub fn add_param(
  params: dict.Dict(String, String),
  key: String,
  value: String,
) -> dict.Dict(String, String)

Add or replace a named interpolation parameter.

pub fn add_translation(
  translations: Translations,
  key: String,
  value: String,
) -> Translations

Add or replace a translation under a dotted key.

pub fn new_format_params() -> dict.Dict(String, String)

Create an empty parameter collection.

pub fn new_translations() -> Translations

Create an empty translation collection.

pub fn new_translator(translations: Translations) -> Translator

Create a translator from a translation collection.

pub fn translate(translator: Translator, key: String) -> String

Translate a key.

Lookup checks the primary translations and then the fallback translations. If both are missing, the original key is returned unchanged.

pub fn translate_with_context(
  translator: Translator,
  key: String,
  context: TranslationContext,
) -> String

Translate a key with optional context.

Context(value) looks up the exact key@value key. NoContext uses the key unchanged.

pub fn translate_with_context_and_params(
  translator: Translator,
  key: String,
  context: TranslationContext,
  params: dict.Dict(String, String),
) -> String

Translate a context key and replace matching parameters.

Placeholders without a matching parameter remain unchanged.

pub fn translate_with_params(
  translator: Translator,
  key: String,
  params params: dict.Dict(String, String),
) -> String

Translate a key and replace every parameter present in params.

Placeholders without a matching parameter remain unchanged.

pub fn translations_from_json(
  json_string: String,
) -> Result(Translations, Nil)

Decode a flat JSON object whose string keys map to string translations.

Dots in JSON keys are retained as translation hierarchy separators.

pub fn translations_from_nested_json(
  json_string: String,
) -> Result(Translations, String)

Decode nested JSON objects into dotted translation keys.

Invalid JSON and non-object roots return an error. Non-string leaves are ignored.

pub fn translations_to_json(translations: Translations) -> String

Encode translations as a flat JSON object with dotted keys.

pub fn translations_to_nested_json(
  translations: Translations,
) -> String

Encode translations as nested JSON objects derived from dotted keys.

pub fn with_fallback(
  translator: Translator,
  fallback_translations: Translations,
) -> Translator

Add or replace the translator’s fallback translations.

Lookup checks primary translations, then fallback translations, and finally returns the original key.

Search Document