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

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

Return the serialization of this language tag.

This is fast since that serialization is already stored in the LanguageTag struct.

Return the serialization of this language tag.

This consumes the LanguageTag and takes ownership of the String stored in it.

Return the primary language subtag.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.primary_language(), "zh");

Return the extended language subtags.

Valid language tags have at most one extended language.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.extended_language(), Some("cmn"));

Iterate on the extended language subtags.

Valid language tags have at most one extended language.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.extended_language_subtags().collect::<Vec<_>>(), vec!["cmn"]);

Return the primary language subtag and its extended language subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.full_language(), "zh-cmn");

Return the script subtag.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.script(), Some("Hans"));

Return the region subtag.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.region(), Some("CN"));

Return the variant subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-Latn-TW-pinyin").unwrap();
assert_eq!(language_tag.variant(), Some("pinyin"));

Iterate on the variant subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-Latn-TW-pinyin").unwrap();
assert_eq!(language_tag.variant_subtags().collect::<Vec<_>>(), vec!["pinyin"]);

Return the extension subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("de-DE-u-co-phonebk").unwrap();
assert_eq!(language_tag.extension(), Some("u-co-phonebk"));

Iterate on the extension subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("de-DE-u-co-phonebk").unwrap();
assert_eq!(language_tag.extension_subtags().collect::<Vec<_>>(), vec![('u', "co-phonebk")]);

Return the private use subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("de-x-foo-bar").unwrap();
assert_eq!(language_tag.private_use(), Some("x-foo-bar"));

Iterate on the private use subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("de-x-foo-bar").unwrap();
assert_eq!(language_tag.private_use_subtags().collect::<Vec<_>>(), vec!["foo", "bar"]);

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.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("en-us").unwrap();
assert_eq!(language_tag.into_string(), "en-US")
Errors

If the language tag is not “well-formed” a ParseError variant will be returned.

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.

Check if the language tag is valid according to RFC 5646.

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.

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));

Checks if it is a language range, meaning that there are no extension and privateuse tags.

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

Formats the value using the given formatter. Read more

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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.

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.