icu_provider_source/locale/
parents.rsuse 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() {
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"))
);
}