forked from lingui/js-lingui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-t.ts
More file actions
186 lines (186 loc) · 4.49 KB
/
js-t.ts
File metadata and controls
186 lines (186 loc) · 4.49 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
export default [
{
name: "Macro is used in expression assignment",
input: `
import { t } from '@lingui/macro';
const a = t\`Expression assignment\`;
`,
expected: `
import { i18n } from "@lingui/core";
const a =
/*i18n*/
i18n._("Expression assignment")
`,
},
{
name: "Variables are replaced with named arguments",
input: `
import { t } from '@lingui/macro';
t\`Variable \${name}\`;
`,
expected: `
import { i18n } from "@lingui/core";
/*i18n*/
i18n._("Variable {name}", {
name: name
})
`,
},
{
name: "Variables with scaped template literals are correctly formatted",
input: `
import { t } from '@lingui/macro';
t\`Variable \\\`\${name}\\\`\`;
`,
expected: `
import { i18n } from "@lingui/core";
/*i18n*/
i18n._("Variable \`{name}\`", {
name: name
})
`,
},
{
name: "Variables with scaped double quotes are correctly formatted",
input: `
import { t } from '@lingui/macro';
t\`Variable \"name\" \`;
`,
expected: `
import { i18n } from "@lingui/core";
/*i18n*/
i18n._("Variable \\"name\\"")
`,
},
{
name: "Variables should be deduplicated",
input: `
import { t } from '@lingui/macro';
t\`\${duplicate} variable \${duplicate}\`;
`,
expected: `
import { i18n } from "@lingui/core";
/*i18n*/
i18n._("{duplicate} variable {duplicate}", {
duplicate: duplicate
})
`,
},
{
name:
"Anything variables except simple identifiers are used as positional arguments",
input: `
import { t } from '@lingui/macro';
t\`
Property \${props.name},\\
function \${random()},\\
array \${array[index]},\\
constant \${42},\\
object \${new Date()}\\
anything \${props.messages[index].value()}
\`
`,
expected: `
import { i18n } from "@lingui/core";
/*i18n*/
i18n._(
"Property {0}, function {1}, array {2}, constant {3}, object {4} anything {5}", {
0: props.name,
1: random(),
2: array[index],
3: 42,
4: new Date(),
5: props.messages[index].value()
}
);
`,
},
{
name: "Newlines are preserved",
input: `
import { t } from '@lingui/macro';
t\`Multiline
string\`
`,
expected: `
import { i18n } from "@lingui/core";
/*i18n*/
i18n._("Multiline\\nstring")
`,
},
{
name: "Support template strings in t macro message",
input: `
import { t } from '@lingui/macro'
const msg = t({ message: \`Hello \${name}\` })
`,
expected: `import { i18n } from "@lingui/core";
const msg =
i18n._(/*i18n*/
{
id: "Hello {name}",
values: {
name: name,
},
});
`,
},
{
name: "Support id and comment in t macro as callExpression",
input: `
import { t } from '@lingui/macro'
const msg = t({ id: 'msgId', comment: 'description for translators', message: plural(val, { one: '...', other: '...' }) })
`,
expected: `import { i18n } from "@lingui/core";
const msg =
i18n._(/*i18n*/
{
id: "msgId",
comment: "description for translators",
message: "{val, plural, one {...} other {...}}",
values: {
val: val,
},
});
`,
},
{
name: "Support id with message interpolation",
input: `
import { t } from '@lingui/macro'
const msg = t({ id: 'msgId', message: \`Some \${value}\` })
`,
expected: `import { i18n } from "@lingui/core";
const msg =
i18n._(/*i18n*/
{
id: "msgId",
message: "Some {value}",
values: {
value: value,
},
});
`,
},
{
name: "Support id in template literal",
input: `
import { t } from '@lingui/macro'
const msg = t({ id: \`msgId\` })
`,
expected: `import { i18n } from "@lingui/core";
const msg =
i18n._(/*i18n*/
{
id: \`msgId\`
});
`,
},
{
name: "Newlines after continuation character are removed",
filename: "js-t-continuation-character.js",
},
{
filename: "js-t-var/js-t-var.js",
},
]