This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.test.ts
More file actions
129 lines (117 loc) · 3.91 KB
/
i18n.test.ts
File metadata and controls
129 lines (117 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { beforeEach, describe, expect, it } from 'vitest'
import type { Accessor } from 'solid-js'
import { createContext, useContext } from 'solid-js'
import type { I18nObject, I18nOptions } from '../src/i18n'
import { defineI18n, useStaticMessage } from '../src/i18n'
import { useTick } from '../src/hooks'
import type { MessageType } from '../src/i18n/types'
/**
* initalize i18n
* @param options i18n options
* @see https://github.com/subframe7536/solid-dollar#i18n
*/
function $i18n<
Locale extends string = string,
Message extends MessageType<Locale> = any,
NumberKey extends string = string,
DatetimeKey extends string = string,
>(
options: I18nOptions<Locale, Message, NumberKey, DatetimeKey>,
): Accessor<I18nObject<Locale, Message, NumberKey, DatetimeKey>> {
const ctx = createContext<{
data: I18nObject<Locale, Message, NumberKey, DatetimeKey> | undefined
}>({ data: undefined })
return () => {
const _ = useContext(ctx)
if (_.data) {
return _.data
}
const data = defineI18n(options)
_.data = data
return data
}
}
describe('i18n', () => {
const en = {
text: 'text',
var: 'welcome {name}, last login: {num}(1=one day|2-4,6=a few days|*=$ days) ago',
nest: {
text: 'nest {value}',
},
useless: 'useless',
} as const
const zh = {
text: '文本',
var: '欢迎 {name}, 上次登录: {num} 天前',
nest: {
text: '嵌套 {value}',
},
} as const
const useI18n = $i18n({
message: useStaticMessage({ en, zh }),
defaultLocale: 'en',
numberFormats: {
en: {
currency: { style: 'currency', currency: 'USD' },
},
zh: {
currency: { style: 'currency', currency: 'CNY' },
},
},
datetimeFormats: {
en: {
short: { dateStyle: 'short' },
long: { dateStyle: 'long' },
custom: d => d.getTime().toString(),
},
zh: {
short: { dateStyle: 'short' },
long: { dateStyle: 'long' },
custom: d => d.getTime().toString(),
},
},
})
const { availableLocales, locale, $t, $d, $n, $scopeT } = useI18n()
const $scopeTranslate = $scopeT('nest')
beforeEach(() => {
locale.$set('en')
})
async function changeLocale() {
locale.$set('zh')
await useTick()
}
it('translation', async () => {
expect(availableLocales).toStrictEqual(['en', 'zh'])
expect($t('text')).toBe('text')
expect($scopeTranslate('text', { value: 1 })).toBe('nest 1')
await changeLocale()
expect($t('text')).toBe('文本')
expect($t('nest.text', { value: 1 })).toBe('嵌套 1')
})
it('variable', async () => {
expect($t('var', { name: 'test', num: 1 })).toBe('welcome test, last login: one day ago')
expect($t('var', { name: 'test', num: 2 })).toBe('welcome test, last login: a few days ago')
expect($t('var', { name: 'test', num: 3 })).toBe('welcome test, last login: a few days ago')
expect($t('var', { name: 'test', num: 4 })).toBe('welcome test, last login: a few days ago')
expect($t('var', { name: 'test', num: 5 })).toBe('welcome test, last login: 5 days ago')
expect($t('var', { name: 'test', num: 6 })).toBe('welcome test, last login: a few days ago')
await changeLocale()
expect($t('var', { name: 'test', num: 0 })).toBe('欢迎 test, 上次登录: 0 天前')
expect($t('var', { name: 'test', num: 4 })).toBe('欢迎 test, 上次登录: 4 天前')
})
it('number', async () => {
expect($n(1, 'currency')).toBe('$1.00')
await changeLocale()
expect($n(1, 'currency')).toBe('¥1.00')
})
it('date', async () => {
const date = new Date('2000-01-01')
expect($d(date, 'short')).toBe('1/1/00')
expect($d(date, 'long')).toBe('January 1, 2000')
expect($d(date, 'custom')).toBe('946684800000')
await changeLocale()
expect($d(date, 'short')).toBe('2000/1/1')
expect($d(date, 'long')).toBe('2000年1月1日')
expect($d(date, 'custom')).toBe('946684800000')
})
})