From cc35fe503fca127dedd67be33ae583b209c3e200 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Sat, 25 Feb 2023 13:11:09 +0530 Subject: [PATCH] Translations for /nojs endpoint Uses the string from #846 for translations. A couple new translations are also added for tag. --- src/html_dumper.cpp | 18 ++++++++++++++++-- src/server/i18n.h | 22 ++++++++++++++++++++++ src/server/internalServer.cpp | 17 ++++++++++++++--- src/server/internalServer.h | 2 +- static/skin/i18n/en.json | 4 ++++ static/skin/i18n/qqq.json | 6 +++++- static/skin/i18n/test.json | 4 ++++ static/templates/no_js_download.html | 14 +++++++------- static/templates/no_js_library_page.html | 17 +++++++++-------- 9 files changed, 82 insertions(+), 22 deletions(-) diff --git a/src/html_dumper.cpp b/src/html_dumper.cpp index 31aae9259..a50315bd8 100644 --- a/src/html_dumper.cpp +++ b/src/html_dumper.cpp @@ -3,6 +3,7 @@ #include "tools/otherTools.h" #include "tools.h" #include "tools/regexTools.h" +#include "server/i18n.h" namespace kiwix { @@ -88,15 +89,28 @@ std::string HTMLDumper::dumpPlainHTML(kiwix::Filter filter) const }); } + auto getTranslation = i18n::GetTranslatedStringWithMsgId(m_userLang); + + const auto translations = kainjow::mustache::object{ + getTranslation("search"), + getTranslation("download"), + getTranslation("count-of-matching-books", {{"COUNT", to_string(filteredBooks.size())}}), + getTranslation("book-filtering-all-categories"), + getTranslation("book-filtering-all-languages"), + getTranslation("powered-by-kiwix-html"), + getTranslation("welcome-to-kiwix-server"), + getTranslation("preview-book") + }; + return render_template( RESOURCE::templates::no_js_library_page_html, kainjow::mustache::object{ {"root", rootLocation}, {"books", booksData }, {"searchQuery", searchQuery}, - {"resultsCount", to_string(filteredBooks.size())}, {"languages", languages}, - {"categories", categories} + {"categories", categories}, + {"translations", translations} } ); } diff --git a/src/server/i18n.h b/src/server/i18n.h index 23236074a..e1cbcc68f 100644 --- a/src/server/i18n.h +++ b/src/server/i18n.h @@ -69,6 +69,28 @@ private: const std::string m_lang; }; +class GetTranslatedStringWithMsgId +{ + typedef kainjow::mustache::basic_data<std::string> MustacheString; + typedef std::pair<std::string, MustacheString> MsgIdAndTranslation; + +public: + explicit GetTranslatedStringWithMsgId(const std::string& lang) : m_lang(lang) {} + + MsgIdAndTranslation operator()(const std::string& key) const + { + return {key, getTranslatedString(m_lang, key)}; + } + + MsgIdAndTranslation operator()(const std::string& key, const Parameters& params) const + { + return {key, expandParameterizedString(m_lang, key, params)}; + } + +private: + const std::string m_lang; +}; + } // namespace i18n struct ParameterizedMessage diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 799ed8e5b..52f32be76 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -759,16 +759,25 @@ std::unique_ptr<Response> InternalServer::handle_viewer_settings(const RequestCo return ContentResponse::build(*this, RESOURCE::templates::viewer_settings_js, data, "application/javascript; charset=utf-8"); } -std::string InternalServer::getNoJSDownloadPageHTML(const std::string& bookId) const +std::string InternalServer::getNoJSDownloadPageHTML(const std::string& bookId, const std::string& userLang) const { const auto book = mp_library->getBookById(bookId); auto bookUrl = kiwix::stripSuffix(book.getUrl(), ".meta4"); + auto getTranslation = i18n::GetTranslatedStringWithMsgId(userLang); + const auto translations = kainjow::mustache::object{ + getTranslation("download-links-heading", {{"BOOK_TITLE", book.getTitle()}}), + getTranslation("download-links-title"), + getTranslation("direct-download-link-text"), + getTranslation("hash-download-link-text"), + getTranslation("magnet-link-text"), + getTranslation("torrent-download-link-text") + }; return render_template( RESOURCE::templates::no_js_download_html, kainjow::mustache::object{ {"url", bookUrl}, - {"bookTitle", book.getTitle()} + {"translations", translations} } ); } @@ -780,6 +789,8 @@ std::unique_ptr<Response> InternalServer::handle_no_js(const RequestContext& req HTMLDumper htmlDumper(mp_library, mp_nameMapper); htmlDumper.setRootLocation(m_root); htmlDumper.setLibraryId(getLibraryId()); + auto userLang = request.get_user_language(); + htmlDumper.setUserLanguage(userLang); std::string content; if (urlParts.size() == 1) { @@ -798,7 +809,7 @@ std::unique_ptr<Response> InternalServer::handle_no_js(const RequestContext& req } else if ((urlParts.size() == 3) && (urlParts[1] == "download")) { try { const auto bookId = mp_nameMapper->getIdForName(urlParts[2]); - content = getNoJSDownloadPageHTML(bookId); + content = getNoJSDownloadPageHTML(bookId, userLang); } catch (const std::out_of_range&) { return HTTP404Response(*this, request) + urlNotFoundMsg; diff --git a/src/server/internalServer.h b/src/server/internalServer.h index de84f251e..867b324e0 100644 --- a/src/server/internalServer.h +++ b/src/server/internalServer.h @@ -156,7 +156,7 @@ class InternalServer { std::string getLibraryId() const; - std::string getNoJSDownloadPageHTML(const std::string& bookId) const; + std::string getNoJSDownloadPageHTML(const std::string& bookId, const std::string& userLang) const; private: // types class LockableSuggestionSearcher; diff --git a/static/skin/i18n/en.json b/static/skin/i18n/en.json index acf2b0131..e8a3d26b1 100644 --- a/static/skin/i18n/en.json +++ b/static/skin/i18n/en.json @@ -47,4 +47,8 @@ , "filter-by-tag": "Filter by tag \"{{TAG}}\"" , "stop-filtering-by-tag": "Stop filtering by tag \"{{TAG}}\"" , "library-opds-feed-parameterised": "Library OPDS Feed - entries matching {{#LANG}}\nLanguage: {{LANG}} {{/LANG}}{{#CATEGORY}}\nCategory: {{CATEGORY}} {{/CATEGORY}}{{#TAG}}\nTag: {{TAG}} {{/TAG}}{{#Q}}\nQuery: {{Q}} {{/Q}}" + , "welcome-to-kiwix-server": "Welcome to Kiwix Server" + , "download-links-heading": "Download links for <b><i>{{BOOK_TITLE}}</i></b>" + , "download-links-title": "Download book" + , "preview-book": "Preview" } diff --git a/static/skin/i18n/qqq.json b/static/skin/i18n/qqq.json index 8d2acf869..069f5e774 100644 --- a/static/skin/i18n/qqq.json +++ b/static/skin/i18n/qqq.json @@ -48,5 +48,9 @@ "filter-by-tag": "Hint for a link that would load results filtered by a single tag", "stop-filtering-by-tag": "Tooltip for the button that cancels filtering by tag", "library-opds-feed-all-entries": "Hint for the library OPDS feed for all entries", - "library-opds-feed-parameterised": "Hint for the library OPDS feed for filtered entries" + "library-opds-feed-parameterised": "Hint for the library OPDS feed for filtered entries", + "welcome-to-kiwix-server": "Title shown in browser's title bar/page tab", + "download-links-heading": "Heading for no-js download page", + "download-links-title": "Title for no-js download page", + "preview-book": "Tooltip of book-tile leading to the book" } diff --git a/static/skin/i18n/test.json b/static/skin/i18n/test.json index b2e02e6b4..c48c181a9 100644 --- a/static/skin/i18n/test.json +++ b/static/skin/i18n/test.json @@ -36,4 +36,8 @@ , "filter-by-tag": "Filter [I18N] by [TESTING] tag \"{{TAG}}\"" , "stop-filtering-by-tag": "[I18N] Stop filtering [TESTING] by tag \"{{TAG}}\"" , "library-opds-feed-parameterised": "[I18N] Library OPDS Feed - [TESTING] entries matching {{#LANG}}\nLanguage: {{LANG}} {{/LANG}}{{#CATEGORY}}\nCategory: {{CATEGORY}} {{/CATEGORY}}{{#TAG}}\nTag: {{TAG}} {{/TAG}}{{#Q}}\nQuery: {{Q}} {{/Q}}" + , "welcome-to-kiwix-server": "[I18N] Welcome to Kiwix Server [TESTING]" + , "download-links-heading": "[I18N] Download links for <b><i>{{BOOK_TITLE}}</i></b> [TESTING]" + , "download-links-title": "[I18N TESTING]Download book" + , "preview-book": "[I18N] Preview [TESTING]" } diff --git a/static/templates/no_js_download.html b/static/templates/no_js_download.html index d1ba6f249..5bddee319 100644 --- a/static/templates/no_js_download.html +++ b/static/templates/no_js_download.html @@ -4,30 +4,30 @@ <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <title>Download book + {{translations.download-links-title}}
- Download links for {{bookTitle}} + {{{translations.download-links-heading}}}
-
Direct
+
{{translations.direct-download-link-text}}
-
Sha256 hash
+
{{translations.hash-download-link-text}}
-
Magnet link
+
{{translations.magnet-link-text}}
-
Torrent file
+
{{translations.torrent-download-link-text}}
\ No newline at end of file diff --git a/static/templates/no_js_library_page.html b/static/templates/no_js_library_page.html index d96f2ed7f..728eb251f 100644 --- a/static/templates/no_js_library_page.html +++ b/static/templates/no_js_library_page.html @@ -4,7 +4,7 @@ - Welcome to Kiwix Server + {{translations.welcome-to-kiwix-server}}
- + {{#categories}} {{/categories}} @@ -85,13 +85,13 @@
- - + +
-

{{resultsCount}} results

+

{{translations.count-of-matching-books}}

{{#books}}
@@ -117,5 +117,6 @@ {{/books}}
+
{{{translations.powered-by-kiwix-html}}}
\ No newline at end of file