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

Merge instead for extend #113

Merged
merged 1 commit into from
Dec 3, 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 @@ -69,7 +69,7 @@ export function extend(language: string, tokens: Record<string, any>): void;
export function extendFromSystemPath(path: string): Promise<void>;
```

> **Note**
> [!NOTE]
> Local lang must be updated otherwise `getTokenSync()` will throws. Make sure to use `await i18n.getLocalLang()` before any synchronous usage.

## Generate documentation
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { pathToFileURL } from "node:url";
// Import Third-party Depedencies
import cacache from "cacache";
import lodashGet from "lodash.get";
import deepmerge from "deepmerge";

// Import Internals
import { CACHE_PATH, CURRENT_LANG } from "./src/constants.js";
Expand Down Expand Up @@ -88,7 +89,7 @@ export function getTokenSync(token, ...params) {

export function extend(extendLanguage, opts = {}) {
if (extendLanguage in languages) {
Object.assign(languages[extendLanguage], opts);
languages[extendLanguage] = deepmerge(languages[extendLanguage], opts);
}
else {
languages[extendLanguage] = opts;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"cacache": "^18.0.0",
"deepmerge": "^4.3.1",
"lodash.get": "^4.4.2"
}
}
11 changes: 11 additions & 0 deletions test/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ describe("extend", () => {
);
});

it("should deeply extend the properties", async() => {
i18n.extend("english", { contributor: { name: "Fraxken" } });
i18n.extend("english", { contributor: { email: "gentilhomme.thomas@gmail.com" } });
await i18n.setLocalLang("english");

assert.deepEqual(
await i18n.getToken("contributor"),
{ name: "Fraxken", email: "gentilhomme.thomas@gmail.com" }
);
});

it("should add a new language if it doesn't exist", async() => {
i18n.extend("spanish", { welcome: "Bienvenido" });

Expand Down