Skip to content

Commit 91fc2b3

Browse files
committed
⚗️ Inject translations with webpack
1 parent 2e1ad4c commit 91fc2b3

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

bin/webpack.common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs-extra';
22
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
33
import webpack from 'webpack';
4+
import translations from '../src/script/translations.js';
45

56
const BUILD = fs.readJsonSync('.build.json');
67

@@ -35,6 +36,7 @@ export default {
3536
},
3637
plugins: [
3738
new webpack.DefinePlugin({ __BUILD__: JSON.stringify(BUILD) }),
39+
new webpack.DefinePlugin({ __TRANSLATIONS__: JSON.stringify(translations) }),
3840
],
3941
resolve: {
4042
extensions: ['.js', '.ts'],

src/script/translation.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import i18next, { i18n } from 'i18next';
2-
import translations from '@APF/translations.js';
32
import { stringArray } from '@APF/lib/helper';
43

4+
// __TRANSLATIONS__ is injected by webpack from ROOT/src/script/
5+
/* eslint-disable-next-line @typescript-eslint/naming-convention */
6+
declare const __TRANSLATIONS__: any;
7+
58
export default class Translation {
69
i18next: i18n;
710

11+
static readonly translations = typeof __TRANSLATIONS__ == 'undefined' ? { common: {} } : __TRANSLATIONS__;
12+
813
//#region Class reference helpers
914
// Can be overridden in children classes
1015
get Class() { return (this.constructor as typeof Translation); }
@@ -17,7 +22,7 @@ export default class Translation {
1722
lng: language,
1823
fallbackLng: 'en',
1924
ns: namespaces,
20-
resources: translations
25+
resources: this.Class.translations,
2126
});
2227
}
2328

0 commit comments

Comments
 (0)