Skip to content

Commit

Permalink
Demo of error page translation
Browse files Browse the repository at this point in the history
This commit demonstrates front-end-side translation of an error page
for a URL like /viewer#INVALIDBOOK/whatever (where INVALIDBOOK should
be a book name not present in the library)

Known issues:

- Tests are broken (not counting that in the CI they are broken due
  the missing patch to mustache.hpp)

- Changing the UI language while an error page is displayed in the
  viewer doesn't retranslate it.
  • Loading branch information
veloman-yunkan committed Jan 6, 2024
1 parent b564246 commit 9b607fd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r

if (archive == nullptr) {
const std::string searchURL = m_root + "/search?pattern=" + kiwix::urlEncode(pattern);
return UrlNotFoundResponse(request)
return UrlNotFoundResponse(request, true)
+ suggestSearchMsg(searchURL, kiwix::urlDecode(pattern));
}

Expand Down
12 changes: 8 additions & 4 deletions src/server/response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,20 @@ HTTPErrorResponse::HTTPErrorResponse(const RequestContext& request,
};
}

HTTP404Response::HTTP404Response(const RequestContext& request)
HTTP404Response::HTTP404Response(const RequestContext& request,
bool includeKiwixResponseData)
: HTTPErrorResponse(request,
MHD_HTTP_NOT_FOUND,
"404-page-title",
"404-page-heading")
"404-page-heading",
std::string(),
includeKiwixResponseData)
{
}

UrlNotFoundResponse::UrlNotFoundResponse(const RequestContext& request)
: HTTP404Response(request)
UrlNotFoundResponse::UrlNotFoundResponse(const RequestContext& request,
bool includeKiwixResponseData)
: HTTP404Response(request, includeKiwixResponseData)
{
const std::string requestUrl = urlDecode(m_request.get_full_url(), false);
*this += ParameterizedMessage("url-not-found", {{"url", requestUrl}});
Expand Down
6 changes: 4 additions & 2 deletions src/server/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,14 @@ struct HTTPErrorResponse : ContentResponseBlueprint

struct HTTP404Response : HTTPErrorResponse
{
explicit HTTP404Response(const RequestContext& request);
explicit HTTP404Response(const RequestContext& request,
bool includeKiwixResponseData = false);
};

struct UrlNotFoundResponse : HTTP404Response
{
explicit UrlNotFoundResponse(const RequestContext& request);
explicit UrlNotFoundResponse(const RequestContext& request,
bool includeKiwixResponseData = false);
};

struct HTTP400Response : HTTPErrorResponse
Expand Down
38 changes: 38 additions & 0 deletions static/skin/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,43 @@ function $t(msgId, params={}) {
}
}

const I18n = {
instantiateParameterizedMessages: function(data) {
//console.log('I18n.instantiateParameterizedMessages: data = ' + JSON.stringify(data));
if ( data.__proto__ == Array.prototype ) {
//console.log('I18n.instantiateParameterizedMessages: data is an array');
const result = [];
for ( const x of data ) {
result.push(this.instantiateParameterizedMessages(x));
}
return result;
} else if ( data.__proto__ == Object.prototype ) {
//console.log('I18n.instantiateParameterizedMessages: data is an object');
const msgId = data.msgid;
const msgParams = data.params;
if ( msgId && msgId.__proto__ == String.prototype && msgParams && msgParams.__proto__ == Object.prototype ) {
return $t(msgId, msgParams);
} else {
const result = {};
for ( const p in data ) {
result[p] = this.instantiateParameterizedMessages(data[p]);
}
return result;
}
} else {
//console.log('I18n.instantiateParameterizedMessages: data is something else');
return data;
}
},

render: function (template, params) {
console.log('I18n.render: params = ' + JSON.stringify(params));
params = this.instantiateParameterizedMessages(params);
console.log('I18n.render: instantiated params = ' + JSON.stringify(params));
return mustache.render(template, params);
}
}

const DEFAULT_UI_LANGUAGE = 'en';

Translations.load(DEFAULT_UI_LANGUAGE, /*asDefault=*/true);
Expand Down Expand Up @@ -145,3 +182,4 @@ window.$t = $t;
window.getUserLanguage = getUserLanguage;
window.setUserLanguage = setUserLanguage;
window.initUILanguageSelector = initUILanguageSelector;
window.I18n = I18n;
13 changes: 13 additions & 0 deletions static/skin/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,18 @@ function handle_location_hash_change() {
history.replaceState(viewerState, null);
}

function translateErrorPageIfNeeded() {
const cw = contentIframe.contentWindow;
if ( cw.KIWIX_RESPONSE_TEMPLATE && cw.KIWIX_RESPONSE_DATA ) {
const template = htmlDecode(cw.KIWIX_RESPONSE_TEMPLATE);
const params = JSON.parse(JSON.stringify(cw.KIWIX_RESPONSE_DATA));
const html = I18n.render(template, params);
setTimeout(function() {
cw.document.documentElement.innerHTML = new DOMParser().parseFromString(html, "text/html").documentElement.innerHTML;
}, 0);
}
}

function handle_content_url_change() {
const iframeLocation = contentIframe.contentWindow.location;
console.log('handle_content_url_change: ' + iframeLocation.href);
Expand All @@ -258,6 +270,7 @@ function handle_content_url_change() {
const newHash = iframeUrl2UserUrl(iframeContentUrl, iframeContentQuery);
history.replaceState(viewerState, null, makeURL(location.search, newHash));
updateCurrentBookIfNeeded(newHash);
translateErrorPageIfNeeded();
};

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 9b607fd

Please sign in to comment.