icu_provider_source/locale/
parents.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::cldr_serde;
use crate::SourceDataProvider;

use icu::locale::provider::*;
use icu::locale::subtags::{script, Language, Region, Script};
use icu_provider::prelude::*;
use potential_utf::PotentialUtf8;
use std::collections::{BTreeMap, HashSet};
use writeable::Writeable;

impl DataProvider<ParentsV1Marker> for SourceDataProvider {
    fn load(&self, req: DataRequest) -> Result<DataResponse<ParentsV1Marker>, DataError> {
        self.check_req::<ParentsV1Marker>(req)?;
        let parents_data: &cldr_serde::parent_locales::Resource = self
            .cldr()?
            .core()
            .read_and_parse("supplemental/parentLocales.json")?;

        let metadata = DataResponseMetadata::default();
        Ok(DataResponse {
            metadata,
            payload: DataPayload::from_owned(parents_data.into()),
        })
    }
}

impl crate::IterableDataProviderCached<ParentsV1Marker> for SourceDataProvider {
    fn iter_ids_cached(&self) -> Result<HashSet<DataIdentifierCow<'static>>, DataError> {
        Ok(HashSet::from_iter([Default::default()]))
    }
}

impl From<&cldr_serde::parent_locales::Resource> for ParentsV1<'static> {
    fn from(source_data: &cldr_serde::parent_locales::Resource) -> Self {
        let mut parents = BTreeMap::<_, (Language, Option<Script>, Option<Region>)>::new();

        for (source, target) in source_data.supplemental.parent_locales.parent_locale.iter() {
            assert!(!source.language.is_default());
            if source.script.is_some() && source.region.is_none() && target.is_default() {
                // We always fall back from language-script to und
                continue;
            }
            parents.insert(source.write_to_string(), target.into());
        }

        parents.insert(
            "und-Hant".into(),
            (Language::UND, Some(script!("Hani")), None),
        );
        parents.insert(
            "und-Hans".into(),
            (Language::UND, Some(script!("Hani")), None),
        );

        ParentsV1 {
            parents: parents
                .iter()
                .map(|(k, v)| (<&PotentialUtf8>::from(k.as_ref()), v))
                .collect(),
        }
    }
}

#[test]
fn test_basic() {
    use icu::locale::{langid, LanguageIdentifier};

    let provider = SourceDataProvider::new_testing();

    let parents: DataResponse<ParentsV1Marker> = provider.load(Default::default()).unwrap();

    assert_eq!(
        parents
            .payload
            .get()
            .parents
            .get_copied("zh-Hant-MO".into())
            .map(LanguageIdentifier::from),
        Some(langid!("zh-Hant-HK"))
    );
}