8 releases

0.3.4 May 22, 2023
0.3.3 May 20, 2023
0.3.2 Aug 18, 2022
0.3.0 Jul 7, 2022
0.1.1 Dec 18, 2020

#24 in Internationalization (i18n)

Download history 1345/week @ 2023-10-31 1323/week @ 2023-11-07 1209/week @ 2023-11-14 849/week @ 2023-11-21 1213/week @ 2023-11-28 1186/week @ 2023-12-05 1749/week @ 2023-12-12 1441/week @ 2023-12-19 787/week @ 2023-12-26 1209/week @ 2024-01-02 941/week @ 2024-01-09 1431/week @ 2024-01-16 1370/week @ 2024-01-23 1884/week @ 2024-01-30 1760/week @ 2024-02-06 1411/week @ 2024-02-13

6,730 downloads per month
Used in 53 crates (15 directly)

MIT/Apache

81KB
2.5K SLoC

Language Tags

CI Crate informations License Documentation

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 to copy the data, or simply LanguageTagBuf::new to move the data.

Once parsed, you can explore 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 mutably 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.

Dependencies

~185KB