15 releases (8 breaking)

0.9.4 Dec 13, 2023
0.9.1 Oct 19, 2022
0.9.0 May 6, 2020
0.8.0 Jan 28, 2020
0.4.1 Jul 29, 2019

#11 in Internationalization (i18n)

Download history 23433/week @ 2023-12-23 22207/week @ 2023-12-30 28763/week @ 2024-01-06 30914/week @ 2024-01-13 27916/week @ 2024-01-20 30611/week @ 2024-01-27 36106/week @ 2024-02-03 35387/week @ 2024-02-10 37370/week @ 2024-02-17 31856/week @ 2024-02-24 34116/week @ 2024-03-02 36336/week @ 2024-03-09 34746/week @ 2024-03-16 39184/week @ 2024-03-23 36481/week @ 2024-03-30 27913/week @ 2024-04-06

143,323 downloads per month
Used in 273 crates (82 directly)

MIT/Apache

600KB
12K SLoC

unic-langid Build Status Coverage Status

unic-langid is an API for managing Unicode Language Identifiers.

The crate provides a way to create a struct from a string, manipulate its fields, canonicalize it, and serialize into a string.

Usage

use unic_langid::LanguageIdentifier;

let loc: LanguageIdentifier = "en-US".parse().expect("Parsing failed.");

assert_eq!(loc.language(), "en");
assert_eq!(loc.script(), None);
assert_eq!(loc.region(), Some("US"));

loc.set_script(Some("latn"));

assert_eq!(&loc.to_string(), "en-Latn-US");
use unic_langid::LanguageIdentifier;

let langid = LanguageIdentifier::from_parts(
    Some("en"),
    None,
    None,
    Some(&["nedis", "macos"])
).expect("Parsing failed.");

assert_eq!(&langid.to_string(), "en-macos-nedis")

Macros

unic-langid can be also compiled with features = ["macros"] which enables langid! macro:

use unic_langid::langid;

// The `const` assignment will currently work only
// if the langid doesn't contain any variants.
const EN_US: LanguageIdentifier = langid!("en-US");

fn main() {
    let de_at = langid!("de-AT");

    assert_eq!(&de_at.to_string(), "de-AT")
    assert_eq!(&EN_US.to_string(), "en-US")
}

The macro allows for compile-time parsing and validation of literal language identifiers.

Status

The crate is mostly complete on the well-formed level. The next batch of work will go into API maturity.

Get Involved

unic-langid is open-source, licensed under the Apache License, Version 2.0. We encourage everyone to take a look at our code and we'll listen to your feedback.

Dependencies

~0.4–0.9MB
~20K SLoC