Skip to content

Commit

Permalink
🔀 Merge pull request #588 from FrostCo/translation_system_updates
Browse files Browse the repository at this point in the history
Translation system updates
  • Loading branch information
richardfrost authored Nov 7, 2024
2 parents cc5b30a + a673429 commit 37cc00d
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 29 deletions.
4 changes: 4 additions & 0 deletions bin/buildTranslation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import BuildTranslation from './buildTranslationClass.js';

const buildTranslation = new BuildTranslation();
buildTranslation.run();
52 changes: 52 additions & 0 deletions bin/buildTranslationClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import fs from 'fs';
import path from 'path';

export default class BuildTranslation {
constructor() {
this.localesDir = path.join('locales');
this.translations = {};
}

build() {
this.translations = this.combineLocaleFiles(this.localesDir);
}

get combinedTranslationsPath() {
return path.join('src', 'script', 'translations.js');
}

combineLocaleFiles(root) {
const combined = {};

// Iterate through each language directory (e.g., 'en', 'fr')
fs.readdirSync(root).forEach((locale) => {
combined[locale] = {};
const localeDir = path.join(root, locale);

// Iterate through each namespace file in the language directory
fs.readdirSync(localeDir).forEach((file) => {
const namespace = path.basename(file, '.json');
const filePath = path.join(localeDir, file);
const fileContents = JSON.parse(fs.readFileSync(filePath, 'utf8'));

// Assign the namespace contents to the appropriate language
combined[locale][namespace] = fileContents;
});
});

return combined;
}

get output() {
return `export const translations = ${JSON.stringify(this.translations, null, 2)};\n\nexport default translations;\n`;
}

run() {
this.build();
this.writeCombinedTranslations();
}

writeCombinedTranslations() {
fs.writeFileSync(this.combinedTranslationsPath, this.output, 'utf8');
}
}
27 changes: 0 additions & 27 deletions bin/buildTranslations.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"build:firefox": "npm run build:firefox:mv2",
"build:libs": "tsc -p ./src/script/lib/tsconfig.json",
"build:static": "node bin/copyStatic.js",
"build:translations": "node bin/buildTranslations.js",
"build:translations": "node bin/buildTranslation.js",
"build": "webpack --config bin/webpack.dev.js && npm run build:static",
"clean:all": "node bin/clean.js --all",
"clean:build": "node bin/clean.js --build",
Expand Down
2 changes: 1 addition & 1 deletion src/script/mainOptionPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const option = new OptionPage;
////
// Events
// Add event listeners to DOM
window.addEventListener('load', (evt) => { option.init(); });
window.addEventListener('DOMContentLoaded', (evt) => { option.init(); });
document.querySelectorAll('#menu a').forEach((el) => { el.addEventListener('click', (evt) => { option.switchPage(evt.target as HTMLAnchorElement); }); });
// Modals
document.getElementById('submitPassword').addEventListener('click', (evt) => { option.auth.authenticate(evt.target as HTMLButtonElement); });
Expand Down
5 changes: 5 additions & 0 deletions src/script/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { stringArray } from '@APF/lib/helper';
export default class Translation {
i18next: i18n;

//#region Class reference helpers
// Can be overridden in children classes
get Class() { return (this.constructor as typeof Translation); }
//#endregion

constructor(namespaces: string|string[] = [], language: string = 'en') {
namespaces = stringArray(namespaces);
this.i18next = i18next;
Expand Down

0 comments on commit 37cc00d

Please sign in to comment.