Crate isolang

source ·
Expand description

ISO 639 language codes

When dealing with different language inputs and APIs, different standards are used to identify a language. Converting between these in an automated way can be tedious. This crate provides an enum which supports conversion from 639-1 and 639-3 and also into these formats, as well as into their names. The English name can be retrieved using Language::to_name() if compiled with the english_names feature. The autonyms (local names) can be retrieved using to_autonym() if compiled with the local_names feature.

The language table is compiled into the library. While this increases the binary size, it means that no additional time is wasted on program startup or on table access for allocating or filling the map. It is hence suitable for retrieval of codes in constraint environments.

Examples

use isolang::Language;
#[cfg(feature = "english_names")]
assert_eq!(Language::from_639_1("de").unwrap().to_name(), "German");
#[cfg(feature = "local_names")]
assert_eq!(Language::from_639_1("de").unwrap().to_autonym(), Some("Deutsch"));

assert_eq!(Language::from_639_3("spa").unwrap().to_639_1(), Some("es"));

#[cfg(feature = "list_languages")]
{
    // Filter languages with a ISO 639-1 code
    let languages = isolang::languages();
    let languages_with_iso_639_1 = languages.filter(|language| language.to_639_1().is_some());
    for language in languages_with_iso_639_1 {
        assert_eq!(language.to_639_1().is_some(), true);
    }
}

Structs

Enums