Skip to content

Commit

Permalink
UI language setting is preserved in a cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
veloman-yunkan committed Jan 25, 2023
1 parent 27ce23b commit 2af6974
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions static/skin/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,31 @@ function $t(msgId, params={}) {
}
}

function getCookie(cookieName) {
const name = cookieName + "=";
let result;
decodeURIComponent(document.cookie).split('; ').forEach(val => {
if (val.indexOf(name) === 0) {
result = val.substring(name.length);
}
});
return result;
}


const DEFAULT_UI_LANGUAGE = 'en';

Translations.load(DEFAULT_UI_LANGUAGE, /*asDefault=*/true);

function getUserLanguage() {
return new URLSearchParams(window.location.search).get('userlang')
|| getCookie('userlang')
|| DEFAULT_UI_LANGUAGE;
}

function setUserLanguage(lang, callback) {
const rootPath = new URL('..', import.meta.url).pathname.replace(/\/$/, '');
document.cookie = `userlang=${lang};path=${rootPath};max-age=31536000`;
Translations.load(lang);
Translations.whenReady(callback);
}
Expand Down
4 changes: 2 additions & 2 deletions test/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const ResourceCollection resources200Compressible{
{ DYNAMIC_CONTENT, "/ROOT/skin/favicon/favicon.ico" },
{ STATIC_CONTENT, "/ROOT/skin/favicon/favicon.ico?cacheid=fba03a27" },
{ DYNAMIC_CONTENT, "/ROOT/skin/i18n.js" },
{ STATIC_CONTENT, "/ROOT/skin/i18n.js?cacheid=eb41f5ce" },
{ STATIC_CONTENT, "/ROOT/skin/i18n.js?cacheid=dcf3d584" },
{ DYNAMIC_CONTENT, "/ROOT/skin/index.css" },
{ STATIC_CONTENT, "/ROOT/skin/index.css?cacheid=0f9ba34e" },
{ DYNAMIC_CONTENT, "/ROOT/skin/index.js" },
Expand Down Expand Up @@ -300,7 +300,7 @@ R"EXPECTEDRESULT( <img src="../skin/download.png?
/* url */ "/ROOT/viewer",
R"EXPECTEDRESULT( <link type="text/css" href="./skin/taskbar.css?cacheid=2cbac34b" rel="Stylesheet" />
<link type="text/css" href="./skin/css/autoComplete.css?cacheid=08951e06" rel="Stylesheet" />
<script type="module" src="./skin/i18n.js?cacheid=eb41f5ce" defer></script>
<script type="module" src="./skin/i18n.js?cacheid=dcf3d584" defer></script>
<script type="text/javascript" src="./skin/languages.js?cacheid=fe100348" defer></script>
<script type="text/javascript" src="./skin/viewer.js?cacheid=b3c754ec" defer></script>
<script type="text/javascript" src="./skin/autoComplete.min.js?cacheid=1191aaaf"></script>
Expand Down

0 comments on commit 2af6974

Please sign in to comment.