Translations for /nojs endpoint

Uses the string from #846 for translations.
A couple new translations are also added for <title> tag.
This commit is contained in:
Nikhil Tanwar 2023-02-25 13:11:09 +05:30
parent 37aadb86fb
commit cc35fe503f
9 changed files with 82 additions and 22 deletions

View File

@ -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}
}
);
}

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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"
}

View File

@ -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"
}

View File

@ -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]"
}

View File

@ -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</title>
<title>{{translations.download-links-title}}</title>
</head>
<style>
.downloadLinksTitle {
text-align: center;
font-size: 32px;
margin-bottom: 6px;
margin-bottom: 8px;
}
</style>
<body>
<div class="downloadLinksTitle">
Download links for <b><i>{{bookTitle}}</i></b>
{{{translations.download-links-heading}}}
</div>
<a href="{{url}}" download>
<div>Direct</div>
<div>{{translations.direct-download-link-text}}</div>
</a>
<a href="{{url}}.sha256" download>
<div>Sha256 hash</div>
<div>{{translations.hash-download-link-text}}</div>
</a>
<a href="{{url}}.magnet" target="_blank">
<div>Magnet link</div>
<div>{{translations.magnet-link-text}}</div>
</a>
<a href="{{url}}.torrent" download>
<div>Torrent file</div>
<div>{{translations.torrent-download-link-text}}</div>
</a>
</body>
</html>

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link type="root" href="{{root}}">
<title>Welcome to Kiwix Server</title>
<title>{{translations.welcome-to-kiwix-server}}</title>
<link
type="text/css"
href="{{root}}/skin/index.css?KIWIXCACHEID"
@ -69,7 +69,7 @@
<div class="kiwixNav__filters">
<div class="kiwixNav__select">
<select name="lang" id="languageFilter" class='kiwixNav__kiwixFilter filter' form="kiwixSearchForm">
<option value="" selected>All languages</option>
<option value="" selected>{{translations.book-filtering-all-languages}}</option>
{{#languages}}
<option value="{{lang_code}}"{{#selected}} selected {{/selected}}>{{lang_self_name}}</option>
{{/languages}}
@ -77,7 +77,7 @@
</div>
<div class="kiwixNav__select">
<select name="category" id="categoryFilter" class='kiwixNav__kiwixFilter filter' form="kiwixSearchForm">
<option value="">All categories</option>
<option value="">{{translations.book-filtering-all-categories}}</option>
{{#categories}}
<option value="{{name}}"{{#selected}} selected {{/selected}}>{{hf_name}}</option>
{{/categories}}
@ -85,13 +85,13 @@
</div>
</div>
<form id='kiwixSearchForm' class='kiwixNav__SearchForm' action="{{root}}/nojs">
<input type="text" name="q" placeholder="Search" id="searchFilter" class='kiwixSearch filter' value="{{searchQuery}}">
<input type="submit" class="kiwixButton kiwixButtonHover" value="Search"/>
<input type="text" name="q" placeholder="{{translations.search}}" id="searchFilter" class='kiwixSearch filter' value="{{searchQuery}}">
<input type="submit" class="kiwixButton kiwixButtonHover" value="{{translations.search}}"/>
</form>
</div>
<div class="kiwixHomeBody">
<div class="book__list">
<h3 class="kiwixHomeBody__results">{{resultsCount}} results</h3>
<h3 class="kiwixHomeBody__results">{{translations.count-of-matching-books}}</h3>
{{#books}}
<div class="book__wrapper">
<div class="book__link__wrapper">
@ -99,10 +99,10 @@
<div class="book__header">
<div id="book__title"><a href="{{root}}/content/{{id}}">{{title}}</a></div>
{{#downloadAvailable}}
<div class="book__download"><span><a href="{{root}}/nojs/download/{{id}}">Download</a></span></div>
<div class="book__download"><span><a href="{{root}}/nojs/download/{{id}}">{{translations.download}}</a></span></div>
{{/downloadAvailable}}
</div>
<a class="book__link" href="{{root}}/content/{{id}}" title="Preview" aria-label="Preview">
<a class="book__link" href="{{root}}/content/{{id}}" title="{{translations.preview-book}}" aria-label="{{translations.preview-book}}">
<div class="book__description" title="{{description}}">{{description}}</div>
</a>
</div>
@ -117,5 +117,6 @@
{{/books}}
</div>
</div>
<div id="kiwixfooter" class="kiwixfooter">{{{translations.powered-by-kiwix-html}}}</div>
</body>
</html>