icu_experimental/personnames/specifications/
derive_name_order.rsuse icu_locale::fallback::LocaleFallbackerBorrowed;
use icu_locale_core::subtags::Language;
use icu_locale_core::Locale;
use writeable::Writeable;
use zerovec::VarZeroVec;
use crate::personnames::api::FormattingOrder;
pub fn name_order_derive(
person_name_locale: &Locale,
surname_first: &VarZeroVec<str>,
given_first: &VarZeroVec<str>,
fallbacker: LocaleFallbackerBorrowed,
) -> FormattingOrder {
let mut fallback_iterator = fallbacker
.for_config(Default::default())
.fallback_for(person_name_locale.into());
loop {
let chain_locale = fallback_iterator.get();
let chain_locale_str = chain_locale.write_to_string();
let mut chain_locale_und = chain_locale.clone();
chain_locale_und.language = Language::UND;
let chain_locale_und_str = chain_locale_und.write_to_string();
if given_first
.iter()
.any(|i| i == chain_locale_str || i == chain_locale_und_str)
{
return FormattingOrder::GivenFirst;
}
if surname_first
.iter()
.any(|i| i == chain_locale_str || i == chain_locale_und_str)
{
return FormattingOrder::SurnameFirst;
}
fallback_iterator.step();
}
}
#[cfg(test)]
mod tests {
use icu_locale::LocaleFallbacker;
use icu_locale_core::locale;
use zerovec::VarZeroVec;
use super::name_order_derive;
use crate::personnames::api::FormattingOrder;
#[test]
fn test_plain_locale() {
let fallbacker = LocaleFallbacker::new();
let given_first = VarZeroVec::from(&["und"]);
let surname_first = VarZeroVec::from(&["hu", "ja", "km", "ko", "mn", "vi", "yue", "zh"]);
assert_eq!(
name_order_derive(
&locale!("de_Latn_ch"),
&surname_first,
&given_first,
fallbacker
),
FormattingOrder::GivenFirst,
"failed for de_Latn_ch"
);
assert_eq!(
name_order_derive(
&locale!("ja_Jpan_jp"),
&surname_first,
&given_first,
fallbacker
),
FormattingOrder::GivenFirst,
"failed for ja_Jpan_jp"
);
}
#[test]
fn test_mixed_und() {
let fallbacker = LocaleFallbacker::new();
let given_first = VarZeroVec::from(&["und"]);
let surname_first = VarZeroVec::from(&[
"zh", "ja", "und-CN", "und-TW", "und-SG", "und-HK", "und-MO", "und-HU", "und-JP",
]);
assert_eq!(
name_order_derive(
&locale!("en_Latn_SG"),
&surname_first,
&given_first,
fallbacker
),
FormattingOrder::SurnameFirst,
"failed for en_Latn_SG"
);
assert_eq!(
name_order_derive(
&locale!("zh_Hans_CN"),
&surname_first,
&given_first,
fallbacker
),
FormattingOrder::SurnameFirst,
"failed for zh_Hans_CN"
);
assert_eq!(
name_order_derive(
&locale!("zh_Hans"),
&surname_first,
&given_first,
fallbacker
),
FormattingOrder::GivenFirst,
"failed for zh_Hans"
);
}
}