From a9ab7084945e1745547dfc54ed546105fc141146 Mon Sep 17 00:00:00 2001 From: Daniel Rainer Date: Mon, 2 Feb 2026 02:39:07 +0100 Subject: [PATCH] gettext: enforce trimmed msgids Now that we have trimmed our msgids, add an assertion to ensure that they stay trimmed. Note that we don't check msgstrs. We could do so when building the maps which get put into the executable, but there we also include messages originating from fish scripts, and there we don't enforce trimmed messages, so limiting the checks to only messages originating from the Rust code there would not be trivial. Closes #12405 --- crates/gettext-extraction/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/gettext-extraction/src/lib.rs b/crates/gettext-extraction/src/lib.rs index 4b2091e96..0be26dd11 100644 --- a/crates/gettext-extraction/src/lib.rs +++ b/crates/gettext-extraction/src/lib.rs @@ -51,6 +51,14 @@ fn write_po_entry_to_file(message: &TokenStream, dir: &OsString) { !message_string.contains('\n'), "Gettext strings may not contain unescaped newlines. Unescaped newline found in '{message_string}'" ); + let msgid_without_quotes = &message_string[1..(message_string.len() - 1)]; + // We don't want leading or trailing whitespace in our messages. + let trimmed_msgid = msgid_without_quotes.trim(); + assert_eq!(msgid_without_quotes, trimmed_msgid); + assert!(!trimmed_msgid.starts_with("\\n")); + assert!(!trimmed_msgid.ends_with("\\n")); + assert!(!trimmed_msgid.starts_with("\\t")); + assert!(!trimmed_msgid.ends_with("\\t")); // Crude check for format strings. This might result in false positives. let format_string_annotation = if message_string.contains('%') { "#, c-format\n"