pub struct Locale { /* private fields */ }
Expand description

A Locale object.

Locale object stores information encoded in a language tag and provides methods allowing for parsing, serializing and manipulating locale fields.

All data is validated and canonicalized on input, which means that the output is always canonicalized.

Currently supported subtags are:

  • language (e.g. “en”)
  • script (e.g. “Latn”)
  • region (e.g. “US”)
  • variants (e.g. “windows”)
  • extensions (e.g. “-u-ca-gregorian-hc-h12”)

The API parses correctly the remaining fields of the BCP47 language tag, but at the moment does not provide any API for operating on them.

Examples

Parsing

Locale supports a From trait from String and &str:

use fluent_locale::Locale;

let loc = Locale::from("en-latn-us");

assert_eq!(loc.to_string(), "en-Latn-US");

Locale can also accept options, similarly to ECMA402 Intl.Locale:

use fluent_locale::Locale;
use std::collections::BTreeMap;

let mut opts = BTreeMap::new();
opts.insert("hour-cycle", "h12");
let loc = Locale::new("en", Some(opts)).unwrap();

assert_eq!(loc.to_string(), "en-u-hc-h12");

Serializing

Locale supports Display trait allowing for:

use fluent_locale::Locale;
use std::collections::BTreeMap;

let mut opts = BTreeMap::new();
opts.insert("hour-cycle", "h12");
let loc = Locale::new("en-Latn-US-u-hc-h23", Some(opts)).unwrap();

assert_eq!(loc.to_string(), "en-Latn-US-u-hc-h12");

Manipulating

During the lifetime of Locale, its fields can be modified via getter/setter methods:

use fluent_locale::Locale;

let mut loc = Locale::from("en-Latn-US");
loc.set_region("GB").unwrap();

assert_eq!(loc.to_string(), "en-Latn-GB");

Implementations§

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts self into a collection.
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Get the TypeId of this object.