Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Aaronlamz/vue-next-i18n into main
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronlamz committed Nov 3, 2022
2 parents d657a15 + 98a2cac commit bd63640
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
19 changes: 16 additions & 3 deletions __tests__/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
}
Expand Down Expand Up @@ -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')
})
Expand All @@ -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('')
})
Expand Down
15 changes: 12 additions & 3 deletions examples/vite-project/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<p>Key[params]: {{ $t('params', param) }}</p>
<p>Key Array: {{ $t(['简体', '繁体', '英文']) }}</p>
<p>Key [global option]: {{ $t('global') }}</p>
<p>Key [empty]: {{ $t('') }}</p>
<p>
Key [support $n]:{{
$t('support $n', 'param1', 'param2', 'param3', 'param4')
}}
</p>
<p>
<button @click="swtchLang('en')">Change Lang to en</button>
</p>
Expand Down Expand Up @@ -53,19 +59,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,$5'
},
zhCHS: {
button: {
add: 'i18n选项 新增'
},
params: (val: string) => `简体 i18n选项 参数: ${val}`
params: (val: string) => `简体 i18n选项 参数: ${val}`,
'support $n': '支持参数 $params: $1, $2, $3, $4,$5'
},
zhCHT: {
button: {
add: 'i18n选项 新增繁体'
},
params: (val: string) => `繁体 i18n选项 参数: ${val}`
params: (val: string) => `繁体 i18n选项 参数: ${val}`,
'support $n': '支持參數 $params: $1, $2, $3, $4,$5'
}
},
setup() {
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: string | number, index: number) => {
const regexp = new RegExp(`\\$${index + 1}`, 'gi')
message = message.replace(regexp, value)
})
}
return message || key
},
changeLocale(locale: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export type I18nInstance = {
currentLocale: UnwrapNestedRefs<Ref<string>>
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
}

0 comments on commit bd63640

Please sign in to comment.