1use super::Locale;
14use super::message::{MessageEvaluator, Mf2MessageEvaluator, NoOpEvaluator};
15use super::raw;
16use super::types::{
17 ContributorTerm, DateTerms, LocaleOverride, LocatorTerm, MaybeGendered, MessageSyntax,
18 MonthNames, SimpleTerm, SingularPlural, TermForm,
19};
20use crate::citation::LocatorType;
21use crate::template::ContributorRole;
22use std::collections::HashMap;
23use std::sync::Arc;
24
25impl Locale {
26 pub fn from_yaml_str(yaml: &str) -> Result<Self, String> {
32 let raw: raw::RawLocale = serde_yaml::from_str(yaml)
33 .map_err(|e| format!("Failed to parse locale YAML: {}", e))?;
34
35 Ok(Self::from_raw(raw))
36 }
37
38 pub fn load(locale_id: &str, locales_dir: &std::path::Path) -> Self {
41 let extensions = ["yaml", "yml", "json", "cbor"];
42
43 for ext in &extensions {
44 let file_name = format!("{}.{}", locale_id, ext);
45 let file_path = locales_dir.join(&file_name);
46
47 if file_path.exists() {
48 match Self::from_file(&file_path) {
49 Ok(locale) => return locale,
50 Err(e) => {
51 eprintln!(
52 "Warning: Failed to load locale {}.{}: {}",
53 locale_id, ext, e
54 );
55 }
56 }
57 }
58 }
59
60 if locale_id.contains('-') {
61 let base = locale_id.split('-').next().unwrap_or("en");
62 if let Ok(entries) = std::fs::read_dir(locales_dir) {
63 for entry in entries.flatten() {
64 let name = entry.file_name();
65 let name_str = name.to_string_lossy();
66 if (name_str.starts_with(base)
67 && extensions.iter().any(|ext| name_str.ends_with(ext)))
68 && let Ok(locale) = Self::from_file(&entry.path())
69 {
70 return locale;
71 }
72 }
73 }
74 }
75
76 Self::en_us()
77 }
78
79 pub fn from_file(path: &std::path::Path) -> Result<Self, String> {
86 let bytes =
87 std::fs::read(path).map_err(|e| format!("Failed to read locale file: {}", e))?;
88 let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("yaml");
89
90 match ext {
91 "cbor" => ciborium::de::from_reader::<raw::RawLocale, _>(std::io::Cursor::new(&bytes))
92 .map(Self::from_raw)
93 .map_err(|e| format!("Failed to parse CBOR locale: {}", e)),
94 "json" => serde_json::from_slice::<raw::RawLocale>(&bytes)
95 .map(Self::from_raw)
96 .map_err(|e| format!("Failed to parse JSON locale: {}", e)),
97 _ => {
98 let content = String::from_utf8_lossy(&bytes);
99 Self::from_yaml_str(&content)
100 }
101 }
102 }
103
104 fn from_raw(raw: raw::RawLocale) -> Self {
116 Self::from_raw_with_base(raw, Locale::en_us())
117 }
118
119 #[allow(
129 clippy::too_many_lines,
130 reason = "Complex parsing of raw locale data with multiple term types"
131 )]
132 pub(super) fn from_raw_with_base(raw: raw::RawLocale, base: Self) -> Self {
133 let punctuation_in_quote = raw.locale.starts_with("en-US")
134 || (raw.locale.starts_with("en") && !raw.locale.starts_with("en-GB"));
135
136 let mut locale = base;
137 locale.locale = raw.locale.clone();
138 Self::remove_base_messages_shadowed_by_raw_terms(&raw, &mut locale.messages);
139 locale.dates = DateTerms {
140 months: MonthNames {
141 long: raw.dates.months.long,
142 short: raw.dates.months.short,
143 },
144 seasons: raw.dates.seasons,
145 uncertainty_term: raw.dates.uncertainty_term,
146 open_ended_term: raw.dates.open_ended_term,
147 am: raw.dates.am,
148 pm: raw.dates.pm,
149 timezone_utc: raw.dates.timezone_utc,
150 before_era: raw.dates.before_era,
151 ad: raw.dates.ad,
152 bc: raw.dates.bc,
153 bce: raw.dates.bce,
154 ce: raw.dates.ce,
155 };
156 locale.punctuation_in_quote = punctuation_in_quote;
157 locale.sort_articles = Self::default_articles_for_locale(&raw.locale);
158
159 locale.locale_schema_version = raw.locale_schema_version;
160 locale.evaluation = raw.evaluation.unwrap_or_default();
161 locale.messages.extend(raw.messages);
162 locale.date_formats.extend(raw.date_formats);
163 locale.legacy_term_aliases.extend(raw.legacy_term_aliases);
164
165 if let Some(raw_vocab) = raw.vocab {
166 locale.vocab.genre.extend(raw_vocab.genre);
167 locale.vocab.medium.extend(raw_vocab.medium);
168 }
169
170 if let Some(go) = raw.grammar_options {
171 locale.grammar_options = go;
172 } else {
173 locale.grammar_options.punctuation_in_quote = locale.punctuation_in_quote;
174 }
175 locale.punctuation_in_quote = locale.grammar_options.punctuation_in_quote;
176
177 if let Some(nf) = raw.number_formats {
178 locale.number_formats = nf;
179 }
180
181 let explicit_locator_keys: std::collections::HashSet<LocatorType> = raw
182 .locators
183 .keys()
184 .filter_map(|key| Self::parse_builtin_locator_type(key))
185 .collect();
186
187 for (key, value) in &raw.locators {
188 if let Some(locator_type) = Self::parse_locator_type(key) {
189 let locator_term = LocatorTerm {
190 long: Self::extract_singular_plural(value.long.as_ref().as_ref()),
191 short: Self::extract_singular_plural(value.short.as_ref().as_ref()),
192 symbol: Self::extract_singular_plural(value.symbol.as_ref().as_ref()),
193 gender: value.gender.clone(),
194 };
195 locale.locators.insert(locator_type, locator_term);
196 }
197 }
198
199 for (key, value) in &raw.terms {
200 if let Some(locator_type) = Self::parse_builtin_locator_type(key)
201 && !explicit_locator_keys.contains(&locator_type)
202 && let Some(forms) = Self::get_forms(value)
203 {
204 let locator_term = LocatorTerm {
205 long: Self::extract_singular_plural(forms.get("long").as_ref()),
206 short: Self::extract_singular_plural(forms.get("short").as_ref()),
207 symbol: Self::extract_singular_plural(forms.get("symbol").as_ref()),
208 gender: None,
209 };
210 locale.locators.insert(locator_type, locator_term);
211 continue;
212 }
213
214 match key.as_str() {
215 "and" => {
216 if let Some(forms) = Self::get_forms(value) {
217 if let Some(v) = forms.get("long").and_then(|v| v.as_string()) {
218 locale.terms.and = Some(v.to_string());
219 }
220 if let Some(v) = forms.get("symbol").and_then(|v| v.as_string()) {
221 locale.terms.and_symbol = Some(v.to_string());
222 }
223 }
224 }
225 "et_al" => {
226 if let Some(forms) = Self::get_forms(value)
227 && let Some(v) = forms.get("long").and_then(|v| v.as_string())
228 {
229 locale.terms.et_al = Some(v.to_string());
230 }
231 }
232 "and others" | "and_others" => {
233 if let Some(forms) = Self::get_forms(value)
234 && let Some(v) = forms.get("long").and_then(|v| v.as_string())
235 {
236 locale.terms.and_others = Some(v.to_string());
237 }
238 }
239 "accessed" => {
240 if let Some(forms) = Self::get_forms(value)
241 && let Some(v) = forms.get("long").and_then(|v| v.as_string())
242 {
243 locale.terms.accessed = Some(v.to_string());
244 }
245 }
246 "ibid" => {
247 if let Some(forms) = Self::get_forms(value)
248 && let Some(v) = forms.get("long").and_then(|v| v.as_string())
249 {
250 locale.terms.ibid = Some(v.to_string());
251 }
252 }
253 "no date" => {
254 let simple = Self::extract_simple_term_from_raw(value);
255 let short_fallback = simple.short.as_default_str().to_string();
256 locale
257 .terms
258 .general
259 .insert(super::types::GeneralTerm::NoDate, simple);
260 locale.terms.no_date.get_or_insert(short_fallback);
261 }
262 "no_date" => {
263 let simple = Self::extract_simple_term_from_raw(value);
264 locale.terms.no_date = Some(simple.short.as_str().to_string());
265 locale
266 .terms
267 .general
268 .entry(super::types::GeneralTerm::NoDate)
269 .or_insert(simple);
270 }
271 _ => {
272 if let Some(general_term) = Self::parse_general_term(key) {
273 let simple = Self::extract_simple_term_from_raw(value);
274 locale.terms.general.insert(general_term, simple);
275 } else {
276 let normalized = Self::normalize_term_key(key);
277 if Self::is_known_type_term_key(&normalized) {
278 let simple = Self::extract_simple_term_from_raw(value);
279 locale.type_terms.insert(normalized, simple);
280 }
281 }
282 }
283 }
284 }
285
286 for (key, role_term) in &raw.roles {
287 let contributor_term = ContributorTerm {
288 singular: Self::extract_simple_term(&role_term.long, &role_term.short, false),
289 plural: Self::extract_simple_term(&role_term.long, &role_term.short, true),
290 verb: Self::extract_verb_term(&role_term.verb, &role_term.verb_short),
291 };
292 if let Some(role) = Self::parse_role_name(key) {
293 locale.roles.insert(role, contributor_term);
294 } else {
295 let canonical = if key == "editortranslator" {
296 "editor-translator".to_string()
297 } else {
298 Self::normalize_term_key(key)
299 };
300 locale.role_combinations.insert(canonical, contributor_term);
301 }
302 }
303
304 locale.evaluator = match locale.evaluation.message_syntax {
305 MessageSyntax::Mf2 => Arc::new(Mf2MessageEvaluator) as Arc<dyn MessageEvaluator>,
306 MessageSyntax::Static => Arc::new(NoOpEvaluator),
307 };
308
309 locale
310 }
311
312 fn default_articles_for_locale(locale_id: &str) -> Vec<String> {
314 #[allow(clippy::string_slice, reason = "locale_id is expected to be ASCII")]
315 let lang = &locale_id[..2.min(locale_id.len())];
316 match lang {
317 "en" => vec!["the".into(), "a".into(), "an".into()],
318 "de" => vec![
319 "der".into(),
320 "die".into(),
321 "das".into(),
322 "ein".into(),
323 "eine".into(),
324 ],
325 "fr" => vec![
326 "le".into(),
327 "la".into(),
328 "les".into(),
329 "l'".into(),
330 "un".into(),
331 "une".into(),
332 ],
333 "es" => vec![
334 "el".into(),
335 "la".into(),
336 "los".into(),
337 "las".into(),
338 "un".into(),
339 "una".into(),
340 ],
341 "it" => vec![
342 "il".into(),
343 "lo".into(),
344 "la".into(),
345 "i".into(),
346 "gli".into(),
347 "le".into(),
348 "un".into(),
349 "una".into(),
350 ],
351 "pt" => vec![
352 "o".into(),
353 "a".into(),
354 "os".into(),
355 "as".into(),
356 "um".into(),
357 "uma".into(),
358 ],
359 "nl" => vec!["de".into(), "het".into(), "een".into()],
360 _ => vec![],
361 }
362 }
363
364 fn get_forms(value: &raw::RawTermValue) -> Option<&HashMap<String, raw::RawTermValue>> {
365 match value {
366 raw::RawTermValue::Forms(forms) => Some(forms),
367 _ => None,
368 }
369 }
370
371 fn parse_locator_type(name: &str) -> Option<LocatorType> {
372 LocatorType::from_key(name).ok()
373 }
374
375 fn parse_builtin_locator_type(name: &str) -> Option<LocatorType> {
376 match Self::parse_locator_type(name)? {
377 LocatorType::Custom(_) => None,
378 locator => Some(locator),
379 }
380 }
381
382 fn parse_role_name(name: &str) -> Option<ContributorRole> {
383 match name {
384 "author" => Some(ContributorRole::Author),
385 "chair" => Some(ContributorRole::Chair),
386 "editor" => Some(ContributorRole::Editor),
387 "translator" => Some(ContributorRole::Translator),
388 "director" => Some(ContributorRole::Director),
389 "compiler" => Some(ContributorRole::Composer),
390 "illustrator" => Some(ContributorRole::Illustrator),
391 "collection-editor" => Some(ContributorRole::CollectionEditor),
392 "container-author" => Some(ContributorRole::ContainerAuthor),
393 "editorial-director" => Some(ContributorRole::EditorialDirector),
394 "textual-editor" | "textual_editor" => Some(ContributorRole::TextualEditor),
395 "interviewer" => Some(ContributorRole::Interviewer),
396 "original-author" => Some(ContributorRole::OriginalAuthor),
397 "recipient" => Some(ContributorRole::Recipient),
398 "reviewed-author" => Some(ContributorRole::ReviewedAuthor),
399 "performer" => Some(ContributorRole::Performer),
400 "composer" => Some(ContributorRole::Composer),
401 "writer" => Some(ContributorRole::Writer),
402 "producer" => Some(ContributorRole::Producer),
403 _ => None,
404 }
405 }
406
407 fn remove_base_messages_shadowed_by_raw_terms(
408 raw: &raw::RawLocale,
409 messages: &mut HashMap<String, String>,
410 ) {
411 for key in raw.locators.keys() {
412 if let Some(locator) = Self::parse_builtin_locator_type(key) {
413 for form in [TermForm::Long, TermForm::Short] {
414 if let Some(message_id) = Self::locator_message_id(&locator, &form) {
415 messages.remove(message_id);
416 }
417 }
418 }
419 }
420
421 for key in raw.roles.keys() {
422 if let Some(role) = Self::parse_role_name(key) {
423 for form in [
424 TermForm::Long,
425 TermForm::Short,
426 TermForm::Verb,
427 TermForm::VerbShort,
428 ] {
429 if let Some(message_id) = Self::role_message_id(&role, &form) {
430 messages.remove(message_id);
431 }
432 }
433 }
434 }
435
436 for key in raw.terms.keys() {
437 if let Some(term) = Self::parse_general_term(key) {
438 for form in [TermForm::Long, TermForm::Short] {
439 if let Some(message_id) = Self::general_message_id(&term, &form) {
440 messages.remove(message_id);
441 }
442 }
443 }
444 }
445 }
446
447 fn extract_singular_plural(value: Option<&&raw::RawTermValue>) -> Option<SingularPlural> {
448 match value {
449 Some(raw::RawTermValue::SingularPlural { singular, plural }) => Some(SingularPlural {
450 singular: Self::from_raw_gendered_string(singular),
451 plural: Self::from_raw_gendered_string(plural),
452 }),
453 Some(raw::RawTermValue::Simple(s)) => Some(SingularPlural {
454 singular: MaybeGendered::Plain(s.clone()),
455 plural: MaybeGendered::Plain(s.clone()),
456 }),
457 Some(raw::RawTermValue::Gendered {
458 masculine,
459 feminine,
460 neuter,
461 common,
462 }) => Some(SingularPlural {
463 singular: MaybeGendered::Gendered {
464 masculine: masculine.clone(),
465 feminine: feminine.clone(),
466 neuter: neuter.clone(),
467 common: common.clone(),
468 },
469 plural: MaybeGendered::Gendered {
470 masculine: masculine.clone(),
471 feminine: feminine.clone(),
472 neuter: neuter.clone(),
473 common: common.clone(),
474 },
475 }),
476 Some(raw::RawTermValue::Forms(forms)) => {
477 let singular = forms
478 .get("singular")
479 .map(Self::extract_maybe_gendered_string);
480 let plural = forms.get("plural").map(Self::extract_maybe_gendered_string);
481
482 singular.map(|s| SingularPlural {
483 plural: plural.unwrap_or_else(|| s.clone()),
484 singular: s,
485 })
486 }
487 _ => None,
488 }
489 }
490
491 fn extract_simple_term(
492 long: &Option<raw::RawTermValue>,
493 short: &Option<raw::RawTermValue>,
494 plural: bool,
495 ) -> SimpleTerm {
496 let long_str = long
497 .as_ref()
498 .map(|v| Self::extract_simple_gendered_term(v, plural))
499 .unwrap_or_default();
500
501 let short_str = short
502 .as_ref()
503 .map(|v| Self::extract_simple_gendered_term(v, plural))
504 .unwrap_or_default();
505
506 SimpleTerm {
507 long: long_str,
508 short: short_str,
509 }
510 }
511
512 fn extract_verb_term(
513 verb: &Option<raw::RawTermValue>,
514 verb_short: &Option<raw::RawTermValue>,
515 ) -> SimpleTerm {
516 let long_str = verb
517 .as_ref()
518 .and_then(|v| v.as_string())
519 .unwrap_or("")
520 .into();
521
522 let short_str = verb_short
523 .as_ref()
524 .and_then(|v| v.as_string())
525 .unwrap_or("")
526 .into();
527
528 SimpleTerm {
529 long: long_str,
530 short: short_str,
531 }
532 }
533
534 fn normalize_term_key(s: &str) -> String {
542 s.replace(['_', ' '], "-")
543 }
544
545 pub fn parse_general_term(name: &str) -> Option<super::types::GeneralTerm> {
547 use super::types::GeneralTerm;
548 match Self::normalize_term_key(name).as_str() {
549 "in" => Some(GeneralTerm::In),
550 "accessed" => Some(GeneralTerm::Accessed),
551 "cited" => Some(GeneralTerm::Cited),
552 "retrieved" => Some(GeneralTerm::Retrieved),
553 "at" => Some(GeneralTerm::At),
554 "from" => Some(GeneralTerm::From),
555 "of" => Some(GeneralTerm::Of),
556 "to" => Some(GeneralTerm::To),
557 "by" => Some(GeneralTerm::By),
558 "no-date" => Some(GeneralTerm::NoDate),
559 "anonymous" => Some(GeneralTerm::Anonymous),
560 "circa" => Some(GeneralTerm::Circa),
561 "available-at" => Some(GeneralTerm::AvailableAt),
562 "ibid" => Some(GeneralTerm::Ibid),
563 "and" => Some(GeneralTerm::And),
564 "role-conjunction" => Some(GeneralTerm::RoleConjunction),
565 "et-al" => Some(GeneralTerm::EtAl),
566 "and-others" => Some(GeneralTerm::AndOthers),
567 "forthcoming" => Some(GeneralTerm::Forthcoming),
568 "online" => Some(GeneralTerm::Online),
569 "here" => Some(GeneralTerm::Here),
570 "deposited" => Some(GeneralTerm::Deposited),
571 "review-of" => Some(GeneralTerm::ReviewOf),
572 "original-work-published" => Some(GeneralTerm::OriginalWorkPublished),
573 "personal-communication" => Some(GeneralTerm::PersonalCommunication),
574 "patent" => Some(GeneralTerm::Patent),
575 "issued" => Some(GeneralTerm::Issued),
576 "volume" => Some(GeneralTerm::Volume),
577 "issue" => Some(GeneralTerm::Issue),
578 "page" => Some(GeneralTerm::Page),
579 "chapter" => Some(GeneralTerm::Chapter),
580 "edition" => Some(GeneralTerm::Edition),
581 "section" => Some(GeneralTerm::Section),
582 "version" => Some(GeneralTerm::Version),
583 _ => None,
584 }
585 }
586
587 fn is_known_type_term_key(normalized_key: &str) -> bool {
613 const KNOWN_TYPE_TERM_KEYS: &[&str] = &[
614 "article-journal",
615 "article-magazine",
616 "article-newspaper",
617 "broadcast",
618 "classic",
619 "collection",
620 "dataset",
621 "document",
622 "entry",
623 "entry-dictionary",
624 "entry-encyclopedia",
625 "event",
626 "graphic",
627 "hearing",
628 "interview",
629 "legal-case",
630 "legislation",
631 "manuscript",
632 "map",
633 "motion-picture",
634 "musical-score",
635 "pamphlet",
636 "paper-conference",
637 "performance",
638 "periodical",
639 "personal-communication",
640 "post",
641 "post-weblog",
642 "preprint",
643 "regulation",
644 "report",
645 "review",
646 "review-book",
647 "software",
648 "song",
649 "speech",
650 "standard",
651 "thesis",
652 "treaty",
653 "webpage",
654 ];
655 KNOWN_TYPE_TERM_KEYS.contains(&normalized_key)
656 }
657
658 fn extract_simple_term_from_raw(value: &raw::RawTermValue) -> SimpleTerm {
659 match value {
660 raw::RawTermValue::Simple(s) => SimpleTerm {
661 long: s.clone().into(),
662 short: s.clone().into(),
663 },
664 raw::RawTermValue::Gendered {
665 masculine,
666 feminine,
667 neuter,
668 common,
669 } => SimpleTerm {
670 long: MaybeGendered::Gendered {
671 masculine: masculine.clone(),
672 feminine: feminine.clone(),
673 neuter: neuter.clone(),
674 common: common.clone(),
675 },
676 short: MaybeGendered::Gendered {
677 masculine: masculine.clone(),
678 feminine: feminine.clone(),
679 neuter: neuter.clone(),
680 common: common.clone(),
681 },
682 },
683 raw::RawTermValue::Forms(forms) => {
684 let long = forms
685 .get("long")
686 .map(Self::extract_maybe_gendered_string)
687 .unwrap_or_default();
688 let short = forms
689 .get("short")
690 .map(Self::extract_maybe_gendered_string)
691 .unwrap_or_else(|| long.clone());
692 SimpleTerm { long, short }
693 }
694 raw::RawTermValue::SingularPlural { singular, .. } => SimpleTerm {
695 long: Self::from_raw_gendered_string(singular),
696 short: Self::from_raw_gendered_string(singular),
697 },
698 }
699 }
700
701 fn from_raw_gendered_string(value: &raw::RawGenderedString) -> MaybeGendered<String> {
702 match value {
703 raw::RawGenderedString::Simple(value) => MaybeGendered::Plain(value.clone()),
704 raw::RawGenderedString::Gendered {
705 masculine,
706 feminine,
707 neuter,
708 common,
709 } => MaybeGendered::Gendered {
710 masculine: masculine.clone(),
711 feminine: feminine.clone(),
712 neuter: neuter.clone(),
713 common: common.clone(),
714 },
715 }
716 }
717
718 fn extract_maybe_gendered_string(value: &raw::RawTermValue) -> MaybeGendered<String> {
719 match value {
720 raw::RawTermValue::Simple(value) => MaybeGendered::Plain(value.clone()),
721 raw::RawTermValue::Gendered {
722 masculine,
723 feminine,
724 neuter,
725 common,
726 } => MaybeGendered::Gendered {
727 masculine: masculine.clone(),
728 feminine: feminine.clone(),
729 neuter: neuter.clone(),
730 common: common.clone(),
731 },
732 raw::RawTermValue::SingularPlural { singular, .. } => {
733 Self::from_raw_gendered_string(singular)
734 }
735 raw::RawTermValue::Forms(forms) => forms
736 .get("long")
737 .or_else(|| forms.get("singular"))
738 .map(Self::extract_maybe_gendered_string)
739 .unwrap_or_default(),
740 }
741 }
742
743 fn extract_simple_gendered_term(
744 value: &raw::RawTermValue,
745 plural: bool,
746 ) -> MaybeGendered<String> {
747 match value {
748 raw::RawTermValue::Simple(value) => MaybeGendered::Plain(value.clone()),
749 raw::RawTermValue::Gendered {
750 masculine,
751 feminine,
752 neuter,
753 common,
754 } => MaybeGendered::Gendered {
755 masculine: masculine.clone(),
756 feminine: feminine.clone(),
757 neuter: neuter.clone(),
758 common: common.clone(),
759 },
760 raw::RawTermValue::SingularPlural {
761 singular,
762 plural: plural_value,
763 } => {
764 if plural {
765 Self::from_raw_gendered_string(plural_value)
766 } else {
767 Self::from_raw_gendered_string(singular)
768 }
769 }
770 raw::RawTermValue::Forms(forms) => {
771 let key = if plural { "plural" } else { "singular" };
772 forms
773 .get(key)
774 .or_else(|| forms.get("long"))
775 .map(Self::extract_maybe_gendered_string)
776 .unwrap_or_default()
777 }
778 }
779 }
780
781 pub fn apply_override(&mut self, ov: &LocaleOverride) {
789 for (k, v) in &ov.messages {
790 self.messages.insert(k.clone(), v.clone());
791 }
792 if let Some(go) = &ov.grammar_options {
793 self.grammar_options = go.clone();
794 self.punctuation_in_quote = go.punctuation_in_quote;
795 }
796 for (k, v) in &ov.legacy_term_aliases {
797 self.legacy_term_aliases.insert(k.clone(), v.clone());
798 }
799 }
800}