@lingo.dev/_spec 0.24.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,496 @@
1
+ // src/locales.ts
2
+ import Z from "zod";
3
+ var localeMap = {
4
+ // Urdu (Pakistan)
5
+ ur: ["ur-PK"],
6
+ // Vietnamese (Vietnam)
7
+ vi: ["vi-VN"],
8
+ // Turkish (Turkey)
9
+ tr: ["tr-TR"],
10
+ // Tamil (India)
11
+ ta: ["ta-IN"],
12
+ // Serbian
13
+ sr: [
14
+ "sr-RS",
15
+ // Serbian (Latin)
16
+ "sr-Latn-RS",
17
+ // Serbian (Latin)
18
+ "sr-Cyrl-RS"
19
+ // Serbian (Cyrillic)
20
+ ],
21
+ // Hungarian (Hungary)
22
+ hu: ["hu-HU"],
23
+ // Hebrew (Israel)
24
+ he: ["he-IL"],
25
+ // Estonian (Estonia)
26
+ et: ["et-EE"],
27
+ // Greek (Greece)
28
+ el: ["el-GR"],
29
+ // Danish (Denmark)
30
+ da: ["da-DK"],
31
+ // Azerbaijani (Azerbaijan)
32
+ az: ["az-AZ"],
33
+ // Thai (Thailand)
34
+ th: ["th-TH"],
35
+ // Swedish (Sweden)
36
+ sv: ["sv-SE"],
37
+ // English
38
+ en: [
39
+ "en-US",
40
+ // United States
41
+ "en-GB",
42
+ // United Kingdom
43
+ "en-AU",
44
+ // Australia
45
+ "en-CA"
46
+ // Canada
47
+ ],
48
+ // Spanish
49
+ es: [
50
+ "es-ES",
51
+ // Spain
52
+ "es-419",
53
+ // Latin America
54
+ "es-MX",
55
+ // Mexico
56
+ "es-AR"
57
+ // Argentina
58
+ ],
59
+ // French
60
+ fr: [
61
+ "fr-FR",
62
+ // France
63
+ "fr-CA",
64
+ // Canada
65
+ "fr-BE"
66
+ // Belgium
67
+ ],
68
+ // Catalan (Spain)
69
+ ca: ["ca-ES"],
70
+ // Japanese (Japan)
71
+ ja: ["ja-JP"],
72
+ // German
73
+ de: [
74
+ "de-DE",
75
+ // Germany
76
+ "de-AT",
77
+ // Austria
78
+ "de-CH"
79
+ // Switzerland
80
+ ],
81
+ // Portuguese
82
+ pt: [
83
+ "pt-PT",
84
+ // Portugal
85
+ "pt-BR"
86
+ // Brazil
87
+ ],
88
+ // Italian
89
+ it: [
90
+ "it-IT",
91
+ // Italy
92
+ "it-CH"
93
+ // Switzerland
94
+ ],
95
+ // Russian
96
+ ru: [
97
+ "ru-RU",
98
+ // Russia
99
+ "ru-BY"
100
+ // Belarus
101
+ ],
102
+ // Ukrainian (Ukraine)
103
+ uk: ["uk-UA"],
104
+ // Belarusian (Belarus)
105
+ be: ["be-BY"],
106
+ // Hindi (India)
107
+ hi: ["hi-IN"],
108
+ // Chinese
109
+ zh: [
110
+ "zh-CN",
111
+ // Simplified Chinese (China)
112
+ "zh-TW",
113
+ // Traditional Chinese (Taiwan)
114
+ "zh-HK",
115
+ // Traditional Chinese (Hong Kong)
116
+ "zh-Hans",
117
+ // Simplified Chinese
118
+ "zh-Hant",
119
+ // Traditional Chinese
120
+ "zh-Hant-HK",
121
+ // Traditional Chinese (Hong Kong)
122
+ "zh-Hant-TW",
123
+ // Traditional Chinese (Taiwan)
124
+ "zh-Hant-CN",
125
+ // Traditional Chinese (China)
126
+ "zh-Hans-HK",
127
+ // Simplified Chinese (Hong Kong)
128
+ "zh-Hans-TW",
129
+ // Simplified Chinese (China)
130
+ "zh-Hans-CN"
131
+ // Simplified Chinese (China)
132
+ ],
133
+ // Korean (South Korea)
134
+ ko: ["ko-KR"],
135
+ // Arabic
136
+ ar: [
137
+ "ar-EG",
138
+ // Egypt
139
+ "ar-SA",
140
+ // Saudi Arabia
141
+ "ar-AE",
142
+ // United Arab Emirates
143
+ "ar-MA"
144
+ // Morocco
145
+ ],
146
+ // Bulgarian (Bulgaria)
147
+ bg: ["bg-BG"],
148
+ // Czech (Czech Republic)
149
+ cs: ["cs-CZ"],
150
+ // Dutch
151
+ nl: [
152
+ "nl-NL",
153
+ // Netherlands
154
+ "nl-BE"
155
+ // Belgium
156
+ ],
157
+ // Polish (Poland)
158
+ pl: ["pl-PL"],
159
+ // Indonesian (Indonesia)
160
+ id: ["id-ID"],
161
+ // Malay (Malaysia)
162
+ ms: ["ms-MY"],
163
+ // Finnish (Finland)
164
+ fi: ["fi-FI"],
165
+ // Basque (Spain)
166
+ eu: ["eu-ES"],
167
+ // Croatian (Croatia)
168
+ hr: ["hr-HR"],
169
+ // Hebrew (Israel) - alternative code
170
+ iw: ["iw-IL"],
171
+ // Khmer (Cambodia)
172
+ km: ["km-KH"],
173
+ // Latvian (Latvia)
174
+ lv: ["lv-LV"],
175
+ // Lithuanian (Lithuania)
176
+ lt: ["lt-LT"],
177
+ // Norwegian (Norway)
178
+ no: ["no-NO"],
179
+ // Romanian (Romania)
180
+ ro: ["ro-RO"],
181
+ // Slovak (Slovakia)
182
+ sk: ["sk-SK"],
183
+ // Swahili
184
+ sw: [
185
+ "sw-TZ",
186
+ // Tanzania
187
+ "sw-KE"
188
+ // Kenya
189
+ ],
190
+ // Persian (Iran)
191
+ fa: ["fa-IR"],
192
+ // Filipino (Philippines)
193
+ fil: ["fil-PH"],
194
+ // Punjabi
195
+ pa: [
196
+ "pa-IN",
197
+ // India
198
+ "pa-PK"
199
+ // Pakistan
200
+ ],
201
+ // Bengali
202
+ bn: [
203
+ "bn-BD",
204
+ // Bangladesh
205
+ "bn-IN"
206
+ // India
207
+ ],
208
+ // Irish (Ireland)
209
+ ga: ["ga-IE"],
210
+ // Maltese (Malta)
211
+ mt: ["mt-MT"],
212
+ // Slovenian (Slovenia)
213
+ sl: ["sl-SI"],
214
+ // Albanian (Albania)
215
+ sq: ["sq-AL"],
216
+ // Bavarian (Germany)
217
+ bar: ["bar-DE"],
218
+ // Neapolitan (Italy)
219
+ nap: ["nap-IT"],
220
+ // Afrikaans (South Africa)
221
+ af: ["af-ZA"],
222
+ // Somali (Somalia)
223
+ so: ["so-SO"],
224
+ // Tigrinya (Ethiopia)
225
+ ti: ["ti-ET"],
226
+ // Standard Moroccan Tamazight (Morocco)
227
+ zgh: ["zgh-MA"],
228
+ // Tagalog (Philippines)
229
+ tl: ["tl-PH"],
230
+ // Telugu (India)
231
+ te: ["te-IN"]
232
+ };
233
+ var localeCodesShort = Object.keys(localeMap);
234
+ var localeCodesFull = Object.values(localeMap).flat();
235
+ var localeCodesFullUnderscore = localeCodesFull.map((value) => value.replace("-", "_"));
236
+ var localeCodesFullExplicitRegion = localeCodesFull.map((value) => {
237
+ const chunks = value.split("-");
238
+ const result = [chunks[0], "-r", chunks.slice(1).join("-")].join("");
239
+ return result;
240
+ });
241
+ var localeCodes = [
242
+ ...localeCodesShort,
243
+ ...localeCodesFull,
244
+ ...localeCodesFullUnderscore,
245
+ ...localeCodesFullExplicitRegion
246
+ ];
247
+ var localeCodeSchema = Z.string().refine((value) => localeCodes.includes(value), {
248
+ message: "Invalid locale code"
249
+ });
250
+ var resolveLocaleCode = (value) => {
251
+ const existingFullLocaleCode = Object.values(localeMap).flat().includes(value);
252
+ if (existingFullLocaleCode) {
253
+ return value;
254
+ }
255
+ const existingShortLocaleCode = Object.keys(localeMap).includes(value);
256
+ if (existingShortLocaleCode) {
257
+ const correspondingFullLocales = localeMap[value];
258
+ const fallbackFullLocale = correspondingFullLocales[0];
259
+ return fallbackFullLocale;
260
+ }
261
+ throw new Error(`Invalid locale code: ${value}`);
262
+ };
263
+ var getAlternativeLocaleCodes = (locale) => {
264
+ if (locale.includes("-")) {
265
+ return [locale.replace(/-/g, "_")];
266
+ } else if (locale.includes("_")) {
267
+ return [locale.replace(/_/g, "-")];
268
+ } else {
269
+ return [];
270
+ }
271
+ };
272
+ var getLocaleCodeDelimiter = (locale) => {
273
+ if (locale.includes("_")) {
274
+ return "_";
275
+ } else if (locale.includes("-")) {
276
+ return "-";
277
+ } else {
278
+ return null;
279
+ }
280
+ };
281
+ var resolveOverridenLocale = (locale, delimiter) => {
282
+ if (!delimiter) {
283
+ return locale;
284
+ }
285
+ const currentDelimiter = getLocaleCodeDelimiter(locale);
286
+ if (!currentDelimiter) {
287
+ return locale;
288
+ }
289
+ return locale.replace(currentDelimiter, delimiter);
290
+ };
291
+
292
+ // src/formats.ts
293
+ import Z2 from "zod";
294
+ var bucketTypes = [
295
+ "android",
296
+ "csv",
297
+ "flutter",
298
+ "html",
299
+ "json",
300
+ "markdown",
301
+ "xcode-strings",
302
+ "xcode-stringsdict",
303
+ "xcode-xcstrings",
304
+ "yaml",
305
+ "yaml-root-key",
306
+ "properties",
307
+ "po",
308
+ "xliff",
309
+ "xml",
310
+ "srt",
311
+ "dato",
312
+ "compiler",
313
+ "vtt"
314
+ ];
315
+ var bucketTypeSchema = Z2.enum(bucketTypes);
316
+
317
+ // src/config.ts
318
+ import Z3 from "zod";
319
+ var localeSchema = Z3.object({
320
+ source: localeCodeSchema,
321
+ targets: Z3.array(localeCodeSchema)
322
+ });
323
+ var createConfigDefinition = (definition) => definition;
324
+ var extendConfigDefinition = (definition, params) => {
325
+ const schema = params.createSchema(definition.schema);
326
+ const defaultValue = params.createDefaultValue(definition.defaultValue);
327
+ const upgrader = (config) => params.createUpgrader(config, schema, defaultValue);
328
+ return createConfigDefinition({
329
+ schema,
330
+ defaultValue,
331
+ parse: (rawConfig) => {
332
+ const safeResult = schema.safeParse(rawConfig);
333
+ if (safeResult.success) {
334
+ return safeResult.data;
335
+ }
336
+ const localeErrors = safeResult.error.errors.filter((issue) => issue.message.includes("Invalid locale code")).map((issue) => {
337
+ let unsupportedLocale = "";
338
+ const path = issue.path;
339
+ const config = rawConfig;
340
+ if (config.locale) {
341
+ unsupportedLocale = path.reduce((acc, key) => {
342
+ if (acc && typeof acc === "object" && key in acc) {
343
+ return acc[key];
344
+ }
345
+ return acc;
346
+ }, config.locale);
347
+ }
348
+ return `Unsupported locale: ${unsupportedLocale}`;
349
+ });
350
+ if (localeErrors.length > 0) {
351
+ throw new Error(`
352
+ ${localeErrors.join("\n")}`);
353
+ }
354
+ const baseConfig = definition.parse(rawConfig);
355
+ const result = upgrader(baseConfig);
356
+ return result;
357
+ }
358
+ });
359
+ };
360
+ var configV0Schema = Z3.object({
361
+ version: Z3.number().default(0)
362
+ });
363
+ var configV0Definition = createConfigDefinition({
364
+ schema: configV0Schema,
365
+ defaultValue: { version: 0 },
366
+ parse: (rawConfig) => {
367
+ return configV0Schema.parse(rawConfig);
368
+ }
369
+ });
370
+ var configV1Definition = extendConfigDefinition(configV0Definition, {
371
+ createSchema: (baseSchema) => baseSchema.extend({
372
+ locale: localeSchema,
373
+ buckets: Z3.record(Z3.string(), bucketTypeSchema).default({}).optional()
374
+ }),
375
+ createDefaultValue: () => ({
376
+ version: 1,
377
+ locale: {
378
+ source: "en",
379
+ targets: ["es"]
380
+ },
381
+ buckets: {}
382
+ }),
383
+ createUpgrader: () => ({
384
+ version: 1,
385
+ locale: {
386
+ source: "en",
387
+ targets: ["es"]
388
+ },
389
+ buckets: {}
390
+ })
391
+ });
392
+ var configV1_1Definition = extendConfigDefinition(configV1Definition, {
393
+ createSchema: (baseSchema) => baseSchema.extend({
394
+ buckets: Z3.record(
395
+ bucketTypeSchema,
396
+ Z3.object({
397
+ include: Z3.array(Z3.string()).default([]),
398
+ exclude: Z3.array(Z3.string()).default([]).optional()
399
+ })
400
+ ).default({})
401
+ }),
402
+ createDefaultValue: (baseDefaultValue) => ({
403
+ ...baseDefaultValue,
404
+ version: 1.1,
405
+ buckets: {}
406
+ }),
407
+ createUpgrader: (oldConfig, schema) => {
408
+ const upgradedConfig = {
409
+ ...oldConfig,
410
+ version: 1.1,
411
+ buckets: {}
412
+ };
413
+ if (oldConfig.buckets) {
414
+ for (const [bucketPath, bucketType] of Object.entries(oldConfig.buckets)) {
415
+ if (!upgradedConfig.buckets[bucketType]) {
416
+ upgradedConfig.buckets[bucketType] = {
417
+ include: []
418
+ };
419
+ }
420
+ upgradedConfig.buckets[bucketType]?.include.push(bucketPath);
421
+ }
422
+ }
423
+ return upgradedConfig;
424
+ }
425
+ });
426
+ var configV1_2Definition = extendConfigDefinition(configV1_1Definition, {
427
+ createSchema: (baseSchema) => baseSchema.extend({
428
+ locale: localeSchema.extend({
429
+ extraSource: localeCodeSchema.optional()
430
+ })
431
+ }),
432
+ createDefaultValue: (baseDefaultValue) => ({
433
+ ...baseDefaultValue,
434
+ version: 1.2
435
+ }),
436
+ createUpgrader: (oldConfig) => ({
437
+ ...oldConfig,
438
+ version: 1.2
439
+ })
440
+ });
441
+ var bucketItemSchema = Z3.object({
442
+ path: Z3.string(),
443
+ delimiter: Z3.union([Z3.literal("-"), Z3.literal("_"), Z3.literal(null)]).optional()
444
+ });
445
+ var configV1_3Definition = extendConfigDefinition(configV1_2Definition, {
446
+ createSchema: (baseSchema) => baseSchema.extend({
447
+ buckets: Z3.record(
448
+ bucketTypeSchema,
449
+ Z3.object({
450
+ include: Z3.array(Z3.union([Z3.string(), bucketItemSchema])).default([]),
451
+ exclude: Z3.array(Z3.union([Z3.string(), bucketItemSchema])).default([]).optional()
452
+ })
453
+ ).default({})
454
+ }),
455
+ createDefaultValue: (baseDefaultValue) => ({
456
+ ...baseDefaultValue,
457
+ version: 1.3
458
+ }),
459
+ createUpgrader: (oldConfig) => ({
460
+ ...oldConfig,
461
+ version: 1.3
462
+ })
463
+ });
464
+ var LATEST_CONFIG_DEFINITION = configV1_3Definition;
465
+ function parseI18nConfig(rawConfig) {
466
+ try {
467
+ const result = LATEST_CONFIG_DEFINITION.parse(rawConfig);
468
+ return result;
469
+ } catch (error) {
470
+ throw new Error(`Failed to parse config: ${error.message}`);
471
+ }
472
+ }
473
+ var defaultConfig = LATEST_CONFIG_DEFINITION.defaultValue;
474
+ export {
475
+ bucketItemSchema,
476
+ bucketTypeSchema,
477
+ bucketTypes,
478
+ configV0Definition,
479
+ configV1Definition,
480
+ configV1_1Definition,
481
+ configV1_2Definition,
482
+ configV1_3Definition,
483
+ defaultConfig,
484
+ getAlternativeLocaleCodes,
485
+ getLocaleCodeDelimiter,
486
+ localeCodeSchema,
487
+ localeCodes,
488
+ localeCodesFull,
489
+ localeCodesFullExplicitRegion,
490
+ localeCodesFullUnderscore,
491
+ localeCodesShort,
492
+ localeSchema,
493
+ parseI18nConfig,
494
+ resolveLocaleCode,
495
+ resolveOverridenLocale
496
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@lingo.dev/_spec",
3
+ "version": "0.24.2",
4
+ "description": "Lingo.dev open specification",
5
+ "private": false,
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "type": "module",
10
+ "sideEffects": false,
11
+ "types": "build/index.d.ts",
12
+ "module": "build/index.mjs",
13
+ "main": "build/index.cjs",
14
+ "files": [
15
+ "build"
16
+ ],
17
+ "scripts": {
18
+ "dev": "tsup --watch",
19
+ "build": "tsc --noEmit && tsup",
20
+ "test": "vitest run"
21
+ },
22
+ "keywords": [],
23
+ "author": "",
24
+ "license": "Apache-2.0",
25
+ "dependencies": {
26
+ "typescript": "^5.7.2",
27
+ "vitest": "^2.1.8",
28
+ "zod": "^3.24.1"
29
+ },
30
+ "devDependencies": {
31
+ "tsup": "^8.3.5"
32
+ }
33
+ }