[][src]Struct unic_langid_impl::LanguageIdentifier

pub struct LanguageIdentifier { /* fields omitted */ }

LanguageIdentifier is a core struct representing a Unicode Language Identifier.

Examples

use unic_langid_impl::LanguageIdentifier;

let li: LanguageIdentifier = "en-US".parse()
    .expect("Failed to parse.");

assert_eq!(li.get_language(), "en");
assert_eq!(li.get_script(), None);
assert_eq!(li.get_region(), Some("US"));
assert_eq!(li.get_variants().len(), 0);

Parsing

Unicode recognizes three levels of standard conformance for any language identifier:

  • 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.

Eamples:

use unic_langid_impl::LanguageIdentifier;

let li: LanguageIdentifier = "eN_latn_Us-Valencia".parse()
    .expect("Failed to parse.");

assert_eq!(li.get_language(), "en");
assert_eq!(li.get_script(), Some("Latn"));
assert_eq!(li.get_region(), Some("US"));
assert_eq!(li.get_variants(), &["valencia"]);

Methods

impl LanguageIdentifier[src]

pub fn from_parts<S: AsRef<str>>(
    language: Option<S>,
    script: Option<S>,
    region: Option<S>,
    variants: &[S]
) -> Result<Self, LanguageIdentifierError>
[src]

A constructor which takes optional subtags as &str, parses them and produces a well-formed LanguageIdentifier.

Examples

use unic_langid_impl::LanguageIdentifier;

let li = LanguageIdentifier::from_parts(Some("fr"), None, Some("CA"), &[])
    .expect("Parsing failed.");

assert_eq!(li.to_string(), "fr-CA");

pub fn into_raw_parts(
    self
) -> (Option<u64>, Option<u32>, Option<u32>, Option<Box<[u64]>>)
[src]

Consumes LanguageIdentifier and produces raw internal representations of all subtags in form of u64/u32.

Primarily used for storing internal representation and restoring via an unsafe from_raw_parts_unchecked.

Examples

use unic_langid_impl::LanguageIdentifier;
use tinystr::{TinyStr8, TinyStr4};

let li: LanguageIdentifier = "en-US".parse()
    .expect("Parsing failed.");

let (lang, script, region, variants) = li.into_raw_parts();

let li2 = unsafe { LanguageIdentifier::from_raw_parts_unchecked(
    lang.map(|l| TinyStr8::new_unchecked(l)),
    script.map(|s| TinyStr4::new_unchecked(s)),
    region.map(|r| TinyStr4::new_unchecked(r)),
    variants.map(|v| v.into_iter().map(|v| TinyStr8::new_unchecked(*v)).collect()),
) };

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

pub const unsafe fn from_raw_parts_unchecked(
    language: Option<TinyStr8>,
    script: Option<TinyStr4>,
    region: Option<TinyStr4>,
    variants: Option<Box<[TinyStr8]>>
) -> Self
[src]

Consumes raw representation of subtags generating new LanguageIdentifier without any checks.

Primarily used for restoring internal representation.

Examples

use unic_langid_impl::LanguageIdentifier;
use tinystr::{TinyStr8, TinyStr4};

let li: LanguageIdentifier = "en-US".parse()
    .expect("Parsing failed.");

let (lang, script, region, variants) = li.into_raw_parts();

let li2 = unsafe { LanguageIdentifier::from_raw_parts_unchecked(
    lang.map(|l| TinyStr8::new_unchecked(l)),
    script.map(|s| TinyStr4::new_unchecked(s)),
    region.map(|r| TinyStr4::new_unchecked(r)),
    variants.map(|v| v.into_iter().map(|v| TinyStr8::new_unchecked(*v)).collect()),
) };

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

pub fn matches<O: AsRef<Self>>(
    &self,
    other: &O,
    self_as_range: bool,
    other_as_range: bool
) -> bool
[src]

Compares a LanguageIdentifier to another AsRef<LanguageIdentifier 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_langid_impl::LanguageIdentifier;

let li1: LanguageIdentifier = "en".parse()
    .expect("Parsing failed.");

let li2: LanguageIdentifier = "en-US".parse()
    .expect("Parsing failed.");

assert_ne!(li1, li2); // "en" != "en-US"
assert_ne!(li1.to_string(), li2.to_string()); // "en" != "en-US"

assert_eq!(li1.matches(&li2, false, false), false); // "en" != "en-US"
assert_eq!(li1.matches(&li2, true, false), true); // "en-*-*-*" == "en-US"
assert_eq!(li1.matches(&li2, false, true), false); // "en" != "en-*-US-*"
assert_eq!(li1.matches(&li2, true, true), true); // "en-*-*-*" == "en-*-US-*"

pub fn get_language(&self) -> &str[src]

Returns the language subtag of the LanguageIdentifier.

If the language is empty, "und" is returned.

Examples

use unic_langid_impl::LanguageIdentifier;

let li1: LanguageIdentifier = "de-AT".parse()
    .expect("Parsing failed.");

assert_eq!(li1.get_language(), "de");

let li2: LanguageIdentifier = "und-AT".parse()
    .expect("Parsing failed.");

assert_eq!(li2.get_language(), "und");

pub fn set_language(
    &mut self,
    language: Option<&str>
) -> Result<(), LanguageIdentifierError>
[src]

Sets the language subtag of the LanguageIdentifier.

If None is passed, the field will be set to None and returned as "und".

Examples

use unic_langid_impl::LanguageIdentifier;

let mut li: LanguageIdentifier = "de-Latn-AT".parse()
    .expect("Parsing failed.");

li.set_language(Some("fr"));

assert_eq!(li.to_string(), "fr-Latn-AT");

pub fn get_script(&self) -> Option<&str>[src]

Returns the script subtag of the LanguageIdentifier, if set.

Examples

use unic_langid_impl::LanguageIdentifier;

let li1: LanguageIdentifier = "de-Latn-AT".parse()
    .expect("Parsing failed.");

assert_eq!(li1.get_script(), Some("Latn"));

let li2: LanguageIdentifier = "de-AT".parse()
    .expect("Parsing failed.");

assert_eq!(li2.get_script(), None);

pub fn set_script(
    &mut self,
    script: Option<&str>
) -> Result<(), LanguageIdentifierError>
[src]

Sets the script subtag of the LanguageIdentifier.

Examples

use unic_langid_impl::LanguageIdentifier;

let mut li: LanguageIdentifier = "sr-Latn".parse()
    .expect("Parsing failed.");

li.set_script(Some("Cyrl"));

assert_eq!(li.to_string(), "sr-Cyrl");

pub fn get_region(&self) -> Option<&str>[src]

Returns the region subtag of the LanguageIdentifier, if set.

Examples

use unic_langid_impl::LanguageIdentifier;

let li1: LanguageIdentifier = "de-Latn-AT".parse()
    .expect("Parsing failed.");

assert_eq!(li1.get_region(), Some("AT"));

let li2: LanguageIdentifier = "de".parse()
    .expect("Parsing failed.");

assert_eq!(li2.get_region(), None);

pub fn set_region(
    &mut self,
    region: Option<&str>
) -> Result<(), LanguageIdentifierError>
[src]

Sets the region subtag of the LanguageIdentifier.

Examples

use unic_langid_impl::LanguageIdentifier;

let mut li: LanguageIdentifier = "fr-FR".parse()
    .expect("Parsing failed.");

li.set_region(Some("CA"));

assert_eq!(li.to_string(), "fr-CA");

pub fn get_variants(&self) -> Vec<&str>[src]

Returns a vector of variants subtags of the LanguageIdentifier.

Examples

use unic_langid_impl::LanguageIdentifier;

let li1: LanguageIdentifier = "ca-ES-valencia".parse()
    .expect("Parsing failed.");

assert_eq!(li1.get_variants(), &["valencia"]);

let li2: LanguageIdentifier = "de".parse()
    .expect("Parsing failed.");

assert_eq!(li2.get_variants().len(), 0);

pub fn set_variants(
    &mut self,
    variants: &[&str]
) -> Result<(), LanguageIdentifierError>
[src]

Sets variant subtags of the LanguageIdentifier.

Examples

use unic_langid_impl::LanguageIdentifier;

let mut li: LanguageIdentifier = "ca-ES".parse()
    .expect("Parsing failed.");

li.set_variants(&["valencia"]);

assert_eq!(li.to_string(), "ca-ES-valencia");

pub fn get_character_direction(&self) -> CharacterDirection[src]

Returns character direction of the LanguageIdentifier.

Examples

use unic_langid_impl::{LanguageIdentifier, CharacterDirection};

let li1: LanguageIdentifier = "es-AR".parse()
    .expect("Parsing failed.");
let li2: LanguageIdentifier = "fa".parse()
    .expect("Parsing failed.");

assert_eq!(li1.get_character_direction(), CharacterDirection::LTR);
assert_eq!(li2.get_character_direction(), CharacterDirection::RTL);

Trait Implementations

impl AsRef<LanguageIdentifier> for LanguageIdentifier[src]

impl Clone for LanguageIdentifier[src]

impl Default for LanguageIdentifier[src]

impl Eq for LanguageIdentifier[src]

impl PartialEq<LanguageIdentifier> for LanguageIdentifier[src]

impl Display for LanguageIdentifier[src]

impl Debug for LanguageIdentifier[src]

impl FromStr for LanguageIdentifier[src]

type Err = LanguageIdentifierError

The associated error which can be returned from parsing.

impl Hash for LanguageIdentifier[src]

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]