1 import { describe, expect, test } from "vitest"
2
3 import {
4 findMissing,
5 getPlaceholders,
6 mergeLevel,
7 } from "../sync-translations.js"
8
9 const en = {
10 $schema: "./$schema.json",
11 general: {
12 save: "Save",
13 cancel: "Cancel",
14 items_one: "{{count}} item",
15 items_other: "{{count}} items",
16 },
17 orders: {
18 title: "Orders",
19 status: {
20 pending: "Pending",
21 paid: "Paid",
22 },
23 },
24 }
25
26 function merge(localeObj: any, translated: any, forms = ["one", "other"]) {
27 const errors: string[] = []
28 const result = mergeLevel(en, localeObj, translated, forms, [], errors)
29 return { result, errors }
30 }
31
32 describe("getPlaceholders", () => {
33 test("returns sorted i18next placeholders and Trans tags", () => {
34 expect(
35 getPlaceholders("Available in <0>{{ x }}</0> of <1>{{y}}</1><2/>", false)
36 ).toEqual(["</0>", "</1>", "<0>", "<1>", "<2/>", "{{x}}", "{{y}}"])
37 })
38
39 test("ignores {{count}} for plural keys only", () => {
40 expect(getPlaceholders("{{count}} of {{name}}", true)).toEqual(["{{name}}"])
41 expect(getPlaceholders("{{count}} of {{name}}", false)).toEqual([
42 "{{count}}",
43 "{{name}}",
44 ])
45 })
46 })
47
48 describe("findMissing", () => {
49 test("returns nothing when the locale is complete", () => {
50 const locale = {
51 $schema: "./$schema.json",
52 general: {
53 save: "Speichern",
54 cancel: "Abbrechen",
55 items_one: "{{count}} Artikel",
56 items_other: "{{count}} Artikel",
57 },
58 orders: {
59 title: "Bestellungen",
60 status: { pending: "Ausstehend", paid: "Bezahlt" },
61 },
62 }
63
64 expect(findMissing(en, locale, ["one", "other"])).toEqual({
65 missing: {},
66 count: 0,
67 })
68 })
69
70 test("returns missing leaves and whole missing sections with English values", () => {
71 const locale = {
72 general: {
73 save: "Speichern",
74 cancel: "",
75 items_one: "{{count}} Artikel",
76 },
77 }
78
79 expect(findMissing(en, locale, ["one", "other"])).toEqual({
80 missing: {
81 general: { cancel: "Cancel", items_other: "{{count}} items" },
82 orders: {
83 title: "Orders",
84 status: { pending: "Pending", paid: "Paid" },
85 },
86 },
87 count: 5,
88 })
89 })
90
91 test("expands plural groups to the locale's forms using _other as the source", () => {
92 const locale = {
93 general: { save: "Zapisz", cancel: "Anuluj" },
94 orders: {
95 title: "Zamówienia",
96 status: { pending: "Oczekuje", paid: "Opłacone" },
97 },
98 }
99
100 expect(
101 findMissing(en, locale, ["one", "few", "many", "other"]).missing
102 ).toEqual({
103 general: {
104 items_one: "{{count}} item",
105 items_few: "{{count}} items",
106 items_many: "{{count}} items",
107 items_other: "{{count}} items",
108 },
109 })
110 })
111
112 test("drops English plural forms the locale doesn't use", () => {
113 const locale = {
114 general: { save: "保存", cancel: "取消" },
115 orders: { title: "订单", status: { pending: "待处理", paid: "已支付" } },
116 }
117
118 expect(findMissing(en, locale, ["other"]).missing).toEqual({
119 general: { items_other: "{{count}} items" },
120 })
121 })
122
123 test("treats a string where en has an object as missing the whole section", () => {
124 const locale = {
125 general: {
126 save: "Speichern",
127 cancel: "Abbrechen",
128 items_one: "{{count}} Artikel",
129 items_other: "{{count}} Artikel",
130 },
131 orders: { title: "Bestellungen", status: "Status" },
132 }
133
134 expect(findMissing(en, locale, ["one", "other"])).toEqual({
135 missing: { orders: { status: { pending: "Pending", paid: "Paid" } } },
136 count: 2,
137 })
138 })
139 })
140
141 describe("mergeLevel", () => {
142 test("inserts new keys after their closest preceding en.json sibling", () => {
143 const locale = {
144 $schema: "./$schema.json",
145 general: { cancel: "Abbrechen", extra: "Veraltet" },
146 orders: { status: { paid: "Bezahlt" } },
147 }
148
149 const { result, errors } = merge(locale, {
150 general: { save: "Speichern", items_other: "{{count}} Artikel" },
151 orders: { title: "Bestellungen", status: { pending: "Ausstehend" } },
152 })
153
154 expect(errors).toEqual([])
155 expect(Object.keys(result)).toEqual(["$schema", "general", "orders"])
156 expect(Object.keys(result.general)).toEqual([
157 "save",
158 "cancel",
159 "items_other",
160 "extra",
161 ])
162 expect(result.orders).toEqual({
163 title: "Bestellungen",
164 status: { pending: "Ausstehend", paid: "Bezahlt" },
165 })
166 expect(Object.keys(result.orders)).toEqual(["title", "status"])
167 expect(Object.keys(result.orders.status)).toEqual(["pending", "paid"])
168 })
169
170 test("keeps $schema first when inserting at the start", () => {
171 const { result } = merge(
172 { $schema: "./$schema.json", orders: { title: "Bestellungen" } },
173 { general: { save: "Speichern" } }
174 )
175
176 expect(Object.keys(result)).toEqual(["$schema", "general", "orders"])
177 })
178
179 test("creates sections that don't exist in the locale", () => {
180 const { result, errors } = merge(
181 {},
182 { orders: { status: { pending: "Ausstehend" } } }
183 )
184
185 expect(errors).toEqual([])
186 expect(result).toEqual({ orders: { status: { pending: "Ausstehend" } } })
187 })
188
189 test("accepts plural forms English doesn't have", () => {
190 const { result, errors } = merge(
191 { general: { items_one: "{{count}} element" } },
192 {
193 general: {
194 items_few: "{{count}} elementy",
195 items_many: "{{count}} elementów",
196 },
197 },
198 ["one", "few", "many", "other"]
199 )
200
201 expect(errors).toEqual([])
202 expect(result.general).toEqual({
203 items_one: "{{count}} element",
204 items_few: "{{count}} elementy",
205 items_many: "{{count}} elementów",
206 })
207 })
208
209 test("allows plural forms to drop {{count}}", () => {
210 const { result, errors } = merge(
211 {},
212 { general: { items_one: "Ein Artikel" } }
213 )
214
215 expect(errors).toEqual([])
216 expect(result.general.items_one).toBe("Ein Artikel")
217 })
218
219 test("rejects keys that aren't in en.json or the locale's plural forms", () => {
220 const { result, errors } = merge(
221 {},
222 { general: { save: "Speichern", unknown: "x", items_few: "y" } }
223 )
224
225 expect(errors).toEqual([
226 'Unknown key "general.unknown"',
227 'Unknown key "general.items_few"',
228 ])
229 expect(result).toEqual({ general: { save: "Speichern" } })
230 })
231
232 test("never overwrites existing translations", () => {
233 const { result, errors } = merge(
234 { general: { save: "Speichern" } },
235 { general: { save: "Sichern" } }
236 )
237
238 expect(errors).toEqual(['"general.save" is already translated'])
239 expect(result.general.save).toBe("Speichern")
240 })
241
242 test("rejects placeholder and Trans tag mismatches", () => {
243 const errors: string[] = []
244 const result = mergeLevel(
245 { a: "Hi {{name}}", b: "Click <0>here</0>" },
246 {},
247 { a: "Hallo {{nom}}", b: "Klicke hier" },
248 ["one", "other"],
249 [],
250 errors
251 )
252
253 expect(errors).toEqual([
254 'Placeholder mismatch at "a": expected [{{name}}], got [{{nom}}]',
255 'Placeholder mismatch at "b": expected [</0>,<0>], got []',
256 ])
257 expect(result).toEqual({})
258 })
259
260 test("rejects empty strings and wrong value types", () => {
261 const { result, errors } = merge(
262 {},
263 {
264 general: { save: " ", cancel: { nested: "x" } },
265 orders: "Bestellungen",
266 }
267 )
268
269 expect(errors).toEqual([
270 'Expected a non-empty string at "general.save"',
271 'Expected a non-empty string at "general.cancel"',
272 'Expected an object at "orders"',
273 ])
274 expect(result).toEqual({})
275 })
276
277 test("merges output split across multiple parts", () => {
278 const first = merge({}, { general: { cancel: "Abbrechen" } })
279 const second = merge(first.result, { general: { save: "Speichern" } })
280
281 expect(second.errors).toEqual([])
282 expect(Object.keys(second.result.general)).toEqual(["save", "cancel"])
283 })
284 })