/skin/languages.js

Serving the language list as a JS file rather than JSON simplifies
a few things:

- cacheid management;
- having to manually delay the UI initialization until the JSON file
  is loaded.

static/skin/languages.js must be generated/updated manually by running
the static/generate_i18n_resources_list.py script.
This commit is contained in:
Veloman Yunkan 2023-01-19 15:38:00 +04:00
parent 9f34613473
commit 2995a00cd0
5 changed files with 164 additions and 3 deletions

View File

@ -17,15 +17,36 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pathlib import Path
import json
script_path = Path(__file__)
resource_file = script_path.parent / "i18n_resources_list.txt"
translation_dir = script_path.parent / "skin/i18n"
language_list_relpath = "skin/languages.js"
def get_translation_info(filepath):
lang_code = Path(filepath).stem
with open(filepath, 'r', encoding="utf-8") as f:
content = json.load(f)
return lang_code, content["name"]
language_list = []
json_files = translation_dir.glob("*.json")
with open(resource_file, 'w', encoding="utf-8") as f:
for json in sorted(translation_dir.glob("*.json")):
if json.name == "qqq.json":
for i18n_file in sorted(translation_dir.glob("*.json")):
if i18n_file.name == "qqq.json":
continue
f.write(str(json.relative_to(script_path.parent)) + '\n')
print("Processing", i18n_file.name)
if i18n_file.name != "test.json":
language_list.append(get_translation_info(i18n_file))
f.write(str(i18n_file.relative_to(script_path.parent)) + '\n')
language_list = [{name: code} for code, name in sorted(language_list)]
language_list_jsobj_str = json.dumps(language_list,
indent=2,
ensure_ascii=False)
print("Saving", language_list_relpath)
fullpath = script_path.parent / language_list_relpath
with open(fullpath, 'w', encoding="utf-8") as f:
f.write("const uiLanguages = " + language_list_jsobj_str)

View File

@ -15,6 +15,7 @@ skin/fonts/Roboto.ttf
skin/search_results.css
skin/blank.html
skin/viewer.js
skin/languages.js
skin/mustache.min.js
viewer.html
templates/search_result.html

View File

@ -4,6 +4,7 @@
"MathXplore"
]
},
"name": "日本語",
"no-query": "クエリを指定していません。",
"400-page-title": "無効なリクエストです",
"400-page-heading": "無効なリクエストです",

65
static/skin/languages.js Normal file
View File

@ -0,0 +1,65 @@
const uiLanguages = [
{
"বাংলা": "bn"
},
{
"Čeština": "cs"
},
{
"Deutsch": "de"
},
{
"English": "en"
},
{
"français": "fr"
},
{
"עברית": "he"
},
{
"Հայերեն": "hy"
},
{
"italiano": "it"
},
{
"日本語": "ja"
},
{
"한국어": "ko"
},
{
"kurdî": "ku-latn"
},
{
"македонски": "mk"
},
{
"ߒߞߏ": "nqo"
},
{
"Polski": "pl"
},
{
"русский": "ru"
},
{
"Sardu": "sc"
},
{
"slovenčina": "sk"
},
{
"Svenska": "sv"
},
{
"Türkçe": "tr"
},
{
"英语": "zh-hans"
},
{
"繁體中文": "zh-hant"
}
]

View File

@ -140,6 +140,8 @@ const ResourceCollection resources200Uncompressible{
{ DYNAMIC_CONTENT, "/ROOT/skin/i18n/test.json" },
// TODO: implement cache management of i18n resources
//{ STATIC_CONTENT, "/ROOT/skin/i18n/test.json?cacheid=unknown" },
{ DYNAMIC_CONTENT, "/ROOT/skin/languages.js" },
{ STATIC_CONTENT, "/ROOT/skin/languages.js?cacheid=fe100348" },
{ ZIM_CONTENT, "/ROOT/raw/zimfile/meta/Title" },
{ ZIM_CONTENT, "/ROOT/raw/zimfile/meta/Description" },
@ -977,6 +979,77 @@ TEST_F(ServerTest, 500)
}
}
TEST_F(ServerTest, UserLanguageList)
{
const auto r = zfs1_->GET("/ROOT/skin/languages.js");
EXPECT_EQ(r->body,
R"EXPECTEDRESPONSE(const uiLanguages = [
{
"বাংলা": "bn"
},
{
"Čeština": "cs"
},
{
"Deutsch": "de"
},
{
"English": "en"
},
{
"français": "fr"
},
{
"עברית": "he"
},
{
"Հայերեն": "hy"
},
{
"italiano": "it"
},
{
"日本語": "ja"
},
{
"한국어": "ko"
},
{
"kurdî": "ku-latn"
},
{
"македонски": "mk"
},
{
"ߒߞߏ": "nqo"
},
{
"Polski": "pl"
},
{
"русский": "ru"
},
{
"Sardu": "sc"
},
{
"slovenčina": "sk"
},
{
"Svenska": "sv"
},
{
"Türkçe": "tr"
},
{
"英语": "zh-hans"
},
{
"繁體中文": "zh-hant"
}
])EXPECTEDRESPONSE");
}
TEST_F(ServerTest, UserLanguageControl)
{
struct TestData