diff --git a/bin/buildTranslation.js b/bin/buildTranslation.js new file mode 100644 index 00000000..cf5bce83 --- /dev/null +++ b/bin/buildTranslation.js @@ -0,0 +1,4 @@ +import BuildTranslation from './buildTranslationClass.js'; + +const buildTranslation = new BuildTranslation(); +buildTranslation.run(); diff --git a/bin/buildTranslationClass.js b/bin/buildTranslationClass.js new file mode 100644 index 00000000..3e981769 --- /dev/null +++ b/bin/buildTranslationClass.js @@ -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'); + } +} diff --git a/bin/buildTranslations.js b/bin/buildTranslations.js deleted file mode 100644 index 92ef2f7a..00000000 --- a/bin/buildTranslations.js +++ /dev/null @@ -1,27 +0,0 @@ -import fs from 'fs'; -import path from 'path'; - -const localesDir = path.join('locales'); -const translations = {}; - -// Iterate through each language directory (e.g., 'en', 'fr') -fs.readdirSync(localesDir).forEach((locale) => { - translations[locale] = {}; - const localeDir = path.join(localesDir, 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 - translations[locale][namespace] = fileContents; - }); -}); - -// Output the result as a JS object -const output = `export const translations = ${JSON.stringify(translations, null, 2)};\n\nexport default translations;\n`; - -// Write the output to a JS file -fs.writeFileSync(path.join('src', 'script', 'translations.js'), output, 'utf8'); diff --git a/package.json b/package.json index f8355689..68484640 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/script/mainOptionPage.ts b/src/script/mainOptionPage.ts index 4abba339..00501c1f 100644 --- a/src/script/mainOptionPage.ts +++ b/src/script/mainOptionPage.ts @@ -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); }); diff --git a/src/script/translation.ts b/src/script/translation.ts index 6eb29459..4d524a60 100644 --- a/src/script/translation.ts +++ b/src/script/translation.ts @@ -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;