Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First draft for implementing Local Displayname Algorithm #3587

Merged
merged 14 commits into from
Aug 23, 2023
98 changes: 98 additions & 0 deletions components/locid/src/langid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,101 @@ impl From<&LanguageIdentifier>
(langid.language, langid.script, langid.region)
}
}

/// Convert from an LS tuple to a [`LanguageIdentifier`].
///
/// # Examples
///
/// ```
/// use icu::locid::{
/// langid, subtags_language as language,
/// subtags_script as script, LanguageIdentifier,
/// };
///
/// let lang = language!("en");
/// let script = script!("Latn");
/// assert_eq!(
/// LanguageIdentifier::from((lang, script)),
robertbastian marked this conversation as resolved.
Show resolved Hide resolved
/// langid!("en-Latn")
/// );
/// ```
impl From<(subtags::Language, subtags::Script)> for LanguageIdentifier {
fn from(lsr: (subtags::Language, subtags::Script)) -> Self {
Self {
language: lsr.0,
script: Some(lsr.1),
..Default::default()
}
}
}

/// Convert from a [`LanguageIdentifier`] to an LS tuple.
///
/// # Examples
///
/// ```
/// use icu::locid::{
/// langid, subtags_language as language,
/// subtags_script as script,
/// };
///
/// let lid = langid!("en-Latn");
/// let (lang, script) = (&lid).into();
///
/// assert_eq!(lang, language!("en"));
/// assert_eq!(script, script!("Latn"));
/// ```
impl From<&LanguageIdentifier> for (subtags::Language, subtags::Script) {
fn from(langid: &LanguageIdentifier) -> Self {
(langid.language, langid.script.unwrap())
}
}

/// Convert from an LR tuple to a [`LanguageIdentifier`].
///
/// # Examples
///
/// ```
/// use icu::locid::{
/// langid, subtags_language as language,
/// subtags_region as region, LanguageIdentifier,
/// };
///
/// let lang = language!("en");
/// let region = region!("GB");
/// assert_eq!(
/// LanguageIdentifier::from((lang, region)),
/// langid!("en-GB")
/// );
/// ```
impl From<(subtags::Language, subtags::Region)> for LanguageIdentifier {
fn from(lsr: (subtags::Language, subtags::Region)) -> Self {
Self {
language: lsr.0,
region: Some(lsr.1),
..Default::default()
}
}
}

/// Convert from a [`LanguageIdentifier`] to an LR tuple.
///
/// # Examples
///
/// ```
/// use icu::locid::{
/// langid, subtags_language as language,
/// subtags_region as region,
/// };
///
/// let lid = langid!("en-GB");
/// let (lang, region) = (&lid).into();
///
/// assert_eq!(lang, language!("en"));
/// assert_eq!(region, region!("GB"));
/// ```
impl From<&LanguageIdentifier> for (subtags::Language, subtags::Region) {
fn from(langid: &LanguageIdentifier) -> Self {
(langid.language, langid.region.unwrap())
}
}
4 changes: 4 additions & 0 deletions experimental/displaynames/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ icu_testdata = { path = "../../provider/testdata", default-features = false, fea
std = ["icu_collections/std", "icu_locid/std", "icu_provider/std"]
serde = ["dep:serde", "zerovec/serde", "icu_collections/serde", "tinystr/serde", "icu_provider/serde"]
datagen = ["serde", "std", "dep:databake", "zerovec/databake", "icu_collections/databake", "tinystr/databake"]

[[test]]
name = "tests"
path = "tests/tests.rs"
snktd marked this conversation as resolved.
Show resolved Hide resolved
Loading