diff --git a/crates/i18n/src/catalog.rs b/crates/i18n/src/catalog.rs index 3d27adadb..b9b360c65 100644 --- a/crates/i18n/src/catalog.rs +++ b/crates/i18n/src/catalog.rs @@ -4,6 +4,8 @@ use std::collections::HashSet; +use crate::message::Template; + /// A row that passed, with its columns unescaped. pub(crate) struct Entry { pub context: String, @@ -52,6 +54,10 @@ pub(crate) fn parse_row(text: &str, forms: usize) -> Result { match row.context.as_str() { "@id" => {} "@plural" => check_plural(&row.source, &row.translation, forms)?, + "@msgplural" => { + check_plural(&row.source, &row.translation, forms)?; + row.source.split('|').try_for_each(check_matchable)?; + } c if c.starts_with('@') && !RESERVED.contains(&c) => { return Err(format!("unknown reserved context {c:?}")); } @@ -62,11 +68,20 @@ pub(crate) fn parse_row(text: &str, forms: usize) -> Result { if row.source.ends_with('…') != row.translation.ends_with('…') { return Err("trailing … differs from the source text".into()); } + if row.context == "@msg" { + check_matchable(&row.source)?; + } } } Ok(row) } +/// A message is matched against its English text, so a template with no literal text or two +/// placeholders in a row (where one ends and the next begins is unknowable) could never be used. +fn check_matchable(english: &str) -> Result<(), String> { + Template::parse(english, Vec::new(), false).map(|_| ()).ok_or_else(|| "unmatchable message template".to_string()) +} + fn check_plural(source: &str, translation: &str, forms: usize) -> Result<(), String> { let english: Vec<&str> = source.split('|').collect(); let [one, other] = english.as_slice() else { diff --git a/crates/i18n/src/lib.rs b/crates/i18n/src/lib.rs index 8e7a65727..b58041025 100644 --- a/crates/i18n/src/lib.rs +++ b/crates/i18n/src/lib.rs @@ -36,6 +36,7 @@ use negotiation::{bare, candidates, is_region, marked, plural_rules}; pub use bidi::{isolate, ltr, strip_isolates}; pub use message::MessageMatch; +use message::{KEPT_VALUE, MAX_MESSAGE_LEN, MESSAGE_DEPTH, Template, insert_template, sort_templates}; /// The CLDR number, plural and date data for every CLDR locale, from `data/generate.sh`. mod data { @@ -131,26 +132,76 @@ struct Locale { /// A locale's rows, app over shared, and what was wrong with the catalogs it came from. struct Parsed { rows: HashMap>, + /// The `@msg` rows without placeholders: the English message, its `{{` and `}}` read as `{` + /// and `}`, → its translation as written. + exact: HashMap, + /// The `@msg` rows with placeholders and each English form of a `@msgplural` row, most + /// specific first. + templates: Vec