dachshund/locale

Dachshund provides internationalization (i18n) for Gleam web applications. It offers locale detection, translation management, and RTL support.

Quick Start

import dachshund/locale

// Get the string code for a locale
locale.to_string(locale.En)  // -> "en"

// Parse a locale from a string
locale.from_string("de")  // -> Ok(De)

// Check text direction for RTL languages
locale.text_direction(locale.Ar)  // -> "rtl"

Usage with Strategies

Dachshund uses strategies to detect the user’s preferred locale. See dachshund/strategy for more details.

Types

A language with its locale code and human-readable display name.

Example

Language(En, "en", "English")
pub type Language {
  Language(Locale, String, String)
}

Constructors

  • Language(Locale, String, String)

A supported locale code.

Use these to define which languages your app supports. Each variant corresponds to an ISO 639-1 language code.

pub type Locale {
  En
  De
  Fr
  Es
  Ja
  Zh
  Ar
  He
  Fa
  Ur
  Yi
  Ps
  Pt
  It
  Ko
  Ru
  Hi
  Nl
  Pl
  Tr
  Sv
  Da
  Fi
  No
  Cs
  El
  Th
  Vi
  Id
  Ms
  Uk
  Hu
  Ro
  Bg
  Hr
  Sk
  Sl
  Ca
  Et
  Lv
  Lt
}

Constructors

  • En
  • De
  • Fr
  • Es
  • Ja
  • Zh
  • Ar
  • He
  • Fa
  • Ur
  • Yi
  • Ps
  • Pt
  • It
  • Ko
  • Ru
  • Hi
  • Nl
  • Pl
  • Tr
  • Sv
  • Da
  • Fi
  • No
  • Cs
  • El
  • Th
  • Vi
  • Id
  • Ms
  • Uk
  • Hu
  • Ro
  • Bg
  • Hr
  • Sk
  • Sl
  • Ca
  • Et
  • Lv
  • Lt

Values

pub fn from_string(s: String) -> Result(Locale, Nil)

Parse a Locale from a string code.

Returns Error(Nil) if the string is not a supported locale.

Examples

locale.from_string("en")    // -> Ok(En)
locale.from_string("fr")    // -> Ok(Fr)
locale.from_string("xyz")   // -> Error(Nil)
pub fn from_string_loose(s: String) -> Result(Locale, Nil)

Parse a Locale from a string, handling locale codes with region subtags.

This is useful for parsing browser navigator.languages which may include codes like "en-US" or "zh-CN".

Examples

locale.from_string_loose("en")     // -> Ok(En)
locale.from_string_loose("en-US")  // -> Ok(En)
locale.from_string_loose("zh-CN")  // -> Ok(Zh)
locale.from_string_loose("invalid") // -> Error(Nil)
pub fn from_string_loose_or_default(s: String) -> Locale

Parse a Locale from a string with region subtag, returning a default if invalid.

Examples

locale.from_string_loose_or_default("fr-CA") // -> Fr
locale.from_string_loose_or_default("xyz")   // -> En (default)
pub fn from_string_or_default(s: String) -> Locale

Parse a Locale from a string, returning a default if invalid.

Examples

locale.from_string_or_default("de")   // -> De
locale.from_string_or_default("xyz") // -> En (default)
pub const languages: List(Language)

All supported languages with their codes and display names.

Use this to build language selector UIs:

import dachshund/locale

fn language_options() {
  locale.languages
  |> list.map(fn(lang) {
    case lang {
      Language(_, code, name) -> #(code, name)
    }
  })
}
pub const rtl_locales: List(Locale)

Locales that use right-to-left text direction.

Use with text_direction to determine which direction to render text.

Example

import dachshund/locale

fn get_html_dir(lang: locale.Locale) {
  locale.text_direction(lang)  // returns "ltr" or "rtl"
}
pub fn text_direction(locale: Locale) -> String

Get the text direction for a locale.

Returns "rtl" for right-to-left languages (Arabic, Hebrew, etc.) and "ltr" for left-to-right languages.

Example

locale.text_direction(locale.En)  // -> "ltr"
locale.text_direction(locale.Ar)  // -> "rtl"
pub fn text_direction_from_string(s: String) -> String

Get the text direction from a locale string.

Convenience function that combines from_string and text_direction.

Example

locale.text_direction_from_string("he")  // -> "rtl"
locale.text_direction_from_string("es")  // -> "ltr"
locale.text_direction_from_string("??")  // -> "ltr" (invalid returns default)
pub fn to_string(locale: Locale) -> String

Convert a Locale to its ISO 639-1 string code.

Examples

locale.to_string(locale.En)  // -> "en"
locale.to_string(locale.Ja)  // -> "ja"
Search Document