Struct language_tags::LanguageTag [−][src]
pub struct LanguageTag { /* fields omitted */ }
A language tag as described in RFC 5646.
Language tags are used to help identify languages, whether spoken, written, signed, or otherwise signaled, for the purpose of communication. This includes constructed and artificial languages but excludes languages not intended primarily for human communication, such as programming languages.
Implementations
impl LanguageTag
[src]
pub fn as_str(&self) -> &str
[src]
Return the serialization of this language tag.
This is fast since that serialization is already stored in the LanguageTag
struct.
pub fn into_string(self) -> String
[src]
Return the serialization of this language tag.
This consumes the LanguageTag
and takes ownership of the String
stored in it.
pub fn primary_language(&self) -> &str
[src]
Return the primary language subtag.
pub fn extended_language(&self) -> Option<&str>
[src]
Return the extended language subtags.
Valid language tags have at most one extended language.
pub fn extended_language_subtags(&self) -> impl Iterator<Item = &str>
[src]
Iterate on the extended language subtags.
Valid language tags have at most one extended language.
pub fn full_language(&self) -> &str
[src]
Return the primary language subtag and its extended language subtags.
pub fn script(&self) -> Option<&str>
[src]
Return the script subtag.
pub fn region(&self) -> Option<&str>
[src]
Return the region subtag.
pub fn variant(&self) -> Option<&str>
[src]
Return the variant subtags.
pub fn variant_subtags(&self) -> impl Iterator<Item = &str>
[src]
Iterate on the variant subtags.
pub fn extension(&self) -> Option<&str>
[src]
Return the extension subtags.
pub fn extension_subtags(&self) -> impl Iterator<Item = (char, &str)>
[src]
Iterate on the extension subtags.
pub fn private_use(&self) -> Option<&str>
[src]
Return the private use subtags.
pub fn private_use_subtags(&self) -> impl Iterator<Item = &str>
[src]
Iterate on the private use subtags.
pub fn parse(input: &str) -> Result<Self, ParseError>
[src]
Create a LanguageTag
from its serialization.
This parser accepts the language tags that are “well-formed” according to
RFC 5646.
Full validation could be done with the validate
method.
Errors
If the language tag is not “well-formed” a ParseError
variant will be returned.
pub fn validate(&self) -> Result<(), ValidationError>
[src]
Check if the language tag is “valid” according to RFC 5646.
It applies the following steps:
- grandfathereds and private use tags are valid
- There should be no more than one extended language subtag (c.f. errata 5457).
- Primary language, extended language, script, region and variants should appear in the IANA Language Subtag Registry.
- Extended language and variants should have a correct prefix as set in the IANA Language Subtag Registry.
- There should be no duplicate variant and singleton (extension) subtags.
Errors
If the language tag is not “valid” a ValidationError
variant will be returned.
pub fn is_valid(&self) -> bool
[src]
Check if the language tag is valid according to RFC 5646.
pub fn canonicalize(&self) -> Result<LanguageTag, ValidationError>
[src]
Returns the canonical version of the language tag following RFC 5646 4.5.
It currently applies the following steps:
- Grandfathered tags are replaced with the canonical version if possible.
- Redundant tags are replaced with the canonical version if possible.
- Extension languages are promoted to primary language.
- Deprecated languages, scripts, regions and variants are replaced with modern equivalents.
- Suppress-Script is applied to remove default script for a language (e.g. “en-Latn” is canonicalized as “en”).
- Variants are deduplicated
Errors
If there is not a unique way to canonicalize the language tag
a ValidationError
variant will be returned.
pub fn matches(&self, other: &LanguageTag) -> bool
[src]
Matches language tags. The first language acts as a language range, the second one is used as a normal language tag. None fields in the language range are ignored. If the language tag has more extlangs than the range these extlangs are ignored. Matches are case-insensitive.
For example the range en-GB
matches only en-GB
and en-Arab-GB
but not en
.
The range en
matches all language tags starting with en
including en
, en-GB
,
en-Arab
and en-Arab-GB
.
Panics
If the language range has extensions or private use tags.
Examples
use language_tags::LanguageTag; let range_italian = LanguageTag::parse("it").unwrap(); let tag_german = LanguageTag::parse("de").unwrap(); let tag_italian_switzerland = LanguageTag::parse("it-CH").unwrap(); assert!(!range_italian.matches(&tag_german)); assert!(range_italian.matches(&tag_italian_switzerland)); let range_spanish_brazil = LanguageTag::parse("es-BR").unwrap(); let tag_spanish = LanguageTag::parse("es").unwrap(); assert!(!range_spanish_brazil.matches(&tag_spanish));
pub fn is_language_range(&self) -> bool
[src]
Checks if it is a language range, meaning that there are no extension and privateuse tags.
Trait Implementations
impl Clone for LanguageTag
[src]
fn clone(&self) -> LanguageTag
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Debug for LanguageTag
[src]
impl Display for LanguageTag
[src]
impl Eq for LanguageTag
[src]
impl FromStr for LanguageTag
[src]
type Err = ParseError
The associated error which can be returned from parsing.
fn from_str(input: &str) -> Result<Self, ParseError>
[src]
impl Hash for LanguageTag
[src]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl PartialEq<LanguageTag> for LanguageTag
[src]
fn eq(&self, other: &LanguageTag) -> bool
[src]
fn ne(&self, other: &LanguageTag) -> bool
[src]
impl StructuralEq for LanguageTag
[src]
impl StructuralPartialEq for LanguageTag
[src]
Auto Trait Implementations
impl RefUnwindSafe for LanguageTag
impl Send for LanguageTag
impl Sync for LanguageTag
impl Unpin for LanguageTag
impl UnwindSafe for LanguageTag
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,