From d64e143297b603ced74ea46a5819b6d75de98df5 Mon Sep 17 00:00:00 2001 From: Frans de Jonge Date: Mon, 3 Feb 2020 12:11:07 +0100 Subject: [PATCH] [i18n] GetText: ignore fuzzy strings (#5807) Strings are prefilled by msgmerge with closely matching ones to reduce the amount of labor required by the translator. Often the string requires only minor adjustment, making the process faster, and when the string isn't a good match it's no big deal. However, these so-called fuzzy strings shouldn't be treated as if they were actually translated. Fixes the effect seen in . --- frontend/gettext.lua | 8 +++++++- spec/unit/gettext_spec.lua | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/frontend/gettext.lua b/frontend/gettext.lua index 5fdef9306..e2359fcf0 100644 --- a/frontend/gettext.lua +++ b/frontend/gettext.lua @@ -188,6 +188,7 @@ function GetText_mt.__index.changeLang(new_lang) end local data = {} + local fuzzy = false local headers local what = nil while true do @@ -257,13 +258,18 @@ function GetText_mt.__index.changeLang(new_lang) -- string continuation s = line:match("^%s*\"(.*)\"%s*$") end - if what and s then + if what and s and not fuzzy then -- unescape \n or msgid won't match s = s:gsub("\\n", "\n") -- unescape " or msgid won't match s = s:gsub('\\"', '"') data[what] = (data[what] or "") .. s + else + -- Don't save this fuzzy string and unset fuzzy for the next one. + fuzzy = false end + elseif line:match("#, fuzzy") then + fuzzy = true end end end diff --git a/spec/unit/gettext_spec.lua b/spec/unit/gettext_spec.lua index 6d699383c..bec1d4eb6 100644 --- a/spec/unit/gettext_spec.lua +++ b/spec/unit/gettext_spec.lua @@ -97,6 +97,11 @@ msgstr[2] "Pagina's context 2 plural 2" msgstr[3] "" msgstr[4] "" msgstr[5] "" + +#: frontend/ui/data/css_tweaks.lua:50 +#, fuzzy +msgid "Fuzzy" +msgstr "Fuzzy translated" ]] describe("GetText module", function() @@ -210,6 +215,9 @@ describe("GetText module", function() describe("language with standard plurals", function() GetText.changeLang("nl_NL") + it("gettext should ignore fuzzy strings", function() + assert.is_equal("Fuzzy", GetText("Fuzzy")) + end) it("gettext should translate multiline string", function() assert.is_equal("\nbericht", GetText("\nmessage")) end) @@ -232,6 +240,9 @@ describe("GetText module", function() describe("language with simple plurals n > 2", function() GetText.changeLang("simple") + it("gettext should ignore fuzzy strings", function() + assert.is_equal("Fuzzy", GetText("Fuzzy")) + end) it("gettext should translate multiline string", function() assert.is_equal("\nbericht", GetText("\nmessage")) end) @@ -256,6 +267,9 @@ describe("GetText module", function() describe("language with no plurals", function() GetText.changeLang("none") + it("gettext should ignore fuzzy strings", function() + assert.is_equal("Fuzzy", GetText("Fuzzy")) + end) it("gettext should translate multiline string", function() assert.is_equal("\nbericht", GetText("\nmessage")) end) @@ -280,6 +294,9 @@ describe("GetText module", function() describe("language with complex plurals (Arabic)", function() GetText.changeLang("ar") + it("gettext should ignore fuzzy strings", function() + assert.is_equal("Fuzzy", GetText("Fuzzy")) + end) it("gettext should translate multiline string", function() assert.is_equal("\nbericht", GetText("\nmessage")) end) @@ -306,6 +323,9 @@ describe("GetText module", function() describe("language with complex plurals (Russian)", function() GetText.changeLang("ru") + it("gettext should ignore fuzzy strings", function() + assert.is_equal("Fuzzy", GetText("Fuzzy")) + end) it("gettext should translate multiline string", function() assert.is_equal("\nbericht", GetText("\nmessage")) end) @@ -332,6 +352,9 @@ describe("GetText module", function() -- to follow, so there we focus on algorithm correctness. describe("language with many plurals", function() GetText.changeLang("many") + it("gettext should ignore fuzzy strings", function() + assert.is_equal("Fuzzy", GetText("Fuzzy")) + end) it("gettext should translate multiline string", function() assert.is_equal("\nbericht", GetText("\nmessage")) end)