summaryrefslogtreecommitdiff
path: root/web/test/i18n.test.ts
blob: 764b50197130db8e727665ffb7117c085febd1dc (plain)
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
/**
 * i18n (Phase 6): all ten locale tables load, `t()` falls back to English
 * (mirroring gen-l10n), and the locale store switches the active table.
 */
import { describe, expect, it } from 'vitest';
import { get } from 'svelte/store';
import { en } from '$lib/i18n/en';
import { de } from '$lib/i18n/de';
import { es } from '$lib/i18n/es';
import { fr } from '$lib/i18n/fr';
import { ko } from '$lib/i18n/ko';
import { pl } from '$lib/i18n/pl';
import { ru } from '$lib/i18n/ru';
import { th } from '$lib/i18n/th';
import { zh } from '$lib/i18n/zh';
import { zh_Hant } from '$lib/i18n/zh_Hant';
import {
	availableLocales,
	locale,
	t,
	hasLocaleKey,
	type Locale
} from '$lib/i18n/index';

const tables: Record<Locale, Record<string, string>> = {
	en,
	de,
	es,
	fr,
	ko,
	pl,
	ru,
	th,
	zh,
	zh_Hant
};

describe('i18n', () => {
	it('offers the ten locales from the reference app', () => {
		expect(availableLocales).toEqual([
			'en',
			'de',
			'es',
			'fr',
			'ko',
			'pl',
			'ru',
			'th',
			'zh',
			'zh_Hant'
		]);
	});

	it('covers every table with non-empty strings (en ⊇ others, as in the arbs)', () => {
		for (const l of availableLocales) {
			const table = tables[l];
			const entries = Object.entries(table);
			expect(entries.length, `${l} has keys`).toBeGreaterThan(100);
			for (const [key, value] of entries) {
				expect(typeof value, `${l}.${key}`).toBe('string');
				expect((value as string).length, `${l}.${key} non-empty`).toBeGreaterThan(0);
			}
		}
	});

	it('translates from the active locale and falls back to English', () => {
		locale.set('de');
		// `settingsNoInit` exists in both en and de (with different text).
		expect(t('settingsNoInit')).toBe(de['settingsNoInit']);
		expect(t('settingsNoInit')).not.toBe(en['settingsNoInit']);
		locale.set('en');
	});

	it('falls back to English for a key a locale lacks', () => {
		locale.set('de');
		// A web-only key: only in the English table.
		expect(t('styleGloomhaven')).toBe('Gloomhaven');
		expect(hasLocaleKey('styleGloomhaven')).toBe(false);
		expect(hasLocaleKey('settingsNoInit')).toBe(true);
		locale.set('en');
	});

	it('returns the key itself when it is unknown everywhere', () => {
		expect(t('noSuchKeyAnywhere')).toBe('noSuchKeyAnywhere');
	});

	it('substitutes variables', () => {
		expect(t('undoWithDescription', { description: 'Add Blinkblade' })).toBe(
			'Undo: Add Blinkblade'
		);
	});
});