From bdbd37be33e9d42d8da09a0b606c94516f08d52d Mon Sep 17 00:00:00 2001 From: Aaronlamz Date: Thu, 3 Nov 2022 19:58:52 +0800 Subject: [PATCH 1/4] feat: support $n --- examples/vite-project/src/App.vue | 13 ++++++++++--- src/index.ts | 6 ++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/examples/vite-project/src/App.vue b/examples/vite-project/src/App.vue index f50d69e..da6dd57 100644 --- a/examples/vite-project/src/App.vue +++ b/examples/vite-project/src/App.vue @@ -7,6 +7,10 @@

Key[params]: {{ $t('params', param) }}

Key Array: {{ $t(['简体', '繁体', '英文']) }}

Key [global option]: {{ $t('global') }}

+

+ Key [support $n]: + {{ $t('support $n', 'param1', 'param2', 'param3', 'param4') }} +

@@ -53,19 +57,22 @@ export default { button: { add: 'i18nOption Add new' }, - params: (val: string) => `en i18n Option params: ${val}` + params: (val: string) => `en i18n Option params: ${val}`, + 'support $n': 'support $params: $1, $2, $3, $4' }, zhCHS: { button: { add: 'i18n选项 新增' }, - params: (val: string) => `简体 i18n选项 参数: ${val}` + params: (val: string) => `简体 i18n选项 参数: ${val}`, + 'support $n': '支持参数 $params: $1, $2, $3, $4' }, zhCHT: { button: { add: 'i18n选项 新增繁体' }, - params: (val: string) => `繁体 i18n选项 参数: ${val}` + params: (val: string) => `繁体 i18n选项 参数: ${val}`, + 'support $n': '支持參數 $params: $1, $2, $3, $4' } }, setup() { diff --git a/src/index.ts b/src/index.ts index dfd377a..b6e7e12 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,6 +36,12 @@ export function createI18n(options?: I18nOptions): I18nInstance { if (typeof message === 'function') { return message(...args) || key } + if (args.length && message) { + args.forEach((value, index) => { + const regexp = new RegExp(`\\$${index + 1}`, 'gi') + message = message.replace(regexp, value) + }) + } return message || key }, changeLocale(locale: string) { From f84cd31600d6181f03960d58c1c51849c6d9abb0 Mon Sep 17 00:00:00 2001 From: Aaronlamz Date: Thu, 3 Nov 2022 20:04:59 +0800 Subject: [PATCH 2/4] fix type --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index b6e7e12..a1249b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,7 +37,7 @@ export function createI18n(options?: I18nOptions): I18nInstance { return message(...args) || key } if (args.length && message) { - args.forEach((value, index) => { + args.forEach((value: string | number, index: number) => { const regexp = new RegExp(`\\$${index + 1}`, 'gi') message = message.replace(regexp, value) }) From 87bb0030efcbf3a1a4c7e3c0c8a3fd381fb08fc2 Mon Sep 17 00:00:00 2001 From: Aaronlamz Date: Thu, 3 Nov 2022 20:06:34 +0800 Subject: [PATCH 3/4] fix --- examples/vite-project/src/App.vue | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/vite-project/src/App.vue b/examples/vite-project/src/App.vue index da6dd57..e606ebb 100644 --- a/examples/vite-project/src/App.vue +++ b/examples/vite-project/src/App.vue @@ -58,21 +58,21 @@ export default { add: 'i18nOption Add new' }, params: (val: string) => `en i18n Option params: ${val}`, - 'support $n': 'support $params: $1, $2, $3, $4' + 'support $n': 'support $params: $1, $2, $3, $4,$5' }, zhCHS: { button: { add: 'i18n选项 新增' }, params: (val: string) => `简体 i18n选项 参数: ${val}`, - 'support $n': '支持参数 $params: $1, $2, $3, $4' + 'support $n': '支持参数 $params: $1, $2, $3, $4,$5' }, zhCHT: { button: { add: 'i18n选项 新增繁体' }, params: (val: string) => `繁体 i18n选项 参数: ${val}`, - 'support $n': '支持參數 $params: $1, $2, $3, $4' + 'support $n': '支持參數 $params: $1, $2, $3, $4,$5' } }, setup() { From cca1f5e4308524ff07ceaefb4af27f487672c1a8 Mon Sep 17 00:00:00 2001 From: Aaronlamz Date: Thu, 3 Nov 2022 20:37:22 +0800 Subject: [PATCH 4/4] fix type --- __tests__/i18n.spec.ts | 19 ++++++++++++++++--- examples/vite-project/src/App.vue | 6 ++++-- src/index.ts | 2 +- src/types.ts | 2 +- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/__tests__/i18n.spec.ts b/__tests__/i18n.spec.ts index f5b91ed..f50e25b 100644 --- a/__tests__/i18n.spec.ts +++ b/__tests__/i18n.spec.ts @@ -5,19 +5,22 @@ const defaultMessages = { en: { message: { hello: 'hello world', - param: (val: string) => `hello ${val}` + param: (val: string) => `hello ${val}`, + $n: 'replacement params: $1,$2' } }, zhCHS: { message: { hello: '你好 世界', - param: (val: string) => `你好 ${val}` + param: (val: string) => `你好 ${val}`, + $n: '替换参数: $1,$2' } }, ja: { message: { hello: 'こんにちは、世界', - param: (val: string) => `こんにちは ${val}` + param: (val: string) => `こんにちは ${val}`, + $n: 'パラメータの置換: $1,$2' } } } @@ -74,6 +77,10 @@ describe('I18n plugin tests', () => { }) }) + it('test empty key message', () => { + expect(plugin.t('')).toEqual('') + }) + it('test message', () => { expect(plugin.t('message.hello')).toEqual('hello world') }) @@ -82,6 +89,12 @@ describe('I18n plugin tests', () => { expect(plugin.t('message.param', 'world')).toEqual('hello world') }) + it('test $n replacement', () => { + expect(plugin.t('message.$n', 'param1', 'param2')).toEqual( + 'replacement params: param1,param2' + ) + }) + it('test message without key', () => { expect(plugin.t('')).toEqual('') }) diff --git a/examples/vite-project/src/App.vue b/examples/vite-project/src/App.vue index e606ebb..97534e6 100644 --- a/examples/vite-project/src/App.vue +++ b/examples/vite-project/src/App.vue @@ -7,9 +7,11 @@

Key[params]: {{ $t('params', param) }}

Key Array: {{ $t(['简体', '繁体', '英文']) }}

Key [global option]: {{ $t('global') }}

+

Key [empty]: {{ $t('') }}

- Key [support $n]: - {{ $t('support $n', 'param1', 'param2', 'param3', 'param4') }} + Key [support $n]:{{ + $t('support $n', 'param1', 'param2', 'param3', 'param4') + }}

diff --git a/src/index.ts b/src/index.ts index a1249b1..5d06ed4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,7 +20,7 @@ export function createI18n(options?: I18nOptions): I18nInstance { locales = initOptions.messages return { currentLocale: readonly(currentLocale), - t(key: string | string[], ...args: any): string { + t(key: string | string[], ...args: any[]): string { if (!key) return '' const locale = currentLocale.value let message: any diff --git a/src/types.ts b/src/types.ts index e0addc2..72a6825 100644 --- a/src/types.ts +++ b/src/types.ts @@ -28,6 +28,6 @@ export type I18nInstance = { currentLocale: UnwrapNestedRefs> changeLocale(locale: string): void addLocales(messages: Locales): void - t(key: string | string[], options?: any): string + t(key: string | string[], ...args: any[]): string install(app: App): void }