Skip to content

Commit 37cc00d

Browse files
authored
🔀 Merge pull request #588 from FrostCo/translation_system_updates
Translation system updates
2 parents cc5b30a + a673429 commit 37cc00d

File tree

6 files changed

+63
-29
lines changed

6 files changed

+63
-29
lines changed

bin/buildTranslation.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import BuildTranslation from './buildTranslationClass.js';
2+
3+
const buildTranslation = new BuildTranslation();
4+
buildTranslation.run();

bin/buildTranslationClass.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
export default class BuildTranslation {
5+
constructor() {
6+
this.localesDir = path.join('locales');
7+
this.translations = {};
8+
}
9+
10+
build() {
11+
this.translations = this.combineLocaleFiles(this.localesDir);
12+
}
13+
14+
get combinedTranslationsPath() {
15+
return path.join('src', 'script', 'translations.js');
16+
}
17+
18+
combineLocaleFiles(root) {
19+
const combined = {};
20+
21+
// Iterate through each language directory (e.g., 'en', 'fr')
22+
fs.readdirSync(root).forEach((locale) => {
23+
combined[locale] = {};
24+
const localeDir = path.join(root, locale);
25+
26+
// Iterate through each namespace file in the language directory
27+
fs.readdirSync(localeDir).forEach((file) => {
28+
const namespace = path.basename(file, '.json');
29+
const filePath = path.join(localeDir, file);
30+
const fileContents = JSON.parse(fs.readFileSync(filePath, 'utf8'));
31+
32+
// Assign the namespace contents to the appropriate language
33+
combined[locale][namespace] = fileContents;
34+
});
35+
});
36+
37+
return combined;
38+
}
39+
40+
get output() {
41+
return `export const translations = ${JSON.stringify(this.translations, null, 2)};\n\nexport default translations;\n`;
42+
}
43+
44+
run() {
45+
this.build();
46+
this.writeCombinedTranslations();
47+
}
48+
49+
writeCombinedTranslations() {
50+
fs.writeFileSync(this.combinedTranslationsPath, this.output, 'utf8');
51+
}
52+
}

bin/buildTranslations.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"build:firefox": "npm run build:firefox:mv2",
2929
"build:libs": "tsc -p ./src/script/lib/tsconfig.json",
3030
"build:static": "node bin/copyStatic.js",
31-
"build:translations": "node bin/buildTranslations.js",
31+
"build:translations": "node bin/buildTranslation.js",
3232
"build": "webpack --config bin/webpack.dev.js && npm run build:static",
3333
"clean:all": "node bin/clean.js --all",
3434
"clean:build": "node bin/clean.js --build",

src/script/mainOptionPage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const option = new OptionPage;
55
////
66
// Events
77
// Add event listeners to DOM
8-
window.addEventListener('load', (evt) => { option.init(); });
8+
window.addEventListener('DOMContentLoaded', (evt) => { option.init(); });
99
document.querySelectorAll('#menu a').forEach((el) => { el.addEventListener('click', (evt) => { option.switchPage(evt.target as HTMLAnchorElement); }); });
1010
// Modals
1111
document.getElementById('submitPassword').addEventListener('click', (evt) => { option.auth.authenticate(evt.target as HTMLButtonElement); });

src/script/translation.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { stringArray } from '@APF/lib/helper';
55
export default class Translation {
66
i18next: i18n;
77

8+
//#region Class reference helpers
9+
// Can be overridden in children classes
10+
get Class() { return (this.constructor as typeof Translation); }
11+
//#endregion
12+
813
constructor(namespaces: string|string[] = [], language: string = 'en') {
914
namespaces = stringArray(namespaces);
1015
this.i18next = i18next;

0 commit comments

Comments
 (0)