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

#6 in Internationalization (i18n)

Download history 43411/week @ 2019-12-10 35423/week @ 2019-12-17 24449/week @ 2019-12-24 28149/week @ 2019-12-31 41992/week @ 2020-01-07 41116/week @ 2020-01-14 40740/week @ 2020-01-21 44223/week @ 2020-01-28 46192/week @ 2020-02-04 43019/week @ 2020-02-11 44057/week @ 2020-02-18 43753/week @ 2020-02-25 45787/week @ 2020-03-03 45054/week @ 2020-03-10 45589/week @ 2020-03-17 39705/week @ 2020-03-24

160,238 downloads per month
Used in 2,005 crates (15 directly)

MIT and maybe MPL-2.0

461 lines


Build Status MIT licensed crates.io Documentation

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.


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

use language_tags::LanguageTag;
let langtag = LanguageTag::parse("fr-BE").unwrap();
assert_eq!(format!("{}", langtag), "fr-BE");

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

use language_tags::LanguageTag;
use std::iter::FromIterator;
let langtag: LanguageTag = "en-x-twain".parse().unwrap();
assert_eq!(langtag.primary_language(), "en");
assert_eq!(Vec::from_iter(langtag.private_use_subtags()), vec!["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 langtag_server = LanguageTag::parse("de-AT").unwrap();
let mut langtag_user = LanguageTag::parse("de").unwrap();