locale for web app (#334)
* locale for web app * more translations * Rename profiles service to avoid locale conflict * More translation work * Lint fix --------- Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
1 parent
019fde1e67
commit
42dadec020
54 files changed
+538
-151
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import { tr } from "lib/Stores";
|
||||
import { PlayletApi } from "./PlayletApi";
|
||||
|
||||
export async function fetchLocale(locale: string) {
|
||||
try {
|
||||
if (!locale) {
|
||||
return;
|
||||
}
|
||||
const localeFile = await PlayletApi.getLocale(locale);
|
||||
const parser = new DOMParser();
|
||||
const xmlDoc = parser.parseFromString(localeFile, "text/xml");
|
||||
const context = xmlDoc.getElementsByTagName("context");
|
||||
if (context.length === 0) {
|
||||
return;
|
||||
}
|
||||
const messages = context[0].getElementsByTagName("message");
|
||||
if (messages.length === 0) {
|
||||
return;
|
||||
}
|
||||
const translations = {};
|
||||
for (let i = 0; i < messages.length; i++) {
|
||||
const message = messages[i];
|
||||
const source = message.getElementsByTagName("source");
|
||||
if (source.length === 0) {
|
||||
continue;
|
||||
}
|
||||
const translation = message.getElementsByTagName("translation");
|
||||
if (translation.length === 0) {
|
||||
continue;
|
||||
}
|
||||
translations[source[0].textContent] = translation[0].textContent;
|
||||
}
|
||||
tr.set((s) => translations[s] || s);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,11 @@ export class PlayletApi {
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
static async getLocale(locale: string) {
|
||||
const response = await fetch(`${PlayletApi.host()}/locale/${locale}/translations.ts`);
|
||||
return await response.text();
|
||||
}
|
||||
|
||||
static async getPreferencesFile() {
|
||||
const response = await fetch(`${PlayletApi.host()}/config/preferences.json5`);
|
||||
return await response.json();
|
||||
|
||||
Reference in new issue
Block a user