Struct unic_locale_impl::Locale
source · pub struct Locale {
pub id: LanguageIdentifier,
pub extensions: ExtensionsMap,
}Expand description
Locale is a core struct representing a Unicode Locale Identifier.
A locale is made of two parts:
id- Unicode Language Identifierextensions- A set of Unicode Extensions
Locale exposes all of the same methods as LanguageIdentifier, and
on top of that is able to parse, manipulate and serialize unicode extension
fields.
Examples
use unic_locale_impl::Locale;
let loc: Locale = "en-US-u-ca-buddhist".parse()
.expect("Failed to parse.");
assert_eq!(loc.id.language, "en");
assert_eq!(loc.id.script, None);
assert_eq!(loc.id.region, Some("US".parse().unwrap()));
assert_eq!(loc.id.variants().len(), 0);
assert_eq!(loc.extensions.unicode.keyword("ca")
.expect("Getting keyword failed.")
.collect::<Vec<_>>(),
&["buddhist"]);Parsing
Unicode recognizes three levels of standard conformance for a locale:
- well-formed - syntactically correct
- valid - well-formed and only uses registered language subtags, extensions, keywords, types…
- canonical - valid and no deprecated codes or structure.
At the moment parsing normalizes a well-formed language identifier converting
_ separators to - and adjusting casing to conform to the Unicode standard.
Any bogus subtags will cause the parsing to fail with an error. No subtag validation is performed.
Examples:
use unic_locale_impl::Locale;
let loc: Locale = "eN_latn_Us-Valencia_u-hC-H12".parse()
.expect("Failed to parse.");
assert_eq!(loc.id.language, "en");
assert_eq!(loc.id.script, Some("Latn".parse().unwrap()));
assert_eq!(loc.id.region, Some("US".parse().unwrap()));
assert_eq!(loc.id.variants().collect::<Vec<_>>(), &["valencia"]);Fields§
§id: LanguageIdentifier§extensions: ExtensionsMapImplementations§
source§impl Locale
impl Locale
sourcepub fn from_bytes(v: &[u8]) -> Result<Self, LocaleError>
pub fn from_bytes(v: &[u8]) -> Result<Self, LocaleError>
A constructor which takes a utf8 slice, parses it and
produces a well-formed Locale.
Examples
use unic_locale_impl::Locale;
let loc = Locale::from_bytes("en-US-u-hc-h12".as_bytes())
.expect("Parsing failed.");
assert_eq!(loc.to_string(), "en-US-u-hc-h12");sourcepub fn from_parts(
language: Language,
script: Option<Script>,
region: Option<Region>,
variants: &[Variant],
extensions: Option<ExtensionsMap>
) -> Self
pub fn from_parts( language: Language, script: Option<Script>, region: Option<Region>, variants: &[Variant], extensions: Option<ExtensionsMap> ) -> Self
A constructor which takes optional subtags as AsRef<[u8]>, parses them and
produces a well-formed Locale.
Examples
use unic_locale_impl::Locale;
let loc = Locale::from_parts("fr".parse().unwrap(), None, Some("CA".parse().unwrap()), &[], None);
assert_eq!(loc.to_string(), "fr-CA");sourcepub const unsafe fn from_raw_parts_unchecked(
language: Language,
script: Option<Script>,
region: Option<Region>,
variants: Option<Box<[Variant]>>,
extensions: ExtensionsMap
) -> Self
pub const unsafe fn from_raw_parts_unchecked( language: Language, script: Option<Script>, region: Option<Region>, variants: Option<Box<[Variant]>>, extensions: ExtensionsMap ) -> Self
Safety
This function accepts subtags expecting variants to be deduplicated and ordered.
sourcepub fn into_parts(
self
) -> (Language, Option<Script>, Option<Region>, Vec<Variant>, String)
pub fn into_parts( self ) -> (Language, Option<Script>, Option<Region>, Vec<Variant>, String)
Consumes Locale and produces raw internal representations
of all subtags in form of u64/u32.
Primarily used for storing internal representation and restoring via
from_raw_parts_unchecked.
Examples
use unic_locale_impl::Locale;
use tinystr::{TinyStr8, TinyStr4};
let loc: Locale = "en-US".parse()
.expect("Parsing failed.");
let (lang, script, region, variants, extensions) = loc.into_parts();
let loc2 = Locale::from_parts(
lang,
script,
region,
&variants,
Some(extensions.parse().unwrap())
);
assert_eq!(loc2.to_string(), "en-US");sourcepub fn matches<O: AsRef<Self>>(
&self,
other: &O,
self_as_range: bool,
other_as_range: bool
) -> bool
pub fn matches<O: AsRef<Self>>( &self, other: &O, self_as_range: bool, other_as_range: bool ) -> bool
Compares a Locale to another AsRef<Locale
allowing for either side to use the missing fields as wildcards.
This allows for matching between en (treated as en-*-*-*) and en-US.
Examples
use unic_locale_impl::Locale;
let loc1: Locale = "en".parse()
.expect("Parsing failed.");
let loc2: Locale = "en-US".parse()
.expect("Parsing failed.");
assert_ne!(loc1, loc2); // "en" != "en-US"
assert_ne!(loc1.to_string(), loc2.to_string()); // "en" != "en-US"
assert_eq!(loc1.matches(&loc2, false, false), false); // "en" != "en-US"
assert_eq!(loc1.matches(&loc2, true, false), true); // "en-*-*-*" == "en-US"
assert_eq!(loc1.matches(&loc2, false, true), false); // "en" != "en-*-US-*"
assert_eq!(loc1.matches(&loc2, true, true), true); // "en-*-*-*" == "en-*-US-*"Trait Implementations§
source§impl AsRef<LanguageIdentifier> for Locale
impl AsRef<LanguageIdentifier> for Locale
source§fn as_ref(&self) -> &LanguageIdentifier
fn as_ref(&self) -> &LanguageIdentifier
source§impl From<LanguageIdentifier> for Locale
impl From<LanguageIdentifier> for Locale
source§fn from(id: LanguageIdentifier) -> Self
fn from(id: LanguageIdentifier) -> Self
source§impl From<Locale> for LanguageIdentifier
impl From<Locale> for LanguageIdentifier
source§impl Ord for Locale
impl Ord for Locale
source§impl PartialEq for Locale
impl PartialEq for Locale
source§impl PartialOrd for Locale
impl PartialOrd for Locale
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more