diff --git a/package.json b/package.json index 81243ef..cb90a43 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "scripts": { "prepublishOnly": "pnpm run build", "lint": "eslint --ext .ts,.tsx . && prettier --check .", - "test": "vitest --coverage", + "test": "vitest --coverage --coverage.include=src/**", "test:ci": "tsc --noEmit && vitest --coverage --coverage.reporter=lcov --coverage.include=src/**", "docs": "typedoc --options typedoc.json", "build": "cross-env NODE_ENV=production tsup --onSuccess \"node scripts/build-umd.mjs\"", diff --git a/src/i18n.ts b/src/i18n.ts index 3487690..80b42db 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -97,8 +97,12 @@ export class I18n { if (args) { const option = args[":option"]; if (option != null) { - const newKey = `${key}.${option}`; - key = flatLocale[newKey] ? newKey : `${key}.other`; + let newKey: string; + key = + flatLocale[(newKey = `${key}.${option}`)] || + flatLocale[(newKey = `${key}.other`)] + ? newKey + : key; } let fn = localeFns.get(key); fn || diff --git a/test/i18n.test.ts b/test/i18n.test.ts index 288d7e4..2e03815 100644 --- a/test/i18n.test.ts +++ b/test/i18n.test.ts @@ -152,6 +152,17 @@ describe("template t function", () => { expect(i18n.t("apple", { ":option": 3 })).toBe("3 apples"); }); + it("should support :option without sub keys", () => { + const locales: Locales = { + en: { + message: "{{:option}} world", + }, + }; + const i18n = new I18n("en", locales); + expect(i18n.t("message", { ":option": "hello" })).toBe("hello world"); + expect(i18n.t("message", { ":option": "bye" })).toBe("bye world"); + }); + it("should return key if message not exists", () => { const i18n = new I18n("en", {}); expect(i18n.t("fruit")).toBe("fruit");