diff --git a/frontend/gettext.lua b/frontend/gettext.lua index 845ae98a2..69bc32cc1 100644 --- a/frontend/gettext.lua +++ b/frontend/gettext.lua @@ -36,14 +36,13 @@ end -- we only implement a sane subset for now function GetText_mt.__index.changeLang(new_lang) - -- can be various things such as `en_US` or `en_US:en` - if new_lang:match("^en_US") == "en_US" then return end - GetText.translation = {} GetText.current_lang = "C" - -- the "C" locale disables localization alltogether - if new_lang == "C" or new_lang == nil then return end + -- the "C" locale disables localization altogether + -- can be various things such as `en_US` or `en_US:en` + if new_lang == "C" or new_lang == nil or new_lang == "" + or new_lang:match("^en_US") == "en_US" then return end -- strip encoding suffix in locale like "zh_CN.utf8" new_lang = new_lang:sub(1, new_lang:find(".%.")) @@ -53,7 +52,7 @@ function GetText_mt.__index.changeLang(new_lang) if not po then logger.err("cannot open translation file:", file) - return + return false end local data = {} diff --git a/kodev b/kodev index e9c7dc8be..e5a8d668f 100755 --- a/kodev +++ b/kodev @@ -561,7 +561,7 @@ OPTIONS: busted --lua="./luajit" "${opts}" \ --no-auto-insulate \ --lazy \ - -o "./spec/$1/unit/verbose_print" \ + --output=gtest \ --exclude-tags=notest "${test_path}" } && popd || exit } diff --git a/spec/unit/gettext_spec.lua b/spec/unit/gettext_spec.lua new file mode 100644 index 000000000..10e362f49 --- /dev/null +++ b/spec/unit/gettext_spec.lua @@ -0,0 +1,25 @@ +describe("GetText module", function() + local GetText + setup(function() + require("commonrequire") + GetText = require("gettext") + end) + describe("changeLang", function() + it("should return nil when passing newlang = C", function() + assert.is_nil(GetText.changeLang("C")) + end) + it("should return nil when passing empty string or nil value", function() + assert.is_nil(GetText.changeLang(nil)) + assert.is_nil(GetText.changeLang("")) + end) + it("should return nil when passing values that start with en_US", function() + assert.is_nil(GetText.changeLang("en_US")) + assert.is_nil(GetText.changeLang("en_US:en")) + assert.is_nil(GetText.changeLang("en_US.utf8")) + end) + it("should return false when it can't find a po file", function() + assert.is_false(GetText.changeLang("nonsense")) + assert.is_false(GetText.changeLang("more_NONSENSE")) + end) + end) +end)