[go: up one dir, main page]

g18n/locale

Types

pub opaque type Locale
pub type LocaleError {
  InvalidLanguage(reason: String)
  InvalidLocale(reason: String)
}

Constructors

  • InvalidLanguage(reason: String)
  • InvalidLocale(reason: String)
pub type LocaleMatch {
  ExactMatch(Locale)
  LanguageMatch(Locale)
  RegionFallback(Locale)
  NoMatch
}

Constructors

pub type LocalePreference {
  Preferred(Locale)
  Acceptable(Locale)
}

Constructors

pub type OrdinalRule {
  First
  Second
  Third
  Nth
}

Constructors

  • First
  • Second
  • Third
  • Nth
pub type PluralForm {
  Cardinal(Int)
  Ordinal(Int)
  Range(from: Int, to: Int)
}

Constructors

  • Cardinal(Int)
  • Ordinal(Int)
  • Range(from: Int, to: Int)
pub type PluralRule {
  Zero
  One
  Two
  Few
  Many
  Other
}

Constructors

  • Zero
  • One
  • Two
  • Few
  • Many
  • Other
pub type PluralRules =
  fn(Int) -> PluralRule

Values

pub fn exact_match(locale1: Locale, locale2: Locale) -> Bool

Check if two locales are exactly identical.

Examples

let assert Ok(en_us1) = g18n.locale("en-US")
let assert Ok(en_us2) = g18n.locale("en-US")
g18n.locales_exact_match(en_us1, en_us2) // True
pub fn get_css_direction(locale: Locale) -> String

Get the CSS direction property value for a locale.

Returns the appropriate CSS direction value for styling purposes.

Examples

let assert Ok(arabic) = g18n.locale("ar")
let assert Ok(english) = g18n.locale("en")

g18n.get_css_direction(arabic)  // "rtl"
g18n.get_css_direction(english) // "ltr"
pub fn is_rtl(locale: Locale) -> Bool

Check if a locale uses right-to-left text direction.

Examples

let assert Ok(arabic) = g18n.locale("ar-SA")
let assert Ok(english) = g18n.locale("en-US")

g18n.is_rtl(arabic)  // True
g18n.is_rtl(english) // False
pub fn language(locale: Locale) -> String

Extract the language code from a locale.

Examples

let assert Ok(locale) = g18n.locale("en-US")
g18n.locale_language(locale) // "en"
pub fn language_only(locale: Locale) -> Locale

Create a new locale with only the language part (no region).

Examples

let assert Ok(en_us) = g18n.locale("en-US")
let en_only = g18n.locale_language_only(en_us)
g18n.locale_string(en_only) // "en"
pub fn match_language(locale1: Locale, locale2: Locale) -> Bool

Check if two locales share the same language (ignoring region).

Examples

let assert Ok(en_us) = g18n.locale("en-US")
let assert Ok(en_gb) = g18n.locale("en-GB")
g18n.locales_match_language(en_us, en_gb) // True
pub fn negotiate_locale(
  available: List(Locale),
  preferred: List(Locale),
) -> Result(Locale, Nil)

Negotiate the best locale match from available options.

Given a list of available locales and user preferences, returns the best match using standard locale negotiation algorithms. Prefers exact matches, falls back to language matches, then to region-less matches.

Examples

let available = [
  locale("en"), locale("en-US"), locale("es"), locale("fr")
]
let preferred = [locale("en-GB"), locale("es"), locale("de")]

g18n.negotiate_locale(available, preferred)
// Returns Some(locale("en")) - language match for en-GB
pub fn new(locale_code: String) -> Result(Locale, LocaleError)

Create a new locale from a locale code string. Supports formats like “en”, “en-US”, “pt-BR”.

Examples

let assert Ok(en) = locale.new("en")
let assert Ok(en_us) = locale.new("en-US")
let assert Error(_) = locale.new("invalid")
pub fn parse_accept_language(header: String) -> List(Locale)

Parse Accept-Language header for locale negotiation.

Parses HTTP Accept-Language header format and returns ordered list of locales by preference (quality values considered).

Examples

g18n.parse_accept_language("en-US,en;q=0.9,fr;q=0.8")
// [Ok(Locale("en", Some("US"))), Ok(Locale("en", None)), Ok(Locale("fr", None))]
pub fn region(locale: Locale) -> option.Option(String)

Extract the region code from a locale.

Examples

let assert Ok(locale) = g18n.locale("en-US")
g18n.locale_region(locale) // Some("US")
pub fn to_string(locale: Locale) -> String

Convert a locale back to its string representation.

Examples

let assert Ok(locale) = g18n.locale("en-US")
g18n.locale_string(locale) // "en-US"
Search Document