-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathi18n-service.test.ts
More file actions
191 lines (168 loc) · 6.7 KB
/
Copy pathi18n-service.test.ts
File metadata and controls
191 lines (168 loc) · 6.7 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { describe, it, expect } from 'vitest';
import type { II18nService } from './i18n-service';
import type { AppTranslationBundle, TranslationCoverageResult, TranslationDiffItem } from '../system/translation.zod';
describe('I18n Service Contract', () => {
it('should allow a minimal II18nService implementation with required methods', () => {
const service: II18nService = {
t: (_key, _locale, _params?) => '',
getTranslations: (_locale) => ({}),
loadTranslations: (_locale, _translations) => {},
getLocales: () => [],
};
expect(typeof service.t).toBe('function');
expect(typeof service.getTranslations).toBe('function');
expect(typeof service.loadTranslations).toBe('function');
expect(typeof service.getLocales).toBe('function');
});
it('should allow a full implementation with optional methods', () => {
const service: II18nService = {
t: () => '',
getTranslations: () => ({}),
loadTranslations: () => {},
getLocales: () => [],
getDefaultLocale: () => 'en',
setDefaultLocale: (_locale) => {},
};
expect(service.getDefaultLocale).toBeDefined();
expect(service.setDefaultLocale).toBeDefined();
});
it('should translate a key', () => {
const translations = new Map<string, Record<string, unknown>>();
translations.set('en', { 'objects.account.label': 'Account' });
translations.set('zh-CN', { 'objects.account.label': '客户' });
const service: II18nService = {
t: (key, locale) => {
const bundle = translations.get(locale);
return (bundle?.[key] as string) ?? key;
},
getTranslations: (locale) => translations.get(locale) ?? {},
loadTranslations: (locale, data) => { translations.set(locale, data); },
getLocales: () => Array.from(translations.keys()),
};
expect(service.t('objects.account.label', 'en')).toBe('Account');
expect(service.t('objects.account.label', 'zh-CN')).toBe('客户');
expect(service.t('missing.key', 'en')).toBe('missing.key');
});
it('should load and retrieve translations', () => {
const store = new Map<string, Record<string, unknown>>();
const service: II18nService = {
t: (key, locale) => {
const bundle = store.get(locale);
return (bundle?.[key] as string) ?? key;
},
getTranslations: (locale) => store.get(locale) ?? {},
loadTranslations: (locale, data) => { store.set(locale, data); },
getLocales: () => Array.from(store.keys()),
};
service.loadTranslations('ja', { greeting: 'こんにちは' });
expect(service.getLocales()).toContain('ja');
expect(service.getTranslations('ja')).toEqual({ greeting: 'こんにちは' });
expect(service.t('greeting', 'ja')).toBe('こんにちは');
});
it('should manage default locale', () => {
let defaultLocale = 'en';
const service: II18nService = {
t: () => '',
getTranslations: () => ({}),
loadTranslations: () => {},
getLocales: () => ['en', 'zh-CN', 'ja'],
getDefaultLocale: () => defaultLocale,
setDefaultLocale: (locale) => { defaultLocale = locale; },
};
expect(service.getDefaultLocale!()).toBe('en');
service.setDefaultLocale!('zh-CN');
expect(service.getDefaultLocale!()).toBe('zh-CN');
});
it('should allow implementation with getAppBundle and loadAppBundle', () => {
const bundles = new Map<string, AppTranslationBundle>();
const service: II18nService = {
t: () => '',
getTranslations: () => ({}),
loadTranslations: () => {},
getLocales: () => Array.from(bundles.keys()),
getAppBundle: (locale) => bundles.get(locale),
loadAppBundle: (locale, bundle) => { bundles.set(locale, bundle); },
};
const zhBundle: AppTranslationBundle = {
o: {
account: {
label: '客户',
fields: { name: { label: '客户名称' } },
_views: { all_accounts: { label: '全部客户' } },
},
},
messages: { 'common.save': '保存' },
};
service.loadAppBundle!('zh-CN', zhBundle);
const loaded = service.getAppBundle!('zh-CN');
expect(loaded).toBeDefined();
expect(loaded?.o?.account.label).toBe('客户');
expect(loaded?.o?.account._views?.all_accounts.label).toBe('全部客户');
expect(loaded?.messages?.['common.save']).toBe('保存');
});
it('should allow implementation with getCoverage', () => {
const service: II18nService = {
t: () => '',
getTranslations: () => ({}),
loadTranslations: () => {},
getLocales: () => ['en', 'zh-CN'],
getCoverage: (locale, objectName?) => {
const result: TranslationCoverageResult = {
locale,
objectName,
totalKeys: 50,
translatedKeys: 45,
missingKeys: 5,
redundantKeys: 0,
staleKeys: 0,
coveragePercent: 90,
items: [
{ key: 'o.account.fields.website.label', status: 'missing', objectName: 'account', locale },
],
};
return result;
},
};
const coverage = service.getCoverage!('zh-CN', 'account');
expect(coverage.locale).toBe('zh-CN');
expect(coverage.objectName).toBe('account');
expect(coverage.coveragePercent).toBe(90);
expect(coverage.missingKeys).toBe(5);
expect(coverage.items).toHaveLength(1);
expect(coverage.items[0].status).toBe('missing');
});
it('should keep backward compatibility — new methods are optional', () => {
const minimalService: II18nService = {
t: (_key, _locale) => '',
getTranslations: (_locale) => ({}),
loadTranslations: (_locale, _translations) => {},
getLocales: () => [],
};
expect(minimalService.getAppBundle).toBeUndefined();
expect(minimalService.loadAppBundle).toBeUndefined();
expect(minimalService.getCoverage).toBeUndefined();
expect(minimalService.suggestTranslations).toBeUndefined();
});
it('should allow implementation with suggestTranslations', async () => {
const service: II18nService = {
t: () => '',
getTranslations: () => ({}),
loadTranslations: () => {},
getLocales: () => ['en', 'zh-CN'],
suggestTranslations: async (_locale, items) => {
return items.map(item => ({
...item,
aiSuggested: `AI翻译: ${item.key}`,
aiConfidence: 0.85,
}));
},
};
const items: TranslationDiffItem[] = [
{ key: 'o.account.fields.website.label', status: 'missing', locale: 'zh-CN' },
];
const suggestions = await service.suggestTranslations!('zh-CN', items);
expect(suggestions).toHaveLength(1);
expect(suggestions[0].aiSuggested).toBe('AI翻译: o.account.fields.website.label');
expect(suggestions[0].aiConfidence).toBe(0.85);
});
});