[][src]Module locale_types::locale

Provides a layer above the LocaleString for different locale specifiers.

The Locale enum represents three forms of locale specification supported by the POSIX C API. These are:

  1. The identifier "POSIX", or "C" is known as the minimal locale. It is a rather neutral locale which has the same settings across all systems and compilers, and therefore the exact results of a program using this locale are predictable. This is the locale used by default on all C programs.
  2. A path, starting with the '/' character and which resolves to a directory containing the POSIX definition of a locale.
  3. A locale string, represented in this crate as a LocaleString structure that effectively represents a language with cultural qualification.

The Locale::from_str method can be used to parse any of these kinds into the separate enumeration values.

Examples

use locale_types::{Locale,LocaleIdentifier};
use std::str::FromStr;

match Locale::from_str("C") {
    Ok(Locale::POSIX) => (),
    _ => panic!("could not parse first locale string")
}

let locale = Locale::from_str("en_US.UTF-8@Latn");
if let Ok(Locale::String(locale_str)) = locale {
    assert_eq!(locale_str.language_code(), "en".to_string());
    assert_eq!(locale_str.territory().unwrap(), "US".to_string());
    assert_eq!(locale_str.code_set().unwrap(), "UTF-8".to_string());
    assert_eq!(locale_str.modifier().unwrap(), "Latn".to_string());
} else {
    panic!("could not parse second locale string")
}

The following example is a more complete use case, the need to parse the commonly used LC_ALL environment variable to determine it's type and potential components.

use locale_types::Locale;
use std::str::FromStr;
use std::env;

if let Ok(lc_str) = env::var("LC_ALL") {
    match Locale::from_str(&lc_str) {
        Ok(Locale::POSIX) =>
            println!("POSIX minimal locale"),
        Ok(Locale::Path(p)) =>
            println!("Path locale"),
        Ok(Locale::String(s)) =>
            println!("String locale"),
        _ => panic!("Parse Error"),
    }
}

Enums

Locale

This enumeration represents the three types of Locale specifiers commonly used by operating systems.