3 unstable releases

0.2.0 Dec 19, 2020
0.1.1 Dec 18, 2020
0.1.0 Dec 18, 2020

#607 in Data structures

Download history 40/week @ 2021-09-28 49/week @ 2021-10-05 141/week @ 2021-10-12 79/week @ 2021-10-19 76/week @ 2021-10-26 83/week @ 2021-11-02 57/week @ 2021-11-09 56/week @ 2021-11-16 29/week @ 2021-11-23 45/week @ 2021-11-30 116/week @ 2021-12-07 50/week @ 2021-12-14 64/week @ 2021-12-21 34/week @ 2021-12-28 80/week @ 2022-01-04 58/week @ 2022-01-11

262 downloads per month
Used in 15 crates (2 directly)

MIT/Apache

76KB
2.5K SLoC

Language Tags

Documentation Crate informations Repository

This crate provides an implementation of language tags defined by RFC5646 (BCP47).

Usage

You can easily parse new language from anything that provides a [u8] reference:

extern crate langtag;
use langtag::LanguageTag;

fn main() -> Result<(), langtag::Error> {
	let tag = LanguageTag::parse("fr-FR")?;
	assert_eq!(tag.language().unwrap().primary(), "fr");
	assert!(tag == "Fr-fr"); // comparison is case-insensitive.
	Ok(())
}

Note that LanguageTag::parse does not copy the data it is given, but only borrows it. You can create an owning LanguageTag instance by using LanguageTagBuf::parse_copy instead.

Once parsed, you can exlore every component of the language tag using the provided functions.

Mutable language tags

When the language tags owns its buffer through Vec<u8>, it becomes possible to access the tag mutabily to modify it.

extern crate langtag;
use std::convert::TryInto;
use langtag::LangTag;

fn main() -> Result<(), langtag::Error> {
	let mut tag = LangTag::parse_copy("fr-FR")?;
	tag.language_mut().set_primary("jp".try_into()?);
	tag.set_region(None);
	tag.extensions_mut().insert('f'.try_into()?, "bar".try_into()?);
	assert_eq!(tag, "jp-f-bar");
	Ok(())
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps

_