Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ out
*.tar.gz
*.7z
*.rar
.DS_Store
**/.DS_Store
.swc
.next
.
Expand Down
54 changes: 34 additions & 20 deletions packages/discord/src/services/i18n-loader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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);
Expand Down
Loading