| 1 | # rust-language-tags |
| 2 | [](https://travis-ci.org/pyfisch/rust-language-tags) |
| 3 | [](./LICENSE) |
| 4 | [](https://crates.io/crates/language-tags) |
| 5 | [](https://docs.rs/language-tags) |
| 6 | |
| 7 | Language tags can be used identify human languages, scripts e.g. Latin script, countries and |
| 8 | other regions. |
| 9 | |
| 10 | Language tags are defined in [BCP47](http://tools.ietf.org/html/bcp47), an introduction is |
| 11 | ["Language tags in HTML and XML"](http://www.w3.org/International/articles/language-tags/) by |
| 12 | the W3C. They are commonly used in HTML and HTTP `Content-Language` and `Accept-Language` |
| 13 | header fields. |
| 14 | |
| 15 | This package currently supports parsing (fully conformant parser), formatting and comparing |
| 16 | language tags. |
| 17 | |
| 18 | ## Examples |
| 19 | Create a simple language tag representing the French language as spoken |
| 20 | in Belgium and print it: |
| 21 | |
| 22 | ```rust |
| 23 | use language_tags::LanguageTag; |
| 24 | let langtag = LanguageTag::parse("fr-BE").unwrap(); |
| 25 | assert_eq!(format!("{}", langtag), "fr-BE"); |
| 26 | ``` |
| 27 | |
| 28 | Parse a tag representing a special type of English specified by private agreement: |
| 29 | |
| 30 | ```rust |
| 31 | use language_tags::LanguageTag; |
| 32 | use std::iter::FromIterator; |
| 33 | let langtag: LanguageTag = "en-x-twain".parse().unwrap(); |
| 34 | assert_eq!(langtag.primary_language(), "en"); |
| 35 | assert_eq!(Vec::from_iter(langtag.private_use_subtags()), vec!["twain"]); |
| 36 | ``` |
| 37 | |
| 38 | You can check for equality, but more often you should test if two tags match. |
| 39 | In this example we check if the resource in German language is suitable for |
| 40 | a user from Austria. While people speaking Austrian German normally understand |
| 41 | standard German the opposite is not always true. So the resource can be presented |
| 42 | to the user but if the resource was in `de-AT` and a user asked for a representation |
| 43 | in `de` the request should be rejected. |
| 44 | |
| 45 | |
| 46 | ```rust |
| 47 | use language_tags::LanguageTag; |
| 48 | let mut langtag_server = LanguageTag::parse("de-AT").unwrap(); |
| 49 | let mut langtag_user = LanguageTag::parse("de").unwrap(); |
| 50 | assert!(langtag_user.matches(&langtag_server)); |
| 51 | ``` |
| 52 | |
| 53 | ## Related crates |
| 54 | |
| 55 | If you only want to validate and normalize the formatting of language tags or you are working with RDF consider using the [oxilangtag](https://github.com/oxigraph/oxilangtag) crate. |
| 56 | It is much more lightweight as it doesn't contain a language tag database and has a very similar interface to this crate. |