pub struct Lang(pub LanguageIdentifier);Expand description
Identifies the language, region and script of text.
Use the lang! macro to construct one, it does compile-time validation.
Use the unic_langid crate for more advanced operations such as runtime parsing and editing identifiers, this
type is just an alias for the core struct of that crate.
Tuple Fields§
§0: LanguageIdentifierImplementations§
Source§impl Lang
impl Lang
Sourcepub fn direction(&self) -> LayoutDirection
pub fn direction(&self) -> LayoutDirection
Returns character direction of the language.
Sourcepub fn matches(
&self,
other: &Self,
self_as_range: bool,
other_as_range: bool,
) -> bool
pub fn matches( &self, other: &Self, self_as_range: bool, other_as_range: bool, ) -> bool
Compares a language to another allowing for either side to use the missing fields as wildcards.
This allows for matching between en (treated as en-*-*-*) and en-US.
§Examples
let l1 = lang!("en");
let l2 = lang!("en-US");
assert_ne!(l1, l2); // "en" != "en-US"
assert!(!l1.matches(&l2, false, false)); // "en" != "en-US"
assert!(l1.matches(&l2, true, false)); // "en-*-*-*" == "en-US"
assert!(!l1.matches(&l2, false, true)); // "en" != "en-*-US-*"
assert!(l1.matches(&l2, true, true)); // "en-*-*-*" == "en-*-US-*"Note that machine variant is ignored when matching.
let pt_br = lang!("pt-BR");
let pt_pt = lang!("pt-PT");
let pt = lang!("pt");
let pt_m = lang!("pt-machine");
assert!(!pt_br.matches(&pt_pt, true, false)); // "pt-BR-*-*" != "pt-PT"
assert!(pt.matches(&pt_pt, true, false)); // "pt-*-*-*" == "pt-PT"
assert!(pt_m.matches(&pt_pt, true, false)); // "pt-machine-*-*" == "pt-PT"
assert!(pt_m.matches(&pt, false, false)); // "pt-machine" == "pt"
assert_ne!(pt_m, pt); // but "pt-machine" != "pt" in `PartialEq`Sourcepub fn is_machine_translation(&self) -> bool
pub fn is_machine_translation(&self) -> bool
If the language has a machine variant.
Machine translated resources are expected to be of lesser quality, but better than nothing.
This variant indicates that the resource should be given lower priority. If a machine generated
resource if reviewed by a human translator the machine variant must be removed.
Sourcepub fn autonym(&self) -> Option<LangAutonym>
pub fn autonym(&self) -> Option<LangAutonym>
Returns the name of the language and region in own language.
The names are in localized title case, when the name is the same in different scripts the script name is part of the language name (currently Hans/Hant).
Returns None if the locale is not one of the 90 most common locales embedded. Language must match,
script must match exactly (omitted different from explicit), region is optional (if not matched returns only language name),
variants are ignored.
Sourcepub fn cmp_display(&self, other: &Self) -> Ordering
pub fn cmp_display(&self, other: &Self) -> Ordering
Get ordering of this and other as fmt::Display text.
This method avoids actually allocating a string.
Methods from Deref<Target = LanguageIdentifier>§
pub fn matches<O>(
&self,
other: &O,
self_as_range: bool,
other_as_range: bool,
) -> boolwhere
O: AsRef<LanguageIdentifier>,
pub fn matches<O>(
&self,
other: &O,
self_as_range: bool,
other_as_range: bool,
) -> boolwhere
O: AsRef<LanguageIdentifier>,
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 variants(&self) -> impl ExactSizeIterator
pub fn variants(&self) -> impl ExactSizeIterator
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.variants().map(|v| v.as_str()).collect::<Vec<_>>(), &["valencia"]);
let li2: LanguageIdentifier = "de".parse()
.expect("Parsing failed.");
assert_eq!(li2.variants().len(), 0);pub fn has_variant(&self, variant: Variant) -> bool
pub fn has_variant(&self, variant: Variant) -> bool
Tests if a variant subtag is present in the LanguageIdentifier.
§Examples
use unic_langid_impl::LanguageIdentifier;
let mut li: LanguageIdentifier = "ca-ES-macos".parse()
.expect("Parsing failed.");
assert_eq!(li.has_variant("valencia".parse().unwrap()), false);
assert_eq!(li.has_variant("macos".parse().unwrap()), true);pub fn character_direction(&self) -> CharacterDirection
pub fn character_direction(&self) -> CharacterDirection
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.character_direction(), CharacterDirection::LTR);
assert_eq!(li2.character_direction(), CharacterDirection::RTL);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Lang
impl<'de> Deserialize<'de> for Lang
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for Lang
Prints the autonym if built with "lang_autonym" and has one, otherwise prints the tags.
impl Display for Lang
Prints the autonym if built with "lang_autonym" and has one, otherwise prints the tags.
{} prints language and region (if set), {:#} only prints language.
Source§impl Ord for Lang
impl Ord for Lang
Source§impl PartialOrd for Lang
impl PartialOrd for Lang
impl Eq for Lang
impl IntoValue<Langs> for Lang
impl StructuralPartialEq for Lang
Auto Trait Implementations§
impl Freeze for Lang
impl RefUnwindSafe for Lang
impl Send for Lang
impl Sync for Lang
impl Unpin for Lang
impl UnwindSafe for Lang
Blanket Implementations§
§impl<T> AnyEq for T
impl<T> AnyEq for T
Source§impl<T> AnyVarValue for T
impl<T> AnyVarValue for T
Source§fn clone_boxed(&self) -> BoxAnyVarValue
fn clone_boxed(&self) -> BoxAnyVarValue
Source§fn eq_any(&self, other: &(dyn AnyVarValue + 'static)) -> bool
fn eq_any(&self, other: &(dyn AnyVarValue + 'static)) -> bool
self and other are equal.Source§fn try_swap(&mut self, other: &mut (dyn AnyVarValue + 'static)) -> bool
fn try_swap(&mut self, other: &mut (dyn AnyVarValue + 'static)) -> bool
other if both are of the same type.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Source§impl<T> FsChangeNote for T
impl<T> FsChangeNote for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more