Skip to content

Fix #9; adds static "type" property to conform to i18next API #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
"@types/lodash.set": "^4.3.6",
"@types/webpack-env": "^1.15.1",
"eslint": "^6.8.0",
"i18next": "^19.3.3",
"prettier": "^1.19.1",
"ts-node": "^8.7.0",
"typescript": "^3.8.3",
"prettier": "^1.19.1"
"typescript": "^3.8.3"
},
"peerDependencies": {
"@types/webpack-env": "^1.15.1"
},
"dependencies": {
"i18next": "^19.3.3",
"lodash.set": "^4.3.2",
"tslib": "^1.11.1"
}
Expand Down
19 changes: 13 additions & 6 deletions src/backend-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ReadCallback, Services, Module, MultiReadCallback } from "i18next";
import type { ReadCallback, Services, Module, MultiReadCallback } from "i18next";
import set from "lodash.set";

export interface WebpackBackendOptions {
context: __WebpackModuleApi.RequireContext | undefined;
}

export class WebpackBackend implements Module {
public static readonly type = "backend";
public readonly type = "backend";
private jsons: __WebpackModuleApi.RequireContext | null = null;
private keys: string[] = [];
Expand Down Expand Up @@ -50,8 +51,8 @@ export class WebpackBackend implements Module {
const translations = {};

await Promise.all(
namespaces.map(async namespace => {
return Promise.all(
namespaces.map(namespace =>
Promise.all(
languages.map(async lang => {
const builtKey = `./${lang}/${namespace}.json`;
if (this.jsons == null) {
Expand All @@ -65,14 +66,20 @@ export class WebpackBackend implements Module {
return;
}
if (this.keys.includes(builtKey) === false) {
console.error(new Error(`Namespace "${namespace}" for language "${lang}" was not found!`));
callback(
new Error(`Namespace "${namespace}" for language "${lang}" was not found!`),
// TODO: Fix this when types are up to date with newest implementation.
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
null
);
return;
}
const json = await this.jsons(builtKey);
set(translations, `${lang}.${namespace}`, json);
})
);
})
)
)
);

if (Object.keys(translations).length === 0) {
Expand Down