language-tags 0.2.2

Language tags for Rust

Language tags can be used identify human languages, scripts e.g. Latin script, countries and other regions.

Language tags are defined in BCP47, an introduction is "Language tags in HTML and XML" by the W3C. They are commonly used in HTML and HTTP Content-Language and Accept-Language header fields.

This package currently supports parsing (fully conformant parser), formatting and comparing language tags.

Examples

Create a simple language tag representing the French language as spoken in Belgium and print it:

use language_tags::LanguageTag;
let mut langtag: LanguageTag = Default::default();
langtag.language = Some("fr".to_owned());
langtag.region = Some("BE".to_owned());
assert_eq!(format!("{}", langtag), "fr-BE");

Parse a tag representing a special type of English specified by private agreement:

use language_tags::LanguageTag;
let langtag: LanguageTag = "en-x-twain".parse().unwrap();
assert_eq!(format!("{}", langtag.language.unwrap()), "en");
assert_eq!(format!("{:?}", langtag.privateuse), "[\"twain\"]");

You can check for equality, but more often you should test if two tags match.

use language_tags::LanguageTag;
let mut langtag_server: LanguageTag = Default::default();
langtag_server.language = Some("de".to_owned());
langtag_server.region = Some("AT".to_owned());
let mut langtag_user: LanguageTag = Default::default();
langtag_user.language = Some("de".to_owned());
assert!(langtag_user.matches(&langtag_server));

There is also the langtag! macro for creating language tags.