12 releases

0.2.2 Feb 12, 2016
0.2.1 Feb 5, 2016
0.2.0 Oct 17, 2015
0.1.0 Jul 11, 2015
0.0.1 Jan 20, 2015

#3 in Internationalization (i18n)

Download history 13907/week @ 2018-05-06 18998/week @ 2018-05-13 19684/week @ 2018-05-20 19582/week @ 2018-05-27 17928/week @ 2018-06-03 21497/week @ 2018-06-10 18574/week @ 2018-06-17 19947/week @ 2018-06-24 17732/week @ 2018-07-01 17464/week @ 2018-07-08 19098/week @ 2018-07-15 20346/week @ 2018-07-22 16903/week @ 2018-07-29

64,949 downloads per month

rust-language-tags

Build Status Coverage Status MIT licensed crates.io

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. In this example we check if the resource in German language is suitable for a user from Austria. While people speaking Austrian German normally understand standard German the opposite is not always true. So the resource can be presented to the user but if the resource was in de-AT and a user asked for a representation in de the request should be rejected.

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

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

MIT license

Dependencies

Reverse deps