icu_experimental/personnames/specifications/
derive_locale.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// 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 icu_locale_core::subtags::script;
use icu_locale_core::{subtags, Locale};
use icu_properties::props::Script;
use icu_properties::{script::ScriptWithExtensionsBorrowed, PropertyNamesShortBorrowed};

use crate::personnames::api::NameFieldKind::{Given, Surname};
use crate::personnames::api::{NameFieldKind, PersonName, PersonNamesFormatterError};

/// Override the formatting payload to use based on specification rules.
///
/// if name locale and formatting locale are incompatible, name locale takes precedence
/// it should dynamically load the name locale formatter using the data_provider given in constructor.
/// https://www.unicode.org/reports/tr35/tr35-personNames.html#switch-the-formatting-locale-if-necessary
///
/// The formatter locale and name locale must be maximized first.
pub fn effective_locale<'a>(
    formatter_locale: &'a Locale,
    person_name_locale: &'a Locale,
) -> Result<&'a Locale, PersonNamesFormatterError> {
    let name_script = person_name_locale.id.script.unwrap();
    let formatter_script = formatter_locale.id.script.unwrap();
    if !compatible_scripts(name_script, formatter_script) {
        return Ok(person_name_locale);
    }
    Ok(formatter_locale)
}

// TODO: proper handling of compatible scripts.
fn compatible_scripts(sc1: subtags::Script, sc2: subtags::Script) -> bool {
    let jpan_compatible = [script!("Hani"), script!("Kana"), script!("Hira")];
    if sc1 == script!("Jpan") && jpan_compatible.contains(&sc2) {
        return true;
    }
    if sc2 == script!("Jpan") && jpan_compatible.contains(&sc1) {
        return true;
    }
    sc1 == sc2
}

/// https://www.unicode.org/reports/tr35/tr35-personNames.html#derive-the-name-locale
pub fn likely_person_name_locale<N>(
    person_name: &N,
    swe: ScriptWithExtensionsBorrowed,
    scripts: PropertyNamesShortBorrowed<Script>,
) -> Result<Locale, PersonNamesFormatterError>
where
    N: PersonName,
{
    let mut found_name_script = find_script(person_name, swe, Surname);
    if found_name_script.is_none() {
        found_name_script = find_script(person_name, swe, Given);
    }
    let name_script = found_name_script.unwrap_or(icu_properties::props::Script::Unknown);

    let locid_script = scripts.get_locale_script(name_script).unwrap();
    person_name.name_locale().map_or_else(
        || {
            let mut effective_locale = Locale::default();
            effective_locale.id.script = Some(locid_script);
            Ok(effective_locale)
        },
        |locale| {
            let mut effective_locale = locale.clone();
            effective_locale.id.script = Some(locid_script);
            Ok(effective_locale)
        },
    )
}

fn find_script<N>(
    person_name: &N,
    swe: ScriptWithExtensionsBorrowed,
    kind: NameFieldKind,
) -> Option<icu_properties::props::Script>
where
    N: PersonName,
{
    person_name
        .available_name_fields()
        .iter()
        .filter(|&name_field| name_field.kind == kind)
        .find_map(|&name_field| {
            person_name.get(name_field).chars().find_map(|c| {
                let char_script = swe.get_script_val(c);
                match char_script {
                    Script::Common | Script::Unknown | Script::Inherited => None,
                    _ => Some(char_script),
                }
            })
        })
}

#[cfg(test)]
mod tests {
    use icu_locale::LocaleExpander;
    use icu_locale_core::locale;
    use litemap::LiteMap;

    use super::{effective_locale, likely_person_name_locale};
    use crate::personnames::api::{
        FieldModifierSet, NameField, NameFieldKind, PersonNamesFormatterError,
    };
    use crate::personnames::provided_struct::DefaultPersonName;

    #[test]
    fn test_effective_locale_matching_script() {
        let lc = LocaleExpander::new_extended();
        let mut locale = locale!("fr");
        lc.maximize(&mut locale.id);
        assert_eq!(
            effective_locale(&locale!("de_Latn_ch"), &locale),
            Ok(&locale!("de_Latn_ch"))
        );
        assert_eq!(
            effective_locale(&locale, &locale!("de_Latn_ch")),
            Ok(&locale!("fr_Latn_FR"))
        );
    }

    #[test]
    fn test_effective_locale_non_matching_script() {
        let lc = LocaleExpander::new_extended();
        let mut locale = locale!("ja");
        lc.maximize(&mut locale.id);
        assert_eq!(
            effective_locale(&locale!("de_Latn_ch"), &locale),
            Ok(&locale!("ja-Jpan-JP"))
        );
        assert_eq!(
            effective_locale(&locale, &locale!("de_Latn_ch")),
            Ok(&locale!("de-Latn-CH"))
        );
    }

    #[test]
    fn test_effective_locale_compatible_script() {
        let lc = LocaleExpander::new_extended();
        let mut locale = locale!("ja");
        lc.maximize(&mut locale.id);
        assert_eq!(
            effective_locale(&locale!("ja_Hani_JP"), &locale),
            Ok(&locale!("ja_Hani_JP"))
        );
        assert_eq!(
            effective_locale(&locale!("ja_Kana_JP"), &locale),
            Ok(&locale!("ja-Kana-JP"))
        );
        assert_eq!(
            effective_locale(&locale!("ja_Hira_JP"), &locale),
            Ok(&locale!("ja-Hira-JP"))
        );
        assert_eq!(
            effective_locale(&locale, &locale!("ja_Hani_JP")),
            Ok(&locale!("ja-Jpan-JP"))
        );
        assert_eq!(
            effective_locale(&locale, &locale!("ja_Kana_JP")),
            Ok(&locale!("ja-Jpan-JP"))
        );
        assert_eq!(
            effective_locale(&locale, &locale!("ja_Hira_JP")),
            Ok(&locale!("ja-Jpan-JP"))
        );
    }

    #[test]
    fn test_likely_person_names_locale() {
        let swe = icu_properties::script::ScriptWithExtensions::new();
        let scripts = icu_properties::PropertyNamesShort::<icu_properties::props::Script>::new();
        assert_eq!(
            likely_person_name_locale(&person_name("Miyazaki", "Hayao").unwrap(), swe, scripts),
            Ok(locale!("und_Latn"))
        );
        assert_eq!(
            likely_person_name_locale(&person_name("駿", "宮崎").unwrap(), swe, scripts),
            Ok(locale!("und_Hani"))
        );
        assert_eq!(
            likely_person_name_locale(&person_name("하야오", "미야자키").unwrap(), swe, scripts),
            Ok(locale!("und_Hang"))
        );
        assert_eq!(
            likely_person_name_locale(
                &person_name("アルベルト", "アインシュタイン").unwrap(),
                swe,
                scripts
            ),
            Ok(locale!("und_Kana"))
        );
    }

    fn person_name(
        surname: &str,
        first_name: &str,
    ) -> Result<DefaultPersonName, PersonNamesFormatterError> {
        let mut person_data: LiteMap<NameField, String> = LiteMap::new();
        person_data.insert(
            NameField {
                kind: NameFieldKind::Surname,
                modifier: FieldModifierSet::default(),
            },
            String::from(surname),
        );
        person_data.insert(
            NameField {
                kind: NameFieldKind::Given,
                modifier: FieldModifierSet::default(),
            },
            String::from(first_name),
        );

        DefaultPersonName::new(person_data, None, None)
    }
}