Skip to content

Commit f4880ae

Browse files
Pre-render HTML with default language
1 parent a34b740 commit f4880ae

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/webui/www/public/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ <h1>qBittorrent WebUI</h1>
3232
<div id="formplace" class="col">
3333
<form id="loginform">
3434
<div class="row">
35-
<label for="username" data-i18n>Username_HttpServer</label><br />
35+
<label for="username" data-i18n="Username_HttpServer"></label><br />
3636
<input type="text" id="username" name="username" autocomplete="username" autofocus required />
3737
</div>
3838
<div class="row">
39-
<label for="password" data-i18n>Password_HttpServer</label><br />
39+
<label for="password" data-i18n="Password_HttpServer"></label><br />
4040
<input type="password" id="password" name="password" autocomplete="current-password" required />
4141
</div>
4242
<div class="row">

src/webui/www/public/scripts/login.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
'use strict';
3030

3131
async function setupI18n() {
32+
// present it to i18next
33+
const i18nextOptions = {
34+
lng: 'en',
35+
fallbackLng: false,
36+
load: 'currentOnly',
37+
returnEmptyString: false
38+
};
39+
// Pre-render HTML with default language and don't wait for the network response
40+
await i18next.init(i18nextOptions, replaceI18nText);
41+
3242
const languages = (() => {
3343
const langs = new Set();
3444
for (const lang of navigator.languages) {
@@ -53,43 +63,35 @@ async function setupI18n() {
5363
data: (translations.length > 0) ? (await translations[0].result.value.json()) : {}
5464
};
5565

56-
// present it to i18next
57-
const i18nextOptions = {
58-
lng: translation.lang,
59-
fallbackLng: false,
60-
load: 'currentOnly',
61-
resources: {
62-
[translation.lang]: { translation: translation.data }
63-
},
64-
returnEmptyString: false
65-
};
66-
i18next.init(i18nextOptions, replaceI18nText);
66+
i18next.addResources(translation.lang, "translation", translation.data);
67+
i18next.changeLanguage(translation.lang, replaceI18nText);
6768
}
6869

6970
function replaceI18nText() {
7071
const tr = i18next.t; // workaround for warnings from i18next-parser
71-
const re = /^(\[([a-zA-Z0-9_-]+)\])?(.*)$/;
72+
const re = /^(\[([a-zA-Z0-9_-]+)\])?(.+?)(_(.+))?$/;
7273

7374
for (const element of document.querySelectorAll('[data-i18n]')) {
74-
const i18nData = element.getAttribute('data-i18n');
75-
if (!i18nData) {
76-
element.textContent = tr(element.textContent);
77-
continue;
78-
}
75+
const translationKeys = element.getAttribute('data-i18n').split(';');
7976

80-
const translationKeys = i18nData.split(';');
8177
for (const key of translationKeys) {
8278
const matches = key.match(re);
8379
if (matches[2]) {
84-
element.setAttribute(matches[2], tr(matches[3]));
80+
if (matches[5])
81+
element.setAttribute(matches[2], tr(matches[3], { context: matches[5] }));
82+
else
83+
element.setAttribute(matches[2], tr(matches[3]));
8584
continue;
8685
}
8786

88-
element.textContent = tr(matches[3]);
87+
if (matches[5])
88+
element.textContent = tr(matches[3], { context: matches[5] });
89+
else
90+
element.textContent = tr(matches[3]);
8991
}
9092
}
9193

92-
document.documentElement.lang = i18next.language.split('-')[0];
94+
document.documentElement.lang = i18next.language;
9395
}
9496

9597
function submitLoginForm(event) {

0 commit comments

Comments
 (0)