diff --git a/.gitignore b/.gitignore index 9c83ea758..01e456184 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,8 @@ out *.tar.gz *.7z *.rar +.DS_Store +**/.DS_Store .swc .next . diff --git a/packages/discord/src/services/i18n-loader.service.ts b/packages/discord/src/services/i18n-loader.service.ts index c73d6863a..27c835291 100644 --- a/packages/discord/src/services/i18n-loader.service.ts +++ b/packages/discord/src/services/i18n-loader.service.ts @@ -6,26 +6,24 @@ * https://github.com/Statsify/statsify/blob/main/LICENSE */ -import Backend from "i18next-fs-backend"; -import i18next from "i18next"; -import { Service } from "typedi"; -import { abbreviationNumber } from "@statsify/util"; -import { readdir } from "node:fs/promises"; +import Backend from "i18next-fs-backend"; +import i18next from "i18next"; +import { Service } from "typedi"; +import { abbreviationNumber } from "@statsify/util"; +import { readdir } from "node:fs/promises"; const DEFAULT_LANGUAGE = "en-US"; @Service() -export class I18nLoaderService { - private languages: string[] = []; - private namespaces: string[] = []; - - public async init() { - this.languages = await readdir("../../locales"); - this.namespaces = (await readdir(`../../locales/${DEFAULT_LANGUAGE}/`)).map((p) => - p.replace(".json", "") - ); - - await i18next.use(Backend).init({ +export class I18nLoaderService { + private languages: string[] = []; + private namespaces: string[] = []; + + public async init() { + this.languages = await this.getLanguages(); + this.namespaces = await this.getNamespaces(); + + await i18next.use(Backend).init({ backend: { loadPath: "../../locales/{{lng}}/{{ns}}.json", }, @@ -42,10 +40,26 @@ export class I18nLoaderService { format: this.format, escapeValue: false, }, - }); - } - - private format(value: any, format?: string | undefined, lng?: string): string { + }); + } + + private async getLanguages() { + const entries = await readdir("../../locales", { withFileTypes: true }); + + return entries + .filter((entry) => entry.isDirectory() && !entry.name.startsWith(".")) + .map((entry) => entry.name); + } + + private async getNamespaces() { + const entries = await readdir(`../../locales/${DEFAULT_LANGUAGE}/`, { withFileTypes: true }); + + return entries + .filter((entry) => entry.isFile() && entry.name.endsWith(".json")) + .map((entry) => entry.name.replace(/\.json$/, "")); + } + + private format(value: any, format?: string | undefined, lng?: string): string { switch (format) { case "number": { const hasDecimals = value >= 1_000_000 || !Number.isInteger(+value);