Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

fix: use ESM dynamic import and export default #101

Merged
merged 1 commit into from
Sep 1, 2023
Merged
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function getTokenSync(token: string, ...parameters): string;
export function getLanguages(): Promise<languages[]>;
export function taggedString(str: string, ...keys: any[]): (...keys: any[]) => string;
export function extend(language: string, tokens: Record<string, any>): void;
export function extendFromSystemPath(path: string): void;
export function extendFromSystemPath(path: string): Promise<void>;
```

> **Note**
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare namespace i18n {
export function getLanguages(): Promise<languages[]>;
export function taggedString(str: string, ...keys: any[]): (...keys: any[]) => string;
export function extend(language: string, tokens: Record<string, any>): void;
export function extendFromSystemPath(path: string): void;
export function extendFromSystemPath(path: string): Promise<void>;
}

export = i18n;
Expand Down
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Import Node.js Dependencies
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";

// Import Third-party Depedencies
import cacache from "cacache";
Expand Down Expand Up @@ -94,7 +95,7 @@ export function extend(extendLanguage, opts = {}) {
}
}

export function extendFromSystemPath(languagesDirPath) {
export async function extendFromSystemPath(languagesDirPath) {
if (!fs.existsSync(languagesDirPath)) {
throw new Error(`The ${languagesDirPath} directory does not exist on this project.`);
}
Expand All @@ -103,7 +104,11 @@ export function extendFromSystemPath(languagesDirPath) {

for (const file of files) {
const langName = path.basename(file.name, ".js");
const data = fs.readFileSync(path.join(languagesDirPath, file.name), "utf-8");
extend(langName, JSON.parse(data));
const fileLocation = path.join(languagesDirPath, file.name);

const i18nTokensFile = await import(pathToFileURL(fileLocation));
if ("default" in i18nTokensFile) {
extend(langName, i18nTokensFile.default);
}
}
}
9 changes: 5 additions & 4 deletions test/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ describe("extend", () => {
});

describe("extendFromSystemPath", () => {
it("extendFromSystemPath with non-existing languages directory", () => {
assert.throws(
it("extendFromSystemPath with non-existing languages directory", async() => {
await assert.rejects(
() => i18n.extendFromSystemPath(kI18nDir),
{
name: "Error",
Expand All @@ -164,10 +164,11 @@ describe("extendFromSystemPath", () => {
fs.mkdirSync(kI18nDir);
fs.writeFileSync(
path.join(kI18nDir, "french.js"),
JSON.stringify({ hello: "Bonjour" })
// eslint-disable-next-line quotes
`export default { hello: "Bonjour" }`
);

i18n.extendFromSystemPath(kI18nDir);
await i18n.extendFromSystemPath(kI18nDir);
await i18n.setLocalLang("french");

assert.deepEqual(await i18n.getToken("hello"), "Bonjour");
Expand Down