diff --git a/static/generate_i18n_resources_list.py b/static/generate_i18n_resources_list.py index 92229feeb..b083e3578 100755 --- a/static/generate_i18n_resources_list.py +++ b/static/generate_i18n_resources_list.py @@ -17,15 +17,36 @@ # along with this program. If not, see . 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) diff --git a/static/resources_list.txt b/static/resources_list.txt index f2fd95792..1ad8a412e 100644 --- a/static/resources_list.txt +++ b/static/resources_list.txt @@ -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 diff --git a/static/skin/i18n/ja.json b/static/skin/i18n/ja.json index e4f0e62d1..8e36ce258 100644 --- a/static/skin/i18n/ja.json +++ b/static/skin/i18n/ja.json @@ -4,6 +4,7 @@ "MathXplore" ] }, + "name": "日本語", "no-query": "クエリを指定していません。", "400-page-title": "無効なリクエストです", "400-page-heading": "無効なリクエストです", diff --git a/static/skin/languages.js b/static/skin/languages.js new file mode 100644 index 000000000..1ff325f1c --- /dev/null +++ b/static/skin/languages.js @@ -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" + } +] \ No newline at end of file diff --git a/test/server.cpp b/test/server.cpp index e4ada359c..ce7819dd9 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -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