From bdfd54877996e21b84dfc36733dfa9420938d944 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 9 Oct 2019 10:02:49 +0200 Subject: [PATCH] error out if %1 %2 replacements are not contained in default english version --- src/stock.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/stock.rs b/src/stock.rs index 347bb949e..5e0004ba8 100644 --- a/src/stock.rs +++ b/src/stock.rs @@ -129,8 +129,21 @@ impl Context { id: StockMessage, stockstring: String, ) -> Result<(), Error> { - self.translated_stockstrings - .insert(id as usize, stockstring); + if stockstring.contains("%1") && !id.fallback().contains("%1") { + bail!( + "translation {} contains invalid %1 placeholder, default is {}", + stockstring, + id.fallback() + ); + } + if stockstring.contains("%2") && !id.fallback().contains("%2") { + bail!( + "translation {} contains invalid %2 placeholder, default is {}", + stockstring, + id.fallback() + ); + } + self.translated_stockstrings.insert(id as usize, stockstring); Ok(()) } @@ -268,6 +281,19 @@ mod tests { assert_eq!(t.ctx.stock_str(StockMessage::NoMessages), "xyz") } + #[test] + fn test_set_stock_translation_wrong_replacements() { + let mut t = dummy_context(); + assert!(t + .ctx + .set_stock_translation(StockMessage::NoMessages, "xyz %1$s ".to_string()) + .is_err()); + assert!(t + .ctx + .set_stock_translation(StockMessage::NoMessages, "xyz %2$s ".to_string()) + .is_err()); + } + #[test] fn test_stock_str() { let t = dummy_context();