-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"i18nModuleInitialized": "i18n Module has been initialized correctly" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Parseable, ValidateProperty } from "parzival"; | ||
|
||
@Parseable() | ||
export default class I18nConfig { | ||
@ValidateProperty({ | ||
type: "string", | ||
}) | ||
baseLanguage!: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { debug, warn } from "@src/engine/utils/Logger"; | ||
import { I18nModule } from "../module"; | ||
|
||
export default async (i18n: I18nModule, lng: string, ns: string, msg: string) => { | ||
warn("Failed to load translation file", { lng, ns, msg }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { debug } from "@src/engine/utils/Logger"; | ||
import { I18nModule } from "../module"; | ||
|
||
export default async (i18n: I18nModule) => { | ||
debug("I18n Module Initialized"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { debug, info } from "@src/engine/utils/Logger"; | ||
import { I18nModule } from "../module"; | ||
|
||
export default async (i18n: I18nModule) => { | ||
info("I18n Loaded"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { debug, warn } from "@src/engine/utils/Logger"; | ||
import { I18nModule } from "../module"; | ||
|
||
export default async (i18n: I18nModule) => { | ||
warn("Missing key in translation file") | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Module from "@src/engine/modules"; | ||
import { debug } from "@src/engine/utils/Logger"; | ||
import { I18nModule } from "./module"; | ||
|
||
|
||
export default { | ||
name: "i18n", | ||
hooksInnerPath: "hooks", | ||
loadFunction: async (config) => { | ||
return new I18nModule() | ||
}, | ||
initFunction: async (ctx, config) => { | ||
await ctx.initialize(config) | ||
debug(ctx.t("default:i18nModuleInitialized") as string) | ||
} | ||
} satisfies Module<I18nModule, "i18n">; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import I18nConfig from "@src/config/modules/i18n"; | ||
import Module from "@src/engine/modules"; | ||
import { debug } from "@src/engine/utils/Logger"; | ||
import { getProcessPath } from "@src/engine/utils/Runtime"; | ||
import { EventEmitter } from "events"; | ||
import i18next, { i18n, TFunction } from "i18next"; | ||
import Backend, { FsBackendOptions } from 'i18next-fs-backend'; | ||
import path from "path"; | ||
|
||
const ReboundEvents = [ | ||
"initialized", | ||
"languageChanged", | ||
"loaded", | ||
"failedLoading", | ||
"missingKey", | ||
] as const | ||
|
||
|
||
export class I18nModule extends EventEmitter { | ||
private i18n: i18n | ||
private fixedTranslators: Map<string, TFunction> = new Map() | ||
constructor() { | ||
super() | ||
|
||
this.i18n = i18next.createInstance() | ||
for (const event of ReboundEvents) { | ||
this.i18n.on(event, (...args) => { | ||
this.emit(event, ...args) | ||
}) | ||
} | ||
} | ||
async initialize(config: I18nConfig) { | ||
await this.i18n | ||
.use(Backend) | ||
.init<FsBackendOptions>({ | ||
fallbackLng: config.baseLanguage, | ||
ns: ["default"], | ||
defaultNS: "default", | ||
backend: { | ||
loadPath: path.join(getProcessPath(), "/lang/{{lng}}/{{ns}}.json"), | ||
addPath: path.join(getProcessPath(), "/lang/{{lng}}/{{ns}}.missing.json"), | ||
} | ||
}) | ||
} | ||
translateTo(key: string, lang: string, opts?: any) { | ||
let translator = this.fixedTranslators.get(lang) | ||
if (!translator) { | ||
translator = this.i18n.getFixedT(lang) | ||
this.fixedTranslators.set(lang, translator) | ||
} | ||
return translator(key, opts) | ||
} | ||
t(key: string, opts?: any) { | ||
return this.i18n.t(key, opts) | ||
} | ||
} |