-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
t.test.ts
89 lines (75 loc) · 3.15 KB
/
t.test.ts
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
import i18next, { TFunction } from 'i18next';
function defaultNamespaceUsage(t: TFunction) {
t('bar');
t('foo');
}
function namedDefaultNamespaceUsage(t: TFunction<'alternate'>) {
t('foobar.barfoo');
t('foobar.deep.deeper.deeeeeper');
// t('foobar.deep.deeper').deeeeeper; // i18next would say: "key 'foobar.deep.deeper (en)' returned an object instead of string."
t('foobar.deep.deeper', { returnObjects: true }).deeeeeper;
}
function arrayNamespace(t: TFunction<['custom', 'alternate']>) {
t('alternate:baz');
t('baz', { ns: 'alternate' });
// t('alternate:foobar.deep').deeper.deeeeeper; // i18next would say: "key 'foobar.deep (en)' returned an object instead of string."
t('alternate:foobar.deep', { returnObjects: true }).deeper.deeeeeper;
t('custom:bar');
t('bar', { ns: 'custom' });
t('bar');
}
// @ts-expect-error
function expectErrorWhenNamespaceDoesNotExist(t: TFunction<'foo'>) {}
function expectErrorWhenKeyNotInNamespace(t: TFunction<'alternate'>) {
// @ts-expect-error
t('bar');
t('foobar.barfoo');
t('foobar.barfoo', 'some default value');
t('foobar.barfoo', { defaultValue: 'some default value' });
t('not.yet.existing', 'some default value');
t('not.yet.existing', { defaultValue: 'some default value' });
}
function i18nextTUsage() {
i18next.t('foobar.barfoo', { ns: 'alternate' });
i18next.t('alternate:foobar.barfoo');
// i18next.t('alternate:foobar.deep').deeper.deeeeeper; // i18next would say: "key 'foobar.deep (en)' returned an object instead of string."
i18next.t('alternate:foobar.deep', { returnObjects: true }).deeper.deeeeeper;
i18next.t('foobar.deep', { ns: 'alternate', returnObjects: true }).deeper.deeeeeper;
i18next.t('foobar.deep', { ns: 'alternate', returnObjects: true, returnDetails: true }).res;
i18next.t('custom:bar').trim();
i18next.t('bar', { ns: 'custom' }).trim();
// @ts-expect-error
i18next.t('bar', { ns: 'alternate' });
// i18next.t('bar', {}).trim();
i18next.t('bar').trim();
i18next.t('baz.bing').trim();
i18next.t('alternate:foobar.barfoo').trim();
i18next.t('custom:bar', { defaultValue: 'some default value' });
i18next.t('custom:bar', 'some default value');
i18next.t('bar', { ns: 'custom', defaultValue: 'some default value' });
i18next.t('bar', { defaultValue: 'some default value' });
i18next.t('bar', 'some default value');
i18next.t('custom:no.existing.yet', { defaultValue: 'some default value' });
i18next.t('custom:no.existing.yet', 'some default value');
i18next.t('no.existing.yet', { ns: 'custom', defaultValue: 'some default value' });
i18next.t('no.existing.yet', { defaultValue: 'some default value' });
i18next.t('no.existing.yet', 'some default value');
}
function expectErrorWhenInvalidKeyWithI18nextT() {
// @ts-expect-error
i18next.t('custom:test');
}
function expectErrorWhenInvalidNamespaceWithI18nextT() {
// @ts-expect-error
i18next.t('test:bar');
}
function i18nextTPluralsUsage() {
i18next.t('plurals:foo', { count: 1 }).trim();
i18next.t('plurals:foo_many', { count: 10 }).trim();
}
// @ts-expect-error
function returnNullWithFalseValue(t: TFunction<string>) {
function fn(value: null) {}
// @ts-expect-error
fn(t('foo'));
}