@lingo.dev/_compiler 0.3.5 → 0.4.0
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.
- package/build/chunk-OO4SG7IH.cjs +2649 -0
- package/build/chunk-YT6JFNHK.mjs +2649 -0
- package/build/index.cjs +164 -2688
- package/build/index.d.cts +6 -1
- package/build/index.d.ts +6 -1
- package/build/index.mjs +174 -2698
- package/build/lingo-turbopack-loader.cjs +47 -0
- package/build/lingo-turbopack-loader.d.cts +3 -0
- package/build/lingo-turbopack-loader.d.ts +3 -0
- package/build/lingo-turbopack-loader.mjs +45 -0
- package/package.json +1 -1
package/build/index.mjs
CHANGED
|
@@ -1,2641 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
files: [
|
|
19
|
-
"build"
|
|
20
|
-
],
|
|
21
|
-
scripts: {
|
|
22
|
-
dev: "tsup --watch",
|
|
23
|
-
build: "tsc --noEmit && tsup",
|
|
24
|
-
clean: "rm -rf build",
|
|
25
|
-
test: "vitest --run",
|
|
26
|
-
"test:watch": "vitest -w"
|
|
27
|
-
},
|
|
28
|
-
keywords: [],
|
|
29
|
-
author: "",
|
|
30
|
-
license: "ISC",
|
|
31
|
-
devDependencies: {
|
|
32
|
-
"@types/babel__generator": "^7.6.8",
|
|
33
|
-
"@types/babel__traverse": "^7.20.6",
|
|
34
|
-
"@types/ini": "^4.1.1",
|
|
35
|
-
"@types/lodash": "^4.17.4",
|
|
36
|
-
"@types/object-hash": "^3.0.6",
|
|
37
|
-
"@types/react": "^18.3.18",
|
|
38
|
-
next: "15.2.4",
|
|
39
|
-
tsup: "^8.3.5",
|
|
40
|
-
typescript: "^5.4.5"
|
|
41
|
-
},
|
|
42
|
-
dependencies: {
|
|
43
|
-
"@ai-sdk/google": "^1.2.19",
|
|
44
|
-
"@ai-sdk/groq": "^1.2.3",
|
|
45
|
-
"@babel/generator": "^7.26.5",
|
|
46
|
-
"@babel/parser": "^7.26.7",
|
|
47
|
-
"@babel/traverse": "^7.27.4",
|
|
48
|
-
"@babel/types": "^7.26.7",
|
|
49
|
-
"@lingo.dev/_sdk": "workspace:*",
|
|
50
|
-
"@openrouter/ai-sdk-provider": "^0.7.1",
|
|
51
|
-
ai: "^4.2.10",
|
|
52
|
-
dedent: "^1.6.0",
|
|
53
|
-
dotenv: "^16.4.5",
|
|
54
|
-
"fast-xml-parser": "^5.0.8",
|
|
55
|
-
ini: "^5.0.0",
|
|
56
|
-
lodash: "^4.17.21",
|
|
57
|
-
"object-hash": "^3.0.0",
|
|
58
|
-
"ollama-ai-provider": "^1.2.0",
|
|
59
|
-
prettier: "^3.4.2",
|
|
60
|
-
unplugin: "^2.1.2",
|
|
61
|
-
vitest: "^2.1.4",
|
|
62
|
-
zod: "^3.24.1"
|
|
63
|
-
},
|
|
64
|
-
packageManager: "pnpm@9.12.3"
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// src/index.ts
|
|
68
|
-
import _11 from "lodash";
|
|
69
|
-
import dedent3 from "dedent";
|
|
70
|
-
|
|
71
|
-
// src/_base.ts
|
|
72
|
-
import generate from "@babel/generator";
|
|
73
|
-
import * as parser from "@babel/parser";
|
|
74
|
-
function createCodeMutation(spec) {
|
|
75
|
-
return (payload) => {
|
|
76
|
-
const result = spec(payload);
|
|
77
|
-
return result;
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
function createPayload(input) {
|
|
81
|
-
const ast = parser.parse(input.code, {
|
|
82
|
-
sourceType: "module",
|
|
83
|
-
plugins: ["jsx", "typescript"]
|
|
84
|
-
});
|
|
85
|
-
return {
|
|
86
|
-
...input,
|
|
87
|
-
ast
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
function createOutput(payload) {
|
|
91
|
-
const generationResult = generate(payload.ast, {}, payload.code);
|
|
92
|
-
return {
|
|
93
|
-
code: generationResult.code,
|
|
94
|
-
map: generationResult.map
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
function composeMutations(...mutations) {
|
|
98
|
-
return (input) => {
|
|
99
|
-
let result = input;
|
|
100
|
-
for (const mutate of mutations) {
|
|
101
|
-
const intermediateResult = mutate(result);
|
|
102
|
-
if (!intermediateResult) {
|
|
103
|
-
break;
|
|
104
|
-
} else {
|
|
105
|
-
result = intermediateResult;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
return result;
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
var defaultParams = {
|
|
112
|
-
sourceRoot: "src",
|
|
113
|
-
lingoDir: "lingo",
|
|
114
|
-
sourceLocale: "en",
|
|
115
|
-
targetLocales: ["es"],
|
|
116
|
-
rsc: false,
|
|
117
|
-
useDirective: false,
|
|
118
|
-
debug: false,
|
|
119
|
-
models: {}
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
// src/utils/index.ts
|
|
123
|
-
import traverse from "@babel/traverse";
|
|
124
|
-
import * as t2 from "@babel/types";
|
|
125
|
-
|
|
126
|
-
// src/utils/jsx-attribute.ts
|
|
127
|
-
import * as t from "@babel/types";
|
|
128
|
-
import _ from "lodash";
|
|
129
|
-
function getJsxAttributesMap(nodePath) {
|
|
130
|
-
const attributes = nodePath.node.openingElement.attributes;
|
|
131
|
-
return _.reduce(
|
|
132
|
-
attributes,
|
|
133
|
-
(result, attr) => {
|
|
134
|
-
if (attr.type !== "JSXAttribute" || attr.name.type !== "JSXIdentifier") {
|
|
135
|
-
return result;
|
|
136
|
-
}
|
|
137
|
-
const name = attr.name.name;
|
|
138
|
-
const value = extractAttributeValue(attr);
|
|
139
|
-
return { ...result, [name]: value };
|
|
140
|
-
},
|
|
141
|
-
{}
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
function getJsxAttributeValue(nodePath, attributeName) {
|
|
145
|
-
const attributes = nodePath.node.openingElement.attributes;
|
|
146
|
-
const attribute = _.find(
|
|
147
|
-
attributes,
|
|
148
|
-
(attr) => attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === attributeName
|
|
149
|
-
);
|
|
150
|
-
if (!attribute) {
|
|
151
|
-
return void 0;
|
|
152
|
-
}
|
|
153
|
-
return extractAttributeValue(attribute);
|
|
154
|
-
}
|
|
155
|
-
function extractAttributeValue(attribute) {
|
|
156
|
-
if (!attribute.value) {
|
|
157
|
-
return true;
|
|
158
|
-
}
|
|
159
|
-
if (attribute.value.type === "StringLiteral") {
|
|
160
|
-
return attribute.value.value;
|
|
161
|
-
}
|
|
162
|
-
if (attribute.value.type === "JSXExpressionContainer") {
|
|
163
|
-
const expression = attribute.value.expression;
|
|
164
|
-
if (expression.type === "BooleanLiteral") {
|
|
165
|
-
return expression.value;
|
|
166
|
-
}
|
|
167
|
-
if (expression.type === "NumericLiteral") {
|
|
168
|
-
return expression.value;
|
|
169
|
-
}
|
|
170
|
-
if (expression.type === "StringLiteral") {
|
|
171
|
-
return expression.value;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// src/utils/index.ts
|
|
178
|
-
function getJsxRoots(node) {
|
|
179
|
-
const result = [];
|
|
180
|
-
traverse(node, {
|
|
181
|
-
JSXElement(path7) {
|
|
182
|
-
result.push(path7);
|
|
183
|
-
path7.skip();
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
return result;
|
|
187
|
-
}
|
|
188
|
-
function getOrCreateImport(ast, params) {
|
|
189
|
-
let importedName = params.exportedName;
|
|
190
|
-
let existingImport = findExistingImport(
|
|
191
|
-
ast,
|
|
192
|
-
params.exportedName,
|
|
193
|
-
params.moduleName
|
|
194
|
-
);
|
|
195
|
-
if (existingImport) {
|
|
196
|
-
return { importedName: existingImport };
|
|
197
|
-
}
|
|
198
|
-
importedName = generateUniqueImportName(ast, params.exportedName);
|
|
199
|
-
createImportDeclaration(
|
|
200
|
-
ast,
|
|
201
|
-
importedName,
|
|
202
|
-
params.exportedName,
|
|
203
|
-
params.moduleName
|
|
204
|
-
);
|
|
205
|
-
return { importedName };
|
|
206
|
-
}
|
|
207
|
-
function findExistingImport(ast, exportedName, moduleName) {
|
|
208
|
-
let result = null;
|
|
209
|
-
traverse(ast, {
|
|
210
|
-
ImportDeclaration(path7) {
|
|
211
|
-
if (!moduleName.includes(path7.node.source.value)) {
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
for (const specifier of path7.node.specifiers) {
|
|
215
|
-
if (t2.isImportSpecifier(specifier) && (t2.isIdentifier(specifier.imported) && specifier.imported.name === exportedName || specifier.importKind === "value" && t2.isIdentifier(specifier.local) && specifier.local.name === exportedName)) {
|
|
216
|
-
result = specifier.local.name;
|
|
217
|
-
path7.stop();
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
return result;
|
|
224
|
-
}
|
|
225
|
-
function generateUniqueImportName(ast, baseName) {
|
|
226
|
-
const usedNames = /* @__PURE__ */ new Set();
|
|
227
|
-
traverse(ast, {
|
|
228
|
-
Identifier(path7) {
|
|
229
|
-
usedNames.add(path7.node.name);
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
if (!usedNames.has(baseName)) {
|
|
233
|
-
return baseName;
|
|
234
|
-
}
|
|
235
|
-
let counter = 1;
|
|
236
|
-
let candidateName = `${baseName}${counter}`;
|
|
237
|
-
while (usedNames.has(candidateName)) {
|
|
238
|
-
counter++;
|
|
239
|
-
candidateName = `${baseName}${counter}`;
|
|
240
|
-
}
|
|
241
|
-
return candidateName;
|
|
242
|
-
}
|
|
243
|
-
function createImportDeclaration(ast, localName, exportedName, moduleName) {
|
|
244
|
-
traverse(ast, {
|
|
245
|
-
Program(path7) {
|
|
246
|
-
const importSpecifier2 = t2.importSpecifier(
|
|
247
|
-
t2.identifier(localName),
|
|
248
|
-
t2.identifier(exportedName)
|
|
249
|
-
);
|
|
250
|
-
const existingImport = path7.get("body").find(
|
|
251
|
-
(nodePath) => t2.isImportDeclaration(nodePath.node) && moduleName.includes(nodePath.node.source.value)
|
|
252
|
-
);
|
|
253
|
-
if (existingImport && t2.isImportDeclaration(existingImport.node)) {
|
|
254
|
-
existingImport.node.specifiers.push(importSpecifier2);
|
|
255
|
-
} else {
|
|
256
|
-
const importDeclaration2 = t2.importDeclaration(
|
|
257
|
-
[importSpecifier2],
|
|
258
|
-
t2.stringLiteral(moduleName[0])
|
|
259
|
-
);
|
|
260
|
-
const lastImportIndex = findLastImportIndex(path7);
|
|
261
|
-
path7.node.body.splice(lastImportIndex + 1, 0, importDeclaration2);
|
|
262
|
-
}
|
|
263
|
-
path7.stop();
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
function findLastImportIndex(programPath) {
|
|
268
|
-
const body = programPath.node.body;
|
|
269
|
-
for (let i = body.length - 1; i >= 0; i--) {
|
|
270
|
-
if (t2.isImportDeclaration(body[i])) {
|
|
271
|
-
return i;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
return -1;
|
|
275
|
-
}
|
|
276
|
-
function _hasFileDirective(ast, directiveValue) {
|
|
277
|
-
let hasDirective = false;
|
|
278
|
-
traverse(ast, {
|
|
279
|
-
Directive(path7) {
|
|
280
|
-
if (path7.node.value.value === directiveValue) {
|
|
281
|
-
hasDirective = true;
|
|
282
|
-
path7.stop();
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
return hasDirective;
|
|
287
|
-
}
|
|
288
|
-
function hasI18nDirective(ast) {
|
|
289
|
-
return _hasFileDirective(ast, "use i18n");
|
|
290
|
-
}
|
|
291
|
-
function hasClientDirective(ast) {
|
|
292
|
-
return _hasFileDirective(ast, "use client");
|
|
293
|
-
}
|
|
294
|
-
function getModuleExecutionMode(ast, rscEnabled) {
|
|
295
|
-
if (rscEnabled) {
|
|
296
|
-
if (hasClientDirective(ast)) {
|
|
297
|
-
return "client";
|
|
298
|
-
} else {
|
|
299
|
-
return "server";
|
|
300
|
-
}
|
|
301
|
-
} else {
|
|
302
|
-
return "client";
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// src/i18n-directive.ts
|
|
307
|
-
var i18nDirectiveMutation = createCodeMutation((payload) => {
|
|
308
|
-
if (!payload.params.useDirective || hasI18nDirective(payload.ast)) {
|
|
309
|
-
return payload;
|
|
310
|
-
} else {
|
|
311
|
-
return null;
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
var i18n_directive_default = i18nDirectiveMutation;
|
|
315
|
-
|
|
316
|
-
// src/jsx-provider.ts
|
|
317
|
-
import traverse2 from "@babel/traverse";
|
|
318
|
-
import * as t4 from "@babel/types";
|
|
319
|
-
|
|
320
|
-
// src/utils/jsx-element.ts
|
|
321
|
-
import * as t3 from "@babel/types";
|
|
322
|
-
function getJsxElementName(nodePath) {
|
|
323
|
-
const openingElement = nodePath.node.openingElement;
|
|
324
|
-
if (t3.isJSXIdentifier(openingElement.name)) {
|
|
325
|
-
return openingElement.name.name;
|
|
326
|
-
}
|
|
327
|
-
return null;
|
|
328
|
-
}
|
|
329
|
-
function getNestedJsxElements(nodePath) {
|
|
330
|
-
const nestedElements = [];
|
|
331
|
-
nodePath.traverse({
|
|
332
|
-
JSXElement(path7) {
|
|
333
|
-
if (path7.node !== nodePath.node) {
|
|
334
|
-
nestedElements.push(path7.node);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
const arrayOfElements = nestedElements.map((element, index) => {
|
|
339
|
-
const param = t3.identifier("children");
|
|
340
|
-
const clonedElement = t3.cloneNode(element);
|
|
341
|
-
clonedElement.children = [t3.jsxExpressionContainer(param)];
|
|
342
|
-
return t3.arrowFunctionExpression(
|
|
343
|
-
[t3.objectPattern([t3.objectProperty(param, param, false, true)])],
|
|
344
|
-
clonedElement
|
|
345
|
-
);
|
|
346
|
-
});
|
|
347
|
-
const result = t3.arrayExpression(arrayOfElements);
|
|
348
|
-
return result;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// src/_const.ts
|
|
352
|
-
var ModuleId = {
|
|
353
|
-
ReactClient: ["lingo.dev/react/client", "lingo.dev/react-client"],
|
|
354
|
-
ReactRSC: ["lingo.dev/react/rsc", "lingo.dev/react-rsc"],
|
|
355
|
-
ReactRouter: ["lingo.dev/react/react-router", "lingo.dev/react-router"]
|
|
356
|
-
};
|
|
357
|
-
var LCP_DICTIONARY_FILE_NAME = "dictionary.js";
|
|
358
|
-
|
|
359
|
-
// src/jsx-provider.ts
|
|
360
|
-
var jsxProviderMutation = createCodeMutation((payload) => {
|
|
361
|
-
traverse2(payload.ast, {
|
|
362
|
-
JSXElement: (path7) => {
|
|
363
|
-
if (getJsxElementName(path7)?.toLowerCase() === "html") {
|
|
364
|
-
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
|
|
365
|
-
if (mode === "client") {
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
const lingoProviderImport = getOrCreateImport(payload.ast, {
|
|
369
|
-
moduleName: ModuleId.ReactRSC,
|
|
370
|
-
exportedName: "LingoProvider"
|
|
371
|
-
});
|
|
372
|
-
const loadDictionaryImport = getOrCreateImport(payload.ast, {
|
|
373
|
-
moduleName: ModuleId.ReactRSC,
|
|
374
|
-
exportedName: "loadDictionary"
|
|
375
|
-
});
|
|
376
|
-
const loadDictionaryArrow = t4.arrowFunctionExpression(
|
|
377
|
-
[t4.identifier("locale")],
|
|
378
|
-
t4.callExpression(t4.identifier(loadDictionaryImport.importedName), [
|
|
379
|
-
t4.identifier("locale")
|
|
380
|
-
])
|
|
381
|
-
);
|
|
382
|
-
const providerProps = [
|
|
383
|
-
t4.jsxAttribute(
|
|
384
|
-
t4.jsxIdentifier("loadDictionary"),
|
|
385
|
-
t4.jsxExpressionContainer(loadDictionaryArrow)
|
|
386
|
-
)
|
|
387
|
-
];
|
|
388
|
-
const provider = t4.jsxElement(
|
|
389
|
-
t4.jsxOpeningElement(
|
|
390
|
-
t4.jsxIdentifier(lingoProviderImport.importedName),
|
|
391
|
-
providerProps,
|
|
392
|
-
false
|
|
393
|
-
),
|
|
394
|
-
t4.jsxClosingElement(
|
|
395
|
-
t4.jsxIdentifier(lingoProviderImport.importedName)
|
|
396
|
-
),
|
|
397
|
-
[path7.node],
|
|
398
|
-
false
|
|
399
|
-
);
|
|
400
|
-
path7.replaceWith(provider);
|
|
401
|
-
path7.skip();
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
});
|
|
405
|
-
return payload;
|
|
406
|
-
});
|
|
407
|
-
var jsx_provider_default = jsxProviderMutation;
|
|
408
|
-
|
|
409
|
-
// src/jsx-root-flag.ts
|
|
410
|
-
import * as t5 from "@babel/types";
|
|
411
|
-
var jsxRootFlagMutation = createCodeMutation((payload) => {
|
|
412
|
-
const jsxRoots = getJsxRoots(payload.ast);
|
|
413
|
-
for (const jsxElementPath of jsxRoots) {
|
|
414
|
-
jsxElementPath.node.openingElement.attributes.push(
|
|
415
|
-
t5.jsxAttribute(t5.jsxIdentifier("data-jsx-root"), null)
|
|
416
|
-
);
|
|
417
|
-
}
|
|
418
|
-
return {
|
|
419
|
-
...payload
|
|
420
|
-
};
|
|
421
|
-
});
|
|
422
|
-
var jsx_root_flag_default = jsxRootFlagMutation;
|
|
423
|
-
|
|
424
|
-
// src/jsx-scope-flag.ts
|
|
425
|
-
import * as t8 from "@babel/types";
|
|
426
|
-
|
|
427
|
-
// src/utils/ast-key.ts
|
|
428
|
-
import * as t6 from "@babel/types";
|
|
429
|
-
import traverse3 from "@babel/traverse";
|
|
430
|
-
function getAstKey(nodePath) {
|
|
431
|
-
const keyChunks = [];
|
|
432
|
-
let current = nodePath;
|
|
433
|
-
while (current) {
|
|
434
|
-
keyChunks.push(current.key);
|
|
435
|
-
current = current.parentPath;
|
|
436
|
-
if (t6.isProgram(current?.node)) {
|
|
437
|
-
break;
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
const result = keyChunks.reverse().join("/");
|
|
441
|
-
return result;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
// src/utils/jsx-scope.ts
|
|
445
|
-
import * as t7 from "@babel/types";
|
|
446
|
-
import traverse4 from "@babel/traverse";
|
|
447
|
-
function collectJsxScopes(ast) {
|
|
448
|
-
const jsxScopes = [];
|
|
449
|
-
traverse4(ast, {
|
|
450
|
-
JSXElement: (path7) => {
|
|
451
|
-
if (!hasJsxScopeAttribute(path7)) return;
|
|
452
|
-
path7.skip();
|
|
453
|
-
jsxScopes.push(path7);
|
|
454
|
-
}
|
|
455
|
-
});
|
|
456
|
-
return jsxScopes;
|
|
457
|
-
}
|
|
458
|
-
function getJsxScopes(node) {
|
|
459
|
-
const result = [];
|
|
460
|
-
traverse4(node, {
|
|
461
|
-
JSXElement(path7) {
|
|
462
|
-
if (getJsxElementName(path7) === "LingoProvider") {
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
const hasNonEmptyTextSiblings = path7.getAllPrevSiblings().concat(path7.getAllNextSiblings()).some(
|
|
466
|
-
(sibling) => t7.isJSXText(sibling.node) && sibling.node.value?.trim() !== ""
|
|
467
|
-
);
|
|
468
|
-
if (hasNonEmptyTextSiblings) {
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
const hasNonEmptyTextChild = path7.get("children").some(
|
|
472
|
-
(child) => t7.isJSXText(child.node) && child.node.value?.trim() !== ""
|
|
473
|
-
);
|
|
474
|
-
if (hasNonEmptyTextChild) {
|
|
475
|
-
result.push(path7);
|
|
476
|
-
path7.skip();
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
});
|
|
480
|
-
return result;
|
|
481
|
-
}
|
|
482
|
-
function hasJsxScopeAttribute(path7) {
|
|
483
|
-
return !!getJsxScopeAttribute(path7);
|
|
484
|
-
}
|
|
485
|
-
function getJsxScopeAttribute(path7) {
|
|
486
|
-
const attribute = path7.node.openingElement.attributes.find(
|
|
487
|
-
(attr) => attr.type === "JSXAttribute" && attr.name.name === "data-jsx-scope"
|
|
488
|
-
);
|
|
489
|
-
return attribute && t7.isJSXAttribute(attribute) && t7.isStringLiteral(attribute.value) ? attribute.value.value : void 0;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
// src/jsx-scope-flag.ts
|
|
493
|
-
var jsxScopeFlagMutation = createCodeMutation((payload) => {
|
|
494
|
-
const jsxScopes = getJsxScopes(payload.ast);
|
|
495
|
-
for (const jsxScope of jsxScopes) {
|
|
496
|
-
jsxScope.node.openingElement.attributes.push(
|
|
497
|
-
t8.jsxAttribute(
|
|
498
|
-
t8.jsxIdentifier("data-jsx-scope"),
|
|
499
|
-
t8.stringLiteral(getAstKey(jsxScope))
|
|
500
|
-
)
|
|
501
|
-
);
|
|
502
|
-
}
|
|
503
|
-
return {
|
|
504
|
-
...payload
|
|
505
|
-
};
|
|
506
|
-
});
|
|
507
|
-
var jsx_scope_flag_default = jsxScopeFlagMutation;
|
|
508
|
-
|
|
509
|
-
// src/jsx-attribute-flag.ts
|
|
510
|
-
import * as t10 from "@babel/types";
|
|
511
|
-
|
|
512
|
-
// src/utils/jsx-attribute-scope.ts
|
|
513
|
-
import * as t9 from "@babel/types";
|
|
514
|
-
import traverse5 from "@babel/traverse";
|
|
515
|
-
function collectJsxAttributeScopes(node) {
|
|
516
|
-
const result = [];
|
|
517
|
-
traverse5(node, {
|
|
518
|
-
JSXElement(path7) {
|
|
519
|
-
if (!hasJsxAttributeScopeAttribute(path7)) return;
|
|
520
|
-
const localizableAttributes = getJsxAttributeScopeAttribute(path7);
|
|
521
|
-
if (!localizableAttributes) return;
|
|
522
|
-
result.push([path7, localizableAttributes]);
|
|
523
|
-
}
|
|
524
|
-
});
|
|
525
|
-
return result;
|
|
526
|
-
}
|
|
527
|
-
function getJsxAttributeScopes(node) {
|
|
528
|
-
const result = [];
|
|
529
|
-
const LOCALIZABLE_ATTRIBUTES = [
|
|
530
|
-
"title",
|
|
531
|
-
"aria-label",
|
|
532
|
-
"aria-description",
|
|
533
|
-
"alt",
|
|
534
|
-
"label",
|
|
535
|
-
"description",
|
|
536
|
-
"placeholder",
|
|
537
|
-
"content",
|
|
538
|
-
"subtitle"
|
|
539
|
-
];
|
|
540
|
-
traverse5(node, {
|
|
541
|
-
JSXElement(path7) {
|
|
542
|
-
const openingElement = path7.node.openingElement;
|
|
543
|
-
const elementName = openingElement.name;
|
|
544
|
-
if (!t9.isJSXIdentifier(elementName) || !elementName.name) {
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
const hasAttributeScope = openingElement.attributes.find(
|
|
548
|
-
(attr) => t9.isJSXAttribute(attr) && attr.name.name === "data-jsx-attribute-scope"
|
|
549
|
-
);
|
|
550
|
-
if (hasAttributeScope) {
|
|
551
|
-
return;
|
|
552
|
-
}
|
|
553
|
-
const localizableAttrs = openingElement.attributes.filter(
|
|
554
|
-
(attr) => {
|
|
555
|
-
if (!t9.isJSXAttribute(attr) || !t9.isStringLiteral(attr.value)) {
|
|
556
|
-
return false;
|
|
557
|
-
}
|
|
558
|
-
const name = attr.name.name;
|
|
559
|
-
return typeof name === "string" && LOCALIZABLE_ATTRIBUTES.includes(name);
|
|
560
|
-
}
|
|
561
|
-
).map((attr) => attr.name.name);
|
|
562
|
-
if (localizableAttrs.length > 0) {
|
|
563
|
-
result.push([path7, localizableAttrs]);
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
});
|
|
567
|
-
return result;
|
|
568
|
-
}
|
|
569
|
-
function hasJsxAttributeScopeAttribute(path7) {
|
|
570
|
-
return !!getJsxAttributeScopeAttribute(path7);
|
|
571
|
-
}
|
|
572
|
-
function getJsxAttributeScopeAttribute(path7) {
|
|
573
|
-
const attribute = path7.node.openingElement.attributes.find(
|
|
574
|
-
(attr) => attr.type === "JSXAttribute" && attr.name.name === "data-jsx-attribute-scope"
|
|
575
|
-
);
|
|
576
|
-
if (!attribute || !t9.isJSXAttribute(attribute)) {
|
|
577
|
-
return void 0;
|
|
578
|
-
}
|
|
579
|
-
if (t9.isJSXExpressionContainer(attribute.value) && t9.isArrayExpression(attribute.value.expression)) {
|
|
580
|
-
const arrayExpr = attribute.value.expression;
|
|
581
|
-
return arrayExpr.elements.filter((el) => t9.isStringLiteral(el)).map((el) => el.value);
|
|
582
|
-
}
|
|
583
|
-
if (t9.isStringLiteral(attribute.value)) {
|
|
584
|
-
return [attribute.value.value];
|
|
585
|
-
}
|
|
586
|
-
return void 0;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
// src/jsx-attribute-flag.ts
|
|
590
|
-
var jsxAttributeFlagMutation = createCodeMutation(
|
|
591
|
-
(payload) => {
|
|
592
|
-
const jsxScopes = getJsxAttributeScopes(payload.ast);
|
|
593
|
-
for (const [jsxScope, attributes] of jsxScopes) {
|
|
594
|
-
const scopeKey = getAstKey(jsxScope);
|
|
595
|
-
jsxScope.node.openingElement.attributes.push(
|
|
596
|
-
t10.jsxAttribute(
|
|
597
|
-
t10.jsxIdentifier("data-jsx-attribute-scope"),
|
|
598
|
-
t10.jsxExpressionContainer(
|
|
599
|
-
t10.arrayExpression(
|
|
600
|
-
attributes.map(
|
|
601
|
-
(attr) => t10.stringLiteral(`${attr}:${scopeKey}-${attr}`)
|
|
602
|
-
)
|
|
603
|
-
)
|
|
604
|
-
)
|
|
605
|
-
)
|
|
606
|
-
);
|
|
607
|
-
}
|
|
608
|
-
return {
|
|
609
|
-
...payload
|
|
610
|
-
};
|
|
611
|
-
}
|
|
612
|
-
);
|
|
613
|
-
var jsx_attribute_flag_default = jsxAttributeFlagMutation;
|
|
614
|
-
|
|
615
|
-
// src/index.ts
|
|
616
|
-
import path6 from "path";
|
|
617
|
-
|
|
618
|
-
// src/utils/module-params.ts
|
|
619
|
-
function parseParametrizedModuleId(rawId) {
|
|
620
|
-
const moduleUri = new URL(rawId, "module://");
|
|
621
|
-
return {
|
|
622
|
-
id: moduleUri.pathname.replace(/^\//, ""),
|
|
623
|
-
params: Object.fromEntries(moduleUri.searchParams.entries())
|
|
624
|
-
};
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
// src/lib/lcp/index.ts
|
|
628
|
-
import * as fs from "fs";
|
|
629
|
-
import _2 from "lodash";
|
|
630
|
-
import * as path from "path";
|
|
631
|
-
import dedent from "dedent";
|
|
632
|
-
var LCP_FILE_NAME = "meta.json";
|
|
633
|
-
var LCP = class _LCP {
|
|
634
|
-
constructor(filePath, data = {
|
|
635
|
-
version: 0.1
|
|
636
|
-
}) {
|
|
637
|
-
this.filePath = filePath;
|
|
638
|
-
this.data = data;
|
|
639
|
-
}
|
|
640
|
-
static ensureFile(params) {
|
|
641
|
-
const filePath = path.resolve(
|
|
642
|
-
process.cwd(),
|
|
643
|
-
params.sourceRoot,
|
|
644
|
-
params.lingoDir,
|
|
645
|
-
LCP_FILE_NAME
|
|
646
|
-
);
|
|
647
|
-
if (!fs.existsSync(filePath)) {
|
|
648
|
-
const dir = path.dirname(filePath);
|
|
649
|
-
if (!fs.existsSync(dir)) {
|
|
650
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
651
|
-
}
|
|
652
|
-
fs.writeFileSync(filePath, "{}");
|
|
653
|
-
console.log(dedent`
|
|
654
|
-
\n
|
|
655
|
-
⚠️ Lingo.dev Compiler detected missing meta.json file in lingo directory.
|
|
656
|
-
Please restart the build / watch command to regenerate all Lingo.dev Compiler files.
|
|
657
|
-
`);
|
|
658
|
-
try {
|
|
659
|
-
fs.rmdirSync(path.resolve(process.cwd(), ".next"), {
|
|
660
|
-
recursive: true
|
|
661
|
-
});
|
|
662
|
-
} catch (error) {
|
|
663
|
-
}
|
|
664
|
-
process.exit(1);
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
static getInstance(params) {
|
|
668
|
-
const filePath = path.resolve(
|
|
669
|
-
process.cwd(),
|
|
670
|
-
params.sourceRoot,
|
|
671
|
-
params.lingoDir,
|
|
672
|
-
LCP_FILE_NAME
|
|
673
|
-
);
|
|
674
|
-
if (fs.existsSync(filePath)) {
|
|
675
|
-
return new _LCP(filePath, JSON.parse(fs.readFileSync(filePath, "utf8")));
|
|
676
|
-
}
|
|
677
|
-
return new _LCP(filePath);
|
|
678
|
-
}
|
|
679
|
-
// wait until LCP file stops updating
|
|
680
|
-
// this ensures all files were transformed before loading / translating dictionaries
|
|
681
|
-
static async ready(params) {
|
|
682
|
-
if (params.isDev) {
|
|
683
|
-
_LCP.ensureFile(params);
|
|
684
|
-
}
|
|
685
|
-
const filePath = path.resolve(
|
|
686
|
-
process.cwd(),
|
|
687
|
-
params.sourceRoot,
|
|
688
|
-
params.lingoDir,
|
|
689
|
-
LCP_FILE_NAME
|
|
690
|
-
);
|
|
691
|
-
if (fs.existsSync(filePath)) {
|
|
692
|
-
const stats = fs.statSync(filePath);
|
|
693
|
-
if (Date.now() - stats.mtimeMs > 1500) {
|
|
694
|
-
return;
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
return new Promise((resolve3) => {
|
|
698
|
-
setTimeout(() => {
|
|
699
|
-
_LCP.ready(params).then(resolve3);
|
|
700
|
-
}, 750);
|
|
701
|
-
});
|
|
702
|
-
}
|
|
703
|
-
resetScope(fileKey, scopeKey) {
|
|
704
|
-
if (!_2.isObject(
|
|
705
|
-
_2.get(this.data, ["files", fileKey])
|
|
706
|
-
)) {
|
|
707
|
-
_2.set(this.data, ["files", fileKey], {});
|
|
708
|
-
}
|
|
709
|
-
_2.set(
|
|
710
|
-
this.data,
|
|
711
|
-
[
|
|
712
|
-
"files",
|
|
713
|
-
fileKey,
|
|
714
|
-
"scopes",
|
|
715
|
-
scopeKey
|
|
716
|
-
],
|
|
717
|
-
{}
|
|
718
|
-
);
|
|
719
|
-
return this;
|
|
720
|
-
}
|
|
721
|
-
setScopeType(fileKey, scopeKey, type) {
|
|
722
|
-
return this._setScopeField(fileKey, scopeKey, "type", type);
|
|
723
|
-
}
|
|
724
|
-
setScopeContext(fileKey, scopeKey, context) {
|
|
725
|
-
return this._setScopeField(fileKey, scopeKey, "context", context);
|
|
726
|
-
}
|
|
727
|
-
setScopeHash(fileKey, scopeKey, hash) {
|
|
728
|
-
return this._setScopeField(fileKey, scopeKey, "hash", hash);
|
|
729
|
-
}
|
|
730
|
-
setScopeSkip(fileKey, scopeKey, skip) {
|
|
731
|
-
return this._setScopeField(fileKey, scopeKey, "skip", skip);
|
|
732
|
-
}
|
|
733
|
-
setScopeOverrides(fileKey, scopeKey, overrides) {
|
|
734
|
-
return this._setScopeField(fileKey, scopeKey, "overrides", overrides);
|
|
735
|
-
}
|
|
736
|
-
setScopeContent(fileKey, scopeKey, content) {
|
|
737
|
-
return this._setScopeField(fileKey, scopeKey, "content", content);
|
|
738
|
-
}
|
|
739
|
-
toJSON() {
|
|
740
|
-
const files = _2(this.data?.files).mapValues((file, fileName) => {
|
|
741
|
-
return {
|
|
742
|
-
...file,
|
|
743
|
-
scopes: _2(file?.scopes).toPairs().sortBy([0]).fromPairs().value()
|
|
744
|
-
};
|
|
745
|
-
}).toPairs().sortBy([0]).fromPairs().value();
|
|
746
|
-
return { ...this.data, files };
|
|
747
|
-
}
|
|
748
|
-
toString() {
|
|
749
|
-
return JSON.stringify(this.toJSON(), null, 2);
|
|
750
|
-
}
|
|
751
|
-
save() {
|
|
752
|
-
const hasChanges = !fs.existsSync(this.filePath) || fs.readFileSync(this.filePath, "utf8") !== this.toString();
|
|
753
|
-
if (hasChanges) {
|
|
754
|
-
const dir = path.dirname(this.filePath);
|
|
755
|
-
if (!fs.existsSync(dir)) {
|
|
756
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
757
|
-
}
|
|
758
|
-
fs.writeFileSync(this.filePath, this.toString());
|
|
759
|
-
this._triggerLCPReload();
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
_triggerLCPReload() {
|
|
763
|
-
const dir = path.dirname(this.filePath);
|
|
764
|
-
const filePath = path.resolve(dir, LCP_DICTIONARY_FILE_NAME);
|
|
765
|
-
if (fs.existsSync(filePath)) {
|
|
766
|
-
try {
|
|
767
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
768
|
-
fs.utimesSync(filePath, now, now);
|
|
769
|
-
} catch (error) {
|
|
770
|
-
if (error?.code === "EINVAL") {
|
|
771
|
-
console.warn(
|
|
772
|
-
dedent`
|
|
773
|
-
⚠️ Lingo: Auto-reload disabled - system blocks Node.js timestamp updates.
|
|
774
|
-
💡 Fix: Adjust security settings to allow Node.js file modifications.
|
|
775
|
-
⚡ Workaround: Manually refresh browser after translation changes.
|
|
776
|
-
💬 Need help? Join our Discord: https://lingo.dev/go/discord.
|
|
777
|
-
`
|
|
778
|
-
);
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
_setScopeField(fileKey, scopeKey, field, value) {
|
|
784
|
-
_2.set(
|
|
785
|
-
this.data,
|
|
786
|
-
[
|
|
787
|
-
"files",
|
|
788
|
-
fileKey,
|
|
789
|
-
"scopes",
|
|
790
|
-
scopeKey,
|
|
791
|
-
field
|
|
792
|
-
],
|
|
793
|
-
value
|
|
794
|
-
);
|
|
795
|
-
return this;
|
|
796
|
-
}
|
|
797
|
-
};
|
|
798
|
-
|
|
799
|
-
// src/lib/lcp/server.ts
|
|
800
|
-
import _7 from "lodash";
|
|
801
|
-
|
|
802
|
-
// src/lib/lcp/cache.ts
|
|
803
|
-
import * as fs2 from "fs";
|
|
804
|
-
import * as path2 from "path";
|
|
805
|
-
import * as prettier from "prettier";
|
|
806
|
-
import _3 from "lodash";
|
|
807
|
-
var LCPCache = class {
|
|
808
|
-
// make sure the cache file exists, otherwise imports will fail
|
|
809
|
-
static ensureDictionaryFile(params) {
|
|
810
|
-
const cachePath = this._getCachePath(params);
|
|
811
|
-
if (!fs2.existsSync(cachePath)) {
|
|
812
|
-
const dir = path2.dirname(cachePath);
|
|
813
|
-
if (!fs2.existsSync(dir)) {
|
|
814
|
-
fs2.mkdirSync(dir, { recursive: true });
|
|
815
|
-
}
|
|
816
|
-
fs2.writeFileSync(cachePath, "export default {};");
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
// read cache entries for given locale, validate entry hash from LCP schema
|
|
820
|
-
static readLocaleDictionary(locale, params) {
|
|
821
|
-
const cache = this._read(params);
|
|
822
|
-
const dictionary = this._extractLocaleDictionary(cache, locale, params.lcp);
|
|
823
|
-
return dictionary;
|
|
824
|
-
}
|
|
825
|
-
// write cache entries for given locale to existing cache file, use hash from LCP schema
|
|
826
|
-
static async writeLocaleDictionary(dictionary, params) {
|
|
827
|
-
const currentCache = this._read(params);
|
|
828
|
-
const newCache = this._mergeLocaleDictionary(
|
|
829
|
-
currentCache,
|
|
830
|
-
dictionary,
|
|
831
|
-
params.lcp
|
|
832
|
-
);
|
|
833
|
-
await this._write(newCache, params);
|
|
834
|
-
}
|
|
835
|
-
// merge dictionary with current cache, sort files, entries and locales to minimize diffs
|
|
836
|
-
static _mergeLocaleDictionary(currentCache, dictionary, lcp) {
|
|
837
|
-
const files = _3(dictionary.files).mapValues((file, fileName) => ({
|
|
838
|
-
...file,
|
|
839
|
-
entries: _3(file.entries).mapValues((entry, entryName) => {
|
|
840
|
-
const cachedEntry = _3.get(currentCache, ["files", fileName, "entries", entryName]) ?? {};
|
|
841
|
-
const hash = _3.get(lcp, [
|
|
842
|
-
"files",
|
|
843
|
-
fileName,
|
|
844
|
-
"scopes",
|
|
845
|
-
entryName,
|
|
846
|
-
"hash"
|
|
847
|
-
]);
|
|
848
|
-
const cachedEntryContent = cachedEntry.hash === hash ? cachedEntry.content : {};
|
|
849
|
-
const content = _3({
|
|
850
|
-
...cachedEntryContent,
|
|
851
|
-
[dictionary.locale]: entry
|
|
852
|
-
}).toPairs().sortBy([0]).fromPairs().value();
|
|
853
|
-
return { content, hash };
|
|
854
|
-
}).toPairs().sortBy([0]).fromPairs().value()
|
|
855
|
-
})).toPairs().sortBy([0]).fromPairs().value();
|
|
856
|
-
const newCache = {
|
|
857
|
-
version: dictionary.version,
|
|
858
|
-
files
|
|
859
|
-
};
|
|
860
|
-
return newCache;
|
|
861
|
-
}
|
|
862
|
-
// extract dictionary from cache for given locale, validate entry hash from LCP schema
|
|
863
|
-
static _extractLocaleDictionary(cache, locale, lcp) {
|
|
864
|
-
const findCachedEntry = (hash) => {
|
|
865
|
-
const cachedEntry = _3(cache.files).flatMap((file) => _3.values(file.entries)).find((entry) => entry.hash === hash);
|
|
866
|
-
if (cachedEntry) {
|
|
867
|
-
return cachedEntry.content[locale];
|
|
868
|
-
}
|
|
869
|
-
return void 0;
|
|
870
|
-
};
|
|
871
|
-
const files = _3(lcp.files).mapValues((file) => {
|
|
872
|
-
return {
|
|
873
|
-
entries: _3(file.scopes).mapValues((entry) => {
|
|
874
|
-
return findCachedEntry(entry.hash);
|
|
875
|
-
}).pickBy((value) => value !== void 0).value()
|
|
876
|
-
};
|
|
877
|
-
}).pickBy((file) => !_3.isEmpty(file.entries)).value();
|
|
878
|
-
const dictionary = {
|
|
879
|
-
version: cache.version,
|
|
880
|
-
locale,
|
|
881
|
-
files
|
|
882
|
-
};
|
|
883
|
-
return dictionary;
|
|
884
|
-
}
|
|
885
|
-
// format with prettier
|
|
886
|
-
static async _format(cachedContent, cachePath) {
|
|
887
|
-
try {
|
|
888
|
-
const config2 = await prettier.resolveConfig(cachePath);
|
|
889
|
-
const prettierOptions = {
|
|
890
|
-
...config2 ?? {},
|
|
891
|
-
parser: config2?.parser ? config2.parser : "typescript"
|
|
892
|
-
};
|
|
893
|
-
return await prettier.format(cachedContent, prettierOptions);
|
|
894
|
-
} catch (error) {
|
|
895
|
-
}
|
|
896
|
-
return cachedContent;
|
|
897
|
-
}
|
|
898
|
-
// write cache to file as JSON
|
|
899
|
-
static async _write(dictionaryCache, params) {
|
|
900
|
-
const cachePath = this._getCachePath(params);
|
|
901
|
-
const cache = `export default ${JSON.stringify(dictionaryCache, null, 2)};`;
|
|
902
|
-
const formattedCache = await this._format(cache, cachePath);
|
|
903
|
-
fs2.writeFileSync(cachePath, formattedCache);
|
|
904
|
-
}
|
|
905
|
-
// read cache from file as JSON
|
|
906
|
-
static _read(params) {
|
|
907
|
-
const cachePath = this._getCachePath(params);
|
|
908
|
-
if (!fs2.existsSync(cachePath)) {
|
|
909
|
-
return {
|
|
910
|
-
version: 0.1,
|
|
911
|
-
files: {}
|
|
912
|
-
};
|
|
913
|
-
}
|
|
914
|
-
const jsObjectString = fs2.readFileSync(cachePath, "utf8");
|
|
915
|
-
const cache = jsObjectString.replace(/^export default/, "").replace(/;\s*$/, "");
|
|
916
|
-
const obj = new Function(`return (${cache})`)();
|
|
917
|
-
return obj;
|
|
918
|
-
}
|
|
919
|
-
// get cache file path
|
|
920
|
-
static _getCachePath(params) {
|
|
921
|
-
return path2.resolve(
|
|
922
|
-
process.cwd(),
|
|
923
|
-
params.sourceRoot,
|
|
924
|
-
params.lingoDir,
|
|
925
|
-
LCP_DICTIONARY_FILE_NAME
|
|
926
|
-
);
|
|
927
|
-
}
|
|
928
|
-
};
|
|
929
|
-
|
|
930
|
-
// src/lib/lcp/api/index.ts
|
|
931
|
-
import { createGroq } from "@ai-sdk/groq";
|
|
932
|
-
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
933
|
-
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
|
|
934
|
-
import { createOllama } from "ollama-ai-provider";
|
|
935
|
-
import { generateText } from "ai";
|
|
936
|
-
import { LingoDotDevEngine } from "@lingo.dev/_sdk";
|
|
937
|
-
import _6 from "lodash";
|
|
938
|
-
|
|
939
|
-
// src/utils/locales.ts
|
|
940
|
-
function getInvalidLocales(localeModels, sourceLocale, targetLocales) {
|
|
941
|
-
return targetLocales.filter((targetLocale) => {
|
|
942
|
-
const { provider, model } = getLocaleModel(
|
|
943
|
-
localeModels,
|
|
944
|
-
sourceLocale,
|
|
945
|
-
targetLocale
|
|
946
|
-
);
|
|
947
|
-
return provider === void 0 || model === void 0;
|
|
948
|
-
});
|
|
949
|
-
}
|
|
950
|
-
function getLocaleModel(localeModels, sourceLocale, targetLocale) {
|
|
951
|
-
const localeKeys = [
|
|
952
|
-
`${sourceLocale}:${targetLocale}`,
|
|
953
|
-
`*:${targetLocale}`,
|
|
954
|
-
`${sourceLocale}:*`,
|
|
955
|
-
"*:*"
|
|
956
|
-
];
|
|
957
|
-
const modelKey = localeKeys.find((key) => localeModels.hasOwnProperty(key));
|
|
958
|
-
if (modelKey) {
|
|
959
|
-
const value = localeModels[modelKey];
|
|
960
|
-
const firstColonIndex = value?.indexOf(":");
|
|
961
|
-
if (value && firstColonIndex !== -1 && firstColonIndex !== void 0) {
|
|
962
|
-
const provider2 = value.substring(0, firstColonIndex);
|
|
963
|
-
const model2 = value.substring(firstColonIndex + 1);
|
|
964
|
-
if (provider2 && model2) {
|
|
965
|
-
return { provider: provider2, model: model2 };
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
const [provider, model] = value?.split(":") || [];
|
|
969
|
-
if (provider && model) {
|
|
970
|
-
return { provider, model };
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
return { provider: void 0, model: void 0 };
|
|
974
|
-
}
|
|
975
|
-
|
|
976
|
-
// src/lib/lcp/api/prompt.ts
|
|
977
|
-
var prompt_default = (args) => {
|
|
978
|
-
return getUserSystemPrompt(args) || getBuiltInSystemPrompt(args);
|
|
979
|
-
};
|
|
980
|
-
function getUserSystemPrompt(args) {
|
|
981
|
-
const userPrompt = args.prompt?.trim()?.replace("{SOURCE_LOCALE}", args.sourceLocale)?.replace("{TARGET_LOCALE}", args.targetLocale);
|
|
982
|
-
if (userPrompt) {
|
|
983
|
-
console.log("\u2728 Compiler is using user-defined prompt.");
|
|
984
|
-
return userPrompt;
|
|
985
|
-
}
|
|
986
|
-
return void 0;
|
|
987
|
-
}
|
|
988
|
-
function getBuiltInSystemPrompt(args) {
|
|
989
|
-
return `
|
|
990
|
-
# Identity
|
|
991
|
-
|
|
992
|
-
You are an advanced AI localization engine. You do state-of-the-art localization for software products.
|
|
993
|
-
Your task is to localize pieces of data from one locale to another locale.
|
|
994
|
-
You always consider context, cultural nuances of source and target locales, and specific localization requirements.
|
|
995
|
-
You replicate the meaning, intent, style, tone, and purpose of the original data.
|
|
996
|
-
|
|
997
|
-
## Setup
|
|
998
|
-
|
|
999
|
-
Source language (locale code): ${args.sourceLocale}
|
|
1000
|
-
Target language (locale code): ${args.targetLocale}
|
|
1001
|
-
|
|
1002
|
-
## Guidelines
|
|
1003
|
-
|
|
1004
|
-
Follow these guidelines for translation:
|
|
1005
|
-
|
|
1006
|
-
1. Analyze the source text to understand its overall context and purpose
|
|
1007
|
-
2. Translate the meaning and intent rather than word-for-word translation
|
|
1008
|
-
3. Rephrase and restructure sentences to sound natural and fluent in the target language
|
|
1009
|
-
4. Adapt idiomatic expressions and cultural references for the target audience
|
|
1010
|
-
5. Maintain the style and tone of the source text
|
|
1011
|
-
6. You must produce valid UTF-8 encoded output
|
|
1012
|
-
7. YOU MUST ONLY PRODUCE VALID XML.
|
|
1013
|
-
|
|
1014
|
-
## Special Instructions
|
|
1015
|
-
|
|
1016
|
-
Do not localize any of these technical elements:
|
|
1017
|
-
- Variables like {variable}, {variable.key}, {data[type]}
|
|
1018
|
-
- Expressions like <expression/>
|
|
1019
|
-
- Functions like <function:value/>, <function:getDisplayName/>
|
|
1020
|
-
- Elements like <element:strong>, </element:strong>, <element:LuPlus>, </element:LuPlus>, <element:LuSparkles>, </element:LuSparkles>
|
|
1021
|
-
|
|
1022
|
-
Remember, you are a context-aware multilingual assistant helping international companies.
|
|
1023
|
-
Your goal is to perform state-of-the-art localization for software products and content.
|
|
1024
|
-
`;
|
|
1025
|
-
}
|
|
1026
|
-
|
|
1027
|
-
// src/lib/lcp/api/xml2obj.ts
|
|
1028
|
-
import { XMLParser, XMLBuilder } from "fast-xml-parser";
|
|
1029
|
-
import _4 from "lodash";
|
|
1030
|
-
var TAG_OBJECT = "object";
|
|
1031
|
-
var TAG_ARRAY = "array";
|
|
1032
|
-
var TAG_VALUE = "value";
|
|
1033
|
-
function _toGenericNode(value, key) {
|
|
1034
|
-
if (_4.isArray(value)) {
|
|
1035
|
-
const children = _4.map(value, (item) => _toGenericNode(item));
|
|
1036
|
-
return {
|
|
1037
|
-
[TAG_ARRAY]: {
|
|
1038
|
-
...key ? { key } : {},
|
|
1039
|
-
..._groupChildren(children)
|
|
1040
|
-
}
|
|
1041
|
-
};
|
|
1042
|
-
}
|
|
1043
|
-
if (_4.isPlainObject(value)) {
|
|
1044
|
-
const children = _4.map(
|
|
1045
|
-
Object.entries(value),
|
|
1046
|
-
([k, v]) => _toGenericNode(v, k)
|
|
1047
|
-
);
|
|
1048
|
-
return {
|
|
1049
|
-
[TAG_OBJECT]: {
|
|
1050
|
-
...key ? { key } : {},
|
|
1051
|
-
..._groupChildren(children)
|
|
1052
|
-
}
|
|
1053
|
-
};
|
|
1054
|
-
}
|
|
1055
|
-
return {
|
|
1056
|
-
[TAG_VALUE]: {
|
|
1057
|
-
...key ? { key } : {},
|
|
1058
|
-
"#text": value ?? ""
|
|
1059
|
-
}
|
|
1060
|
-
};
|
|
1061
|
-
}
|
|
1062
|
-
function _groupChildren(nodes) {
|
|
1063
|
-
return _4(nodes).groupBy((node) => Object.keys(node)[0]).mapValues((arr) => _4.map(arr, (n) => n[Object.keys(n)[0]])).value();
|
|
1064
|
-
}
|
|
1065
|
-
function _fromGenericNode(tag, data) {
|
|
1066
|
-
if (tag === TAG_VALUE) {
|
|
1067
|
-
if (_4.isPlainObject(data)) {
|
|
1068
|
-
return _4.get(data, "#text", "");
|
|
1069
|
-
}
|
|
1070
|
-
return data ?? "";
|
|
1071
|
-
}
|
|
1072
|
-
if (tag === TAG_ARRAY) {
|
|
1073
|
-
const result = [];
|
|
1074
|
-
_4.forEach([TAG_VALUE, TAG_OBJECT, TAG_ARRAY], (childTag) => {
|
|
1075
|
-
const childNodes = _4.castArray(_4.get(data, childTag, []));
|
|
1076
|
-
_4.forEach(childNodes, (child) => {
|
|
1077
|
-
result.push(_fromGenericNode(childTag, child));
|
|
1078
|
-
});
|
|
1079
|
-
});
|
|
1080
|
-
return result;
|
|
1081
|
-
}
|
|
1082
|
-
const obj = {};
|
|
1083
|
-
_4.forEach([TAG_VALUE, TAG_OBJECT, TAG_ARRAY], (childTag) => {
|
|
1084
|
-
const childNodes = _4.castArray(_4.get(data, childTag, []));
|
|
1085
|
-
_4.forEach(childNodes, (child) => {
|
|
1086
|
-
const key = _4.get(child, "key", "");
|
|
1087
|
-
obj[key] = _fromGenericNode(childTag, child);
|
|
1088
|
-
});
|
|
1089
|
-
});
|
|
1090
|
-
return obj;
|
|
1091
|
-
}
|
|
1092
|
-
function obj2xml(obj) {
|
|
1093
|
-
const rootNode = _toGenericNode(obj)[TAG_OBJECT];
|
|
1094
|
-
const builder = new XMLBuilder({
|
|
1095
|
-
ignoreAttributes: false,
|
|
1096
|
-
attributeNamePrefix: "",
|
|
1097
|
-
format: true,
|
|
1098
|
-
suppressEmptyNode: true
|
|
1099
|
-
});
|
|
1100
|
-
return builder.build({ [TAG_OBJECT]: rootNode });
|
|
1101
|
-
}
|
|
1102
|
-
function xml2obj(xml) {
|
|
1103
|
-
const parser2 = new XMLParser({
|
|
1104
|
-
ignoreAttributes: false,
|
|
1105
|
-
attributeNamePrefix: "",
|
|
1106
|
-
parseTagValue: true,
|
|
1107
|
-
parseAttributeValue: false,
|
|
1108
|
-
processEntities: true,
|
|
1109
|
-
isArray: (name) => [TAG_VALUE, TAG_ARRAY, TAG_OBJECT].includes(name)
|
|
1110
|
-
});
|
|
1111
|
-
const parsed = parser2.parse(xml);
|
|
1112
|
-
const withoutDeclaration = _4.omit(parsed, "?xml");
|
|
1113
|
-
const rootTag = Object.keys(withoutDeclaration)[0];
|
|
1114
|
-
const rootNode = _4.castArray(withoutDeclaration[rootTag])[0];
|
|
1115
|
-
return _fromGenericNode(rootTag, rootNode);
|
|
1116
|
-
}
|
|
1117
|
-
|
|
1118
|
-
// src/lib/lcp/api/shots.ts
|
|
1119
|
-
var shots_default = [
|
|
1120
|
-
// Shot #1
|
|
1121
|
-
[
|
|
1122
|
-
{
|
|
1123
|
-
version: 0.1,
|
|
1124
|
-
locale: "en",
|
|
1125
|
-
files: {
|
|
1126
|
-
"demo-app/my-custom-header.tsx": {
|
|
1127
|
-
entries: {
|
|
1128
|
-
"1z2x3c4v": "Dashboard",
|
|
1129
|
-
"5t6y7u8i": "Settings",
|
|
1130
|
-
"9o0p1q2r": "Logout"
|
|
1131
|
-
}
|
|
1132
|
-
},
|
|
1133
|
-
"demo-app/my-custom-footer.tsx": {
|
|
1134
|
-
entries: {
|
|
1135
|
-
"9k0l1m2n": "\xA9 2025 Lingo.dev. All rights reserved."
|
|
1136
|
-
}
|
|
1137
|
-
}
|
|
1138
|
-
}
|
|
1139
|
-
},
|
|
1140
|
-
{
|
|
1141
|
-
version: 0.1,
|
|
1142
|
-
locale: "es",
|
|
1143
|
-
files: {
|
|
1144
|
-
"demo-app/my-custom-header.tsx": {
|
|
1145
|
-
entries: {
|
|
1146
|
-
"1z2x3c4v": "Panel de control",
|
|
1147
|
-
"5t6y7u8i": "Configuraci\xF3n",
|
|
1148
|
-
"9o0p1q2r": "Cerrar sesi\xF3n"
|
|
1149
|
-
}
|
|
1150
|
-
},
|
|
1151
|
-
"demo-app/my-custom-footer.tsx": {
|
|
1152
|
-
entries: {
|
|
1153
|
-
"9k0l1m2n": "\xA9 2025 Lingo.dev. Todos los derechos reservados."
|
|
1154
|
-
}
|
|
1155
|
-
}
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1158
|
-
]
|
|
1159
|
-
// More shots here...
|
|
1160
|
-
];
|
|
1161
|
-
|
|
1162
|
-
// src/utils/rc.ts
|
|
1163
|
-
import os from "os";
|
|
1164
|
-
import path3 from "path";
|
|
1165
|
-
import fs3 from "fs";
|
|
1166
|
-
import Ini from "ini";
|
|
1167
|
-
function getRc() {
|
|
1168
|
-
const settingsFile = ".lingodotdevrc";
|
|
1169
|
-
const homedir = os.homedir();
|
|
1170
|
-
const settingsFilePath = path3.join(homedir, settingsFile);
|
|
1171
|
-
const content = fs3.existsSync(settingsFilePath) ? fs3.readFileSync(settingsFilePath, "utf-8") : "";
|
|
1172
|
-
const data = Ini.parse(content);
|
|
1173
|
-
return data;
|
|
1174
|
-
}
|
|
1175
|
-
|
|
1176
|
-
// src/utils/llm-api-key.ts
|
|
1177
|
-
import _5 from "lodash";
|
|
1178
|
-
import * as dotenv from "dotenv";
|
|
1179
|
-
import path4 from "path";
|
|
1180
|
-
function getKeyFromEnv(envVarName) {
|
|
1181
|
-
if (process.env[envVarName]) {
|
|
1182
|
-
return process.env[envVarName];
|
|
1183
|
-
}
|
|
1184
|
-
const result = dotenv.config({
|
|
1185
|
-
path: [
|
|
1186
|
-
path4.resolve(process.cwd(), ".env"),
|
|
1187
|
-
path4.resolve(process.cwd(), ".env.local"),
|
|
1188
|
-
path4.resolve(process.cwd(), ".env.development")
|
|
1189
|
-
]
|
|
1190
|
-
});
|
|
1191
|
-
return result?.parsed?.[envVarName];
|
|
1192
|
-
}
|
|
1193
|
-
function getKeyFromRc(rcPath) {
|
|
1194
|
-
const rc = getRc();
|
|
1195
|
-
const result = _5.get(rc, rcPath);
|
|
1196
|
-
return typeof result === "string" ? result : void 0;
|
|
1197
|
-
}
|
|
1198
|
-
function getGroqKey() {
|
|
1199
|
-
return getGroqKeyFromEnv() || getGroqKeyFromRc();
|
|
1200
|
-
}
|
|
1201
|
-
function getGroqKeyFromRc() {
|
|
1202
|
-
return getKeyFromRc("llm.groqApiKey");
|
|
1203
|
-
}
|
|
1204
|
-
function getGroqKeyFromEnv() {
|
|
1205
|
-
return getKeyFromEnv("GROQ_API_KEY");
|
|
1206
|
-
}
|
|
1207
|
-
function getLingoDotDevKeyFromEnv() {
|
|
1208
|
-
return getKeyFromEnv("LINGODOTDEV_API_KEY");
|
|
1209
|
-
}
|
|
1210
|
-
function getLingoDotDevKeyFromRc() {
|
|
1211
|
-
return getKeyFromRc("auth.apiKey");
|
|
1212
|
-
}
|
|
1213
|
-
function getLingoDotDevKey() {
|
|
1214
|
-
return getLingoDotDevKeyFromEnv() || getLingoDotDevKeyFromRc();
|
|
1215
|
-
}
|
|
1216
|
-
function getGoogleKey() {
|
|
1217
|
-
return getGoogleKeyFromEnv() || getGoogleKeyFromRc();
|
|
1218
|
-
}
|
|
1219
|
-
function getGoogleKeyFromRc() {
|
|
1220
|
-
return getKeyFromRc("llm.googleApiKey");
|
|
1221
|
-
}
|
|
1222
|
-
function getGoogleKeyFromEnv() {
|
|
1223
|
-
return getKeyFromEnv("GOOGLE_API_KEY");
|
|
1224
|
-
}
|
|
1225
|
-
function getOpenRouterKey() {
|
|
1226
|
-
return getOpenRouterKeyFromEnv() || getOpenRouterKeyFromRc();
|
|
1227
|
-
}
|
|
1228
|
-
function getOpenRouterKeyFromRc() {
|
|
1229
|
-
return getKeyFromRc("llm.openrouterApiKey");
|
|
1230
|
-
}
|
|
1231
|
-
function getOpenRouterKeyFromEnv() {
|
|
1232
|
-
return getKeyFromEnv("OPENROUTER_API_KEY");
|
|
1233
|
-
}
|
|
1234
|
-
|
|
1235
|
-
// src/lib/lcp/api/index.ts
|
|
1236
|
-
import dedent2 from "dedent";
|
|
1237
|
-
|
|
1238
|
-
// src/utils/env.ts
|
|
1239
|
-
import fs4 from "fs";
|
|
1240
|
-
function isRunningInCIOrDocker() {
|
|
1241
|
-
return Boolean(process.env.CI) || fs4.existsSync("/.dockerenv");
|
|
1242
|
-
}
|
|
1243
|
-
|
|
1244
|
-
// src/lib/lcp/api/provider-details.ts
|
|
1245
|
-
var providerDetails = {
|
|
1246
|
-
groq: {
|
|
1247
|
-
name: "Groq",
|
|
1248
|
-
apiKeyEnvVar: "GROQ_API_KEY",
|
|
1249
|
-
apiKeyConfigKey: "llm.groqApiKey",
|
|
1250
|
-
getKeyLink: "https://groq.com",
|
|
1251
|
-
docsLink: "https://console.groq.com/docs/errors"
|
|
1252
|
-
},
|
|
1253
|
-
google: {
|
|
1254
|
-
name: "Google",
|
|
1255
|
-
apiKeyEnvVar: "GOOGLE_API_KEY",
|
|
1256
|
-
apiKeyConfigKey: "llm.googleApiKey",
|
|
1257
|
-
getKeyLink: "https://ai.google.dev/",
|
|
1258
|
-
docsLink: "https://ai.google.dev/gemini-api/docs/troubleshooting"
|
|
1259
|
-
},
|
|
1260
|
-
openrouter: {
|
|
1261
|
-
name: "OpenRouter",
|
|
1262
|
-
apiKeyEnvVar: "OPENROUTER_API_KEY",
|
|
1263
|
-
apiKeyConfigKey: "llm.openrouterApiKey",
|
|
1264
|
-
getKeyLink: "https://openrouter.ai",
|
|
1265
|
-
docsLink: "https://openrouter.ai/docs"
|
|
1266
|
-
},
|
|
1267
|
-
ollama: {
|
|
1268
|
-
name: "Ollama",
|
|
1269
|
-
apiKeyEnvVar: void 0,
|
|
1270
|
-
// Ollama doesn't require an API key
|
|
1271
|
-
apiKeyConfigKey: void 0,
|
|
1272
|
-
// Ollama doesn't require an API key
|
|
1273
|
-
getKeyLink: "https://ollama.com/download",
|
|
1274
|
-
docsLink: "https://github.com/ollama/ollama/tree/main/docs"
|
|
1275
|
-
}
|
|
1276
|
-
};
|
|
1277
|
-
|
|
1278
|
-
// src/lib/lcp/api/index.ts
|
|
1279
|
-
var LCPAPI = class {
|
|
1280
|
-
static async translate(models, sourceDictionary, sourceLocale, targetLocale) {
|
|
1281
|
-
const timeLabel = `LCPAPI.translate: ${targetLocale}`;
|
|
1282
|
-
console.time(timeLabel);
|
|
1283
|
-
const chunks = this._chunkDictionary(sourceDictionary);
|
|
1284
|
-
const translatedChunks = [];
|
|
1285
|
-
for (const chunk of chunks) {
|
|
1286
|
-
const translatedChunk = await this._translateChunk(
|
|
1287
|
-
models,
|
|
1288
|
-
chunk,
|
|
1289
|
-
sourceLocale,
|
|
1290
|
-
targetLocale
|
|
1291
|
-
);
|
|
1292
|
-
translatedChunks.push(translatedChunk);
|
|
1293
|
-
}
|
|
1294
|
-
const result = this._mergeDictionaries(translatedChunks);
|
|
1295
|
-
console.timeEnd(timeLabel);
|
|
1296
|
-
return result;
|
|
1297
|
-
}
|
|
1298
|
-
static _chunkDictionary(dictionary) {
|
|
1299
|
-
const MAX_ENTRIES_PER_CHUNK = 100;
|
|
1300
|
-
const { files, ...rest } = dictionary;
|
|
1301
|
-
const chunks = [];
|
|
1302
|
-
let currentChunk = {
|
|
1303
|
-
...rest,
|
|
1304
|
-
files: {}
|
|
1305
|
-
};
|
|
1306
|
-
let currentEntryCount = 0;
|
|
1307
|
-
Object.entries(files).forEach(([fileName, file]) => {
|
|
1308
|
-
const entries = file.entries;
|
|
1309
|
-
const entryPairs = Object.entries(entries);
|
|
1310
|
-
let currentIndex = 0;
|
|
1311
|
-
while (currentIndex < entryPairs.length) {
|
|
1312
|
-
const remainingSpace = MAX_ENTRIES_PER_CHUNK - currentEntryCount;
|
|
1313
|
-
const entriesToAdd = entryPairs.slice(
|
|
1314
|
-
currentIndex,
|
|
1315
|
-
currentIndex + remainingSpace
|
|
1316
|
-
);
|
|
1317
|
-
if (entriesToAdd.length > 0) {
|
|
1318
|
-
currentChunk.files[fileName] = currentChunk.files[fileName] || {
|
|
1319
|
-
entries: {}
|
|
1320
|
-
};
|
|
1321
|
-
currentChunk.files[fileName].entries = {
|
|
1322
|
-
...currentChunk.files[fileName].entries,
|
|
1323
|
-
...Object.fromEntries(entriesToAdd)
|
|
1324
|
-
};
|
|
1325
|
-
currentEntryCount += entriesToAdd.length;
|
|
1326
|
-
}
|
|
1327
|
-
currentIndex += entriesToAdd.length;
|
|
1328
|
-
if (currentEntryCount >= MAX_ENTRIES_PER_CHUNK || currentIndex < entryPairs.length && currentEntryCount + (entryPairs.length - currentIndex) > MAX_ENTRIES_PER_CHUNK) {
|
|
1329
|
-
chunks.push(currentChunk);
|
|
1330
|
-
currentChunk = { ...rest, files: {} };
|
|
1331
|
-
currentEntryCount = 0;
|
|
1332
|
-
}
|
|
1333
|
-
}
|
|
1334
|
-
});
|
|
1335
|
-
if (currentEntryCount > 0) {
|
|
1336
|
-
chunks.push(currentChunk);
|
|
1337
|
-
}
|
|
1338
|
-
return chunks;
|
|
1339
|
-
}
|
|
1340
|
-
static _mergeDictionaries(dictionaries) {
|
|
1341
|
-
const fileNames = _6.uniq(
|
|
1342
|
-
_6.flatMap(dictionaries, (dict) => Object.keys(dict.files))
|
|
1343
|
-
);
|
|
1344
|
-
const files = _6(fileNames).map((fileName) => {
|
|
1345
|
-
const entries = dictionaries.reduce((entries2, dict) => {
|
|
1346
|
-
const file = dict.files[fileName];
|
|
1347
|
-
if (file) {
|
|
1348
|
-
entries2 = _6.merge(entries2, file.entries);
|
|
1349
|
-
}
|
|
1350
|
-
return entries2;
|
|
1351
|
-
}, {});
|
|
1352
|
-
return [fileName, { entries }];
|
|
1353
|
-
}).fromPairs().value();
|
|
1354
|
-
const dictionary = {
|
|
1355
|
-
version: dictionaries[0].version,
|
|
1356
|
-
locale: dictionaries[0].locale,
|
|
1357
|
-
files
|
|
1358
|
-
};
|
|
1359
|
-
return dictionary;
|
|
1360
|
-
}
|
|
1361
|
-
static _createLingoDotDevEngine() {
|
|
1362
|
-
if (isRunningInCIOrDocker()) {
|
|
1363
|
-
const apiKeyFromEnv = getLingoDotDevKeyFromEnv();
|
|
1364
|
-
if (!apiKeyFromEnv) {
|
|
1365
|
-
this._failMissingLLMKeyCi("lingo.dev");
|
|
1366
|
-
}
|
|
1367
|
-
}
|
|
1368
|
-
const apiKey = getLingoDotDevKey();
|
|
1369
|
-
if (!apiKey) {
|
|
1370
|
-
throw new Error(
|
|
1371
|
-
"\u26A0\uFE0F Lingo.dev API key not found. Please set LINGODOTDEV_API_KEY environment variable or configure it user-wide."
|
|
1372
|
-
);
|
|
1373
|
-
}
|
|
1374
|
-
console.log(`Creating Lingo.dev client`);
|
|
1375
|
-
return new LingoDotDevEngine({
|
|
1376
|
-
apiKey
|
|
1377
|
-
});
|
|
1378
|
-
}
|
|
1379
|
-
static async _translateChunk(models, sourceDictionary, sourceLocale, targetLocale) {
|
|
1380
|
-
if (models === "lingo.dev") {
|
|
1381
|
-
try {
|
|
1382
|
-
const lingoDotDevEngine = this._createLingoDotDevEngine();
|
|
1383
|
-
console.log(
|
|
1384
|
-
`\u2728 Using Lingo.dev Engine to localize from "${sourceLocale}" to "${targetLocale}"`
|
|
1385
|
-
);
|
|
1386
|
-
const result = await lingoDotDevEngine.localizeObject(
|
|
1387
|
-
sourceDictionary,
|
|
1388
|
-
{
|
|
1389
|
-
sourceLocale,
|
|
1390
|
-
targetLocale
|
|
1391
|
-
}
|
|
1392
|
-
);
|
|
1393
|
-
return result;
|
|
1394
|
-
} catch (error) {
|
|
1395
|
-
this._failLLMFailureLocal(
|
|
1396
|
-
"lingo.dev",
|
|
1397
|
-
targetLocale,
|
|
1398
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
1399
|
-
);
|
|
1400
|
-
throw error;
|
|
1401
|
-
}
|
|
1402
|
-
} else {
|
|
1403
|
-
const { provider, model } = getLocaleModel(
|
|
1404
|
-
models,
|
|
1405
|
-
sourceLocale,
|
|
1406
|
-
targetLocale
|
|
1407
|
-
);
|
|
1408
|
-
if (!provider || !model) {
|
|
1409
|
-
throw new Error(
|
|
1410
|
-
dedent2`
|
|
1411
|
-
🚫 Lingo.dev Localization Engine Not Configured!
|
|
1412
|
-
|
|
1413
|
-
The "models" parameter is missing or incomplete in your Lingo.dev configuration.
|
|
1414
|
-
|
|
1415
|
-
👉 To fix this, set the "models" parameter to either:
|
|
1416
|
-
• "lingo.dev" (for the default engine)
|
|
1417
|
-
• a map of locale-to-model, e.g. { "models": { "en:es": "openai:gpt-3.5-turbo" } }
|
|
1418
|
-
|
|
1419
|
-
Example:
|
|
1420
|
-
{
|
|
1421
|
-
// ...
|
|
1422
|
-
"models": "lingo.dev"
|
|
1423
|
-
}
|
|
1424
|
-
|
|
1425
|
-
For more details, see: https://lingo.dev/compiler
|
|
1426
|
-
To get help, join our Discord: https://lingo.dev/go/discord
|
|
1427
|
-
`
|
|
1428
|
-
);
|
|
1429
|
-
}
|
|
1430
|
-
try {
|
|
1431
|
-
const aiModel = this._createAiModel(provider, model, targetLocale);
|
|
1432
|
-
console.log(
|
|
1433
|
-
`\u2139\uFE0F Using raw LLM API ("${provider}":"${model}") to translate from "${sourceLocale}" to "${targetLocale}"`
|
|
1434
|
-
);
|
|
1435
|
-
const response = await generateText({
|
|
1436
|
-
model: aiModel,
|
|
1437
|
-
messages: [
|
|
1438
|
-
{
|
|
1439
|
-
role: "system",
|
|
1440
|
-
content: prompt_default({ sourceLocale, targetLocale })
|
|
1441
|
-
},
|
|
1442
|
-
...shots_default.flatMap((shotsTuple) => [
|
|
1443
|
-
{
|
|
1444
|
-
role: "user",
|
|
1445
|
-
content: obj2xml(shotsTuple[0])
|
|
1446
|
-
},
|
|
1447
|
-
{
|
|
1448
|
-
role: "assistant",
|
|
1449
|
-
content: obj2xml(shotsTuple[1])
|
|
1450
|
-
}
|
|
1451
|
-
]),
|
|
1452
|
-
{
|
|
1453
|
-
role: "user",
|
|
1454
|
-
content: obj2xml(sourceDictionary)
|
|
1455
|
-
}
|
|
1456
|
-
]
|
|
1457
|
-
});
|
|
1458
|
-
console.log("Response text received for", targetLocale);
|
|
1459
|
-
let responseText = response.text;
|
|
1460
|
-
responseText = responseText.substring(
|
|
1461
|
-
responseText.indexOf("<"),
|
|
1462
|
-
responseText.lastIndexOf(">") + 1
|
|
1463
|
-
);
|
|
1464
|
-
return xml2obj(responseText);
|
|
1465
|
-
} catch (error) {
|
|
1466
|
-
this._failLLMFailureLocal(
|
|
1467
|
-
provider,
|
|
1468
|
-
targetLocale,
|
|
1469
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
1470
|
-
);
|
|
1471
|
-
throw error;
|
|
1472
|
-
}
|
|
1473
|
-
}
|
|
1474
|
-
}
|
|
1475
|
-
/**
|
|
1476
|
-
* Instantiates an AI model based on provider and model ID.
|
|
1477
|
-
* Includes CI/CD API key checks.
|
|
1478
|
-
* @param providerId The ID of the AI provider (e.g., "groq", "google").
|
|
1479
|
-
* @param modelId The ID of the specific model (e.g., "llama3-8b-8192", "gemini-2.0-flash").
|
|
1480
|
-
* @param targetLocale The target locale being translated to (for logging/error messages).
|
|
1481
|
-
* @returns An instantiated AI LanguageModel.
|
|
1482
|
-
* @throws Error if the provider is not supported or API key is missing in CI/CD.
|
|
1483
|
-
*/
|
|
1484
|
-
static _createAiModel(providerId, modelId, targetLocale) {
|
|
1485
|
-
switch (providerId) {
|
|
1486
|
-
case "groq": {
|
|
1487
|
-
if (isRunningInCIOrDocker()) {
|
|
1488
|
-
const groqFromEnv = getGroqKeyFromEnv();
|
|
1489
|
-
if (!groqFromEnv) {
|
|
1490
|
-
this._failMissingLLMKeyCi(providerId);
|
|
1491
|
-
}
|
|
1492
|
-
}
|
|
1493
|
-
const groqKey = getGroqKey();
|
|
1494
|
-
if (!groqKey) {
|
|
1495
|
-
throw new Error(
|
|
1496
|
-
"\u26A0\uFE0F GROQ API key not found. Please set GROQ_API_KEY environment variable or configure it user-wide."
|
|
1497
|
-
);
|
|
1498
|
-
}
|
|
1499
|
-
console.log(
|
|
1500
|
-
`Creating Groq client for ${targetLocale} using model ${modelId}`
|
|
1501
|
-
);
|
|
1502
|
-
return createGroq({ apiKey: groqKey })(modelId);
|
|
1503
|
-
}
|
|
1504
|
-
case "google": {
|
|
1505
|
-
if (isRunningInCIOrDocker()) {
|
|
1506
|
-
const googleFromEnv = getGoogleKeyFromEnv();
|
|
1507
|
-
if (!googleFromEnv) {
|
|
1508
|
-
this._failMissingLLMKeyCi(providerId);
|
|
1509
|
-
}
|
|
1510
|
-
}
|
|
1511
|
-
const googleKey = getGoogleKey();
|
|
1512
|
-
if (!googleKey) {
|
|
1513
|
-
throw new Error(
|
|
1514
|
-
"\u26A0\uFE0F Google API key not found. Please set GOOGLE_API_KEY environment variable or configure it user-wide."
|
|
1515
|
-
);
|
|
1516
|
-
}
|
|
1517
|
-
console.log(
|
|
1518
|
-
`Creating Google Generative AI client for ${targetLocale} using model ${modelId}`
|
|
1519
|
-
);
|
|
1520
|
-
return createGoogleGenerativeAI({ apiKey: googleKey })(modelId);
|
|
1521
|
-
}
|
|
1522
|
-
case "openrouter": {
|
|
1523
|
-
if (isRunningInCIOrDocker()) {
|
|
1524
|
-
const openRouterFromEnv = getOpenRouterKeyFromEnv();
|
|
1525
|
-
if (!openRouterFromEnv) {
|
|
1526
|
-
this._failMissingLLMKeyCi(providerId);
|
|
1527
|
-
}
|
|
1528
|
-
}
|
|
1529
|
-
const openRouterKey = getOpenRouterKey();
|
|
1530
|
-
if (!openRouterKey) {
|
|
1531
|
-
throw new Error(
|
|
1532
|
-
"\u26A0\uFE0F OpenRouter API key not found. Please set OPENROUTER_API_KEY environment variable or configure it user-wide."
|
|
1533
|
-
);
|
|
1534
|
-
}
|
|
1535
|
-
console.log(
|
|
1536
|
-
`Creating OpenRouter client for ${targetLocale} using model ${modelId}`
|
|
1537
|
-
);
|
|
1538
|
-
return createOpenRouter({
|
|
1539
|
-
apiKey: openRouterKey
|
|
1540
|
-
})(modelId);
|
|
1541
|
-
}
|
|
1542
|
-
case "ollama": {
|
|
1543
|
-
console.log(
|
|
1544
|
-
`Creating Ollama client for ${targetLocale} using model ${modelId} at default Ollama address`
|
|
1545
|
-
);
|
|
1546
|
-
return createOllama()(modelId);
|
|
1547
|
-
}
|
|
1548
|
-
default: {
|
|
1549
|
-
throw new Error(
|
|
1550
|
-
`\u26A0\uFE0F Provider "${providerId}" for locale "${targetLocale}" is not supported. Only "groq" and "google" providers are supported at the moment.`
|
|
1551
|
-
);
|
|
1552
|
-
}
|
|
1553
|
-
}
|
|
1554
|
-
}
|
|
1555
|
-
/**
|
|
1556
|
-
* Show an actionable error message and exit the process when the compiler
|
|
1557
|
-
* is running in CI/CD without a required LLM API key.
|
|
1558
|
-
* The message explains why this situation is unusual and how to fix it.
|
|
1559
|
-
* @param providerId The ID of the LLM provider whose key is missing.
|
|
1560
|
-
*/
|
|
1561
|
-
static _failMissingLLMKeyCi(providerId) {
|
|
1562
|
-
let details = providerDetails[providerId];
|
|
1563
|
-
if (!details) {
|
|
1564
|
-
console.error(
|
|
1565
|
-
`Internal Error: Missing details for provider "${providerId}" when reporting missing key in CI/CD. You might be using an unsupported provider.`
|
|
1566
|
-
);
|
|
1567
|
-
process.exit(1);
|
|
1568
|
-
}
|
|
1569
|
-
console.log(
|
|
1570
|
-
dedent2`
|
|
1571
|
-
\n
|
|
1572
|
-
💡 You're using Lingo.dev Localization Compiler, and it detected unlocalized components in your app.
|
|
1573
|
-
|
|
1574
|
-
The compiler needs a ${details.name} API key to translate missing strings, but ${details.apiKeyEnvVar} is not set in the environment.
|
|
1575
|
-
|
|
1576
|
-
This is unexpected: typically you run a full build locally, commit the generated translation files, and push them to CI/CD.
|
|
1577
|
-
|
|
1578
|
-
However, If you want CI/CD to translate the new strings, provide the key with:
|
|
1579
|
-
• Session-wide: export ${details.apiKeyEnvVar}=<your-api-key>
|
|
1580
|
-
• Project-wide / CI: add ${details.apiKeyEnvVar}=<your-api-key> to your pipeline environment variables
|
|
1581
|
-
|
|
1582
|
-
⭐️ Also:
|
|
1583
|
-
1. If you don't yet have a ${details.name} API key, get one for free at ${details.getKeyLink}
|
|
1584
|
-
2. If you want to use a different LLM, update your configuration. Refer to documentation for help: https://lingo.dev/compiler
|
|
1585
|
-
3. If the model you want to use isn't supported yet, raise an issue in our open-source repo: https://lingo.dev/go/gh
|
|
1586
|
-
|
|
1587
|
-
✨
|
|
1588
|
-
`
|
|
1589
|
-
);
|
|
1590
|
-
process.exit(1);
|
|
1591
|
-
}
|
|
1592
|
-
/**
|
|
1593
|
-
* Show an actionable error message and exit the process when an LLM API call
|
|
1594
|
-
* fails during local compilation.
|
|
1595
|
-
* @param providerId The ID of the LLM provider that failed.
|
|
1596
|
-
* @param targetLocale The target locale being translated to.
|
|
1597
|
-
* @param errorMessage The error message received from the API.
|
|
1598
|
-
*/
|
|
1599
|
-
static _failLLMFailureLocal(providerId, targetLocale, errorMessage) {
|
|
1600
|
-
const details = providerDetails[providerId];
|
|
1601
|
-
if (!details) {
|
|
1602
|
-
console.error(
|
|
1603
|
-
`Internal Error: Missing details for provider "${providerId}" when reporting local failure.`
|
|
1604
|
-
);
|
|
1605
|
-
console.error(`Original Error: ${errorMessage}`);
|
|
1606
|
-
process.exit(1);
|
|
1607
|
-
}
|
|
1608
|
-
const isInvalidApiKey = errorMessage.match("Invalid API Key");
|
|
1609
|
-
if (isInvalidApiKey) {
|
|
1610
|
-
console.log(dedent2`
|
|
1611
|
-
\n
|
|
1612
|
-
⚠️ Lingo.dev Compiler requires a valid ${details.name} API key to translate your application.
|
|
1613
|
-
|
|
1614
|
-
It looks like you set ${details.name} API key but it is not valid. Please check your API key and try again.
|
|
1615
|
-
|
|
1616
|
-
Error details from ${details.name} API: ${errorMessage}
|
|
1617
|
-
|
|
1618
|
-
👉 You can set the API key in one of the following ways:
|
|
1619
|
-
1. User-wide: Run npx lingo.dev@latest config set ${details.apiKeyConfigKey} <your-api-key>
|
|
1620
|
-
2. Project-wide: Add ${details.apiKeyEnvVar}=<your-api-key> to .env file in every project that uses Lingo.dev Localization Compiler
|
|
1621
|
-
3 Session-wide: Run export ${details.apiKeyEnvVar}=<your-api-key> in your terminal before running the compiler to set the API key for the current session
|
|
1622
|
-
|
|
1623
|
-
⭐️ Also:
|
|
1624
|
-
1. If you don't yet have a ${details.name} API key, get one for free at ${details.getKeyLink}
|
|
1625
|
-
2. If you want to use a different LLM, raise an issue in our open-source repo: https://lingo.dev/go/gh
|
|
1626
|
-
3. If you have questions, feature requests, or would like to contribute, join our Discord: https://lingo.dev/go/discord
|
|
1627
|
-
|
|
1628
|
-
✨
|
|
1629
|
-
`);
|
|
1630
|
-
} else {
|
|
1631
|
-
console.log(
|
|
1632
|
-
dedent2`
|
|
1633
|
-
\n
|
|
1634
|
-
⚠️ Lingo.dev Compiler tried to translate your application to "${targetLocale}" locale via ${details.name} but it failed.
|
|
1635
|
-
|
|
1636
|
-
Error details from ${details.name} API: ${errorMessage}
|
|
1637
|
-
|
|
1638
|
-
This error comes from the ${details.name} API, please check their documentation for more details: ${details.docsLink}
|
|
1639
|
-
|
|
1640
|
-
⭐️ Also:
|
|
1641
|
-
1. Did you set ${details.apiKeyEnvVar ? `${details.apiKeyEnvVar}` : "the provider API key"} environment variable correctly ${!details.apiKeyEnvVar ? "(if required)" : ""}?
|
|
1642
|
-
2. Did you reach any limits of your ${details.name} account?
|
|
1643
|
-
3. If you have questions, feature requests, or would like to contribute, join our Discord: https://lingo.dev/go/discord
|
|
1644
|
-
|
|
1645
|
-
✨
|
|
1646
|
-
`
|
|
1647
|
-
);
|
|
1648
|
-
}
|
|
1649
|
-
process.exit(1);
|
|
1650
|
-
}
|
|
1651
|
-
};
|
|
1652
|
-
|
|
1653
|
-
// src/lib/lcp/server.ts
|
|
1654
|
-
var LCPServer = class {
|
|
1655
|
-
static inFlightPromise = null;
|
|
1656
|
-
static async loadDictionaries(params) {
|
|
1657
|
-
if (this.inFlightPromise) {
|
|
1658
|
-
return this.inFlightPromise;
|
|
1659
|
-
}
|
|
1660
|
-
this.inFlightPromise = (async () => {
|
|
1661
|
-
try {
|
|
1662
|
-
const targetLocales = _7.uniq([
|
|
1663
|
-
...params.targetLocales,
|
|
1664
|
-
params.sourceLocale
|
|
1665
|
-
]);
|
|
1666
|
-
const dictionaries = await Promise.all(
|
|
1667
|
-
targetLocales.map(
|
|
1668
|
-
(targetLocale) => this.loadDictionaryForLocale({ ...params, targetLocale })
|
|
1669
|
-
)
|
|
1670
|
-
);
|
|
1671
|
-
const result = _7.fromPairs(
|
|
1672
|
-
targetLocales.map((targetLocale, index) => [
|
|
1673
|
-
targetLocale,
|
|
1674
|
-
dictionaries[index]
|
|
1675
|
-
])
|
|
1676
|
-
);
|
|
1677
|
-
return result;
|
|
1678
|
-
} finally {
|
|
1679
|
-
this.inFlightPromise = null;
|
|
1680
|
-
}
|
|
1681
|
-
})();
|
|
1682
|
-
return this.inFlightPromise;
|
|
1683
|
-
}
|
|
1684
|
-
static async loadDictionaryForLocale(params) {
|
|
1685
|
-
const sourceDictionary = this._extractSourceDictionary(
|
|
1686
|
-
params.lcp,
|
|
1687
|
-
params.sourceLocale,
|
|
1688
|
-
params.targetLocale
|
|
1689
|
-
);
|
|
1690
|
-
const cacheParams = {
|
|
1691
|
-
lcp: params.lcp,
|
|
1692
|
-
sourceLocale: params.sourceLocale,
|
|
1693
|
-
lingoDir: params.lingoDir,
|
|
1694
|
-
sourceRoot: params.sourceRoot
|
|
1695
|
-
};
|
|
1696
|
-
if (this._countDictionaryEntries(sourceDictionary) === 0) {
|
|
1697
|
-
console.log(
|
|
1698
|
-
"Source dictionary is empty, returning empty dictionary for target locale"
|
|
1699
|
-
);
|
|
1700
|
-
return { ...sourceDictionary, locale: params.targetLocale };
|
|
1701
|
-
}
|
|
1702
|
-
const cache = LCPCache.readLocaleDictionary(
|
|
1703
|
-
params.targetLocale,
|
|
1704
|
-
cacheParams
|
|
1705
|
-
);
|
|
1706
|
-
const uncachedSourceDictionary = this._getDictionaryDiff(
|
|
1707
|
-
sourceDictionary,
|
|
1708
|
-
cache
|
|
1709
|
-
);
|
|
1710
|
-
let targetDictionary;
|
|
1711
|
-
let newTranslations;
|
|
1712
|
-
if (this._countDictionaryEntries(uncachedSourceDictionary) === 0) {
|
|
1713
|
-
targetDictionary = cache;
|
|
1714
|
-
} else if (params.targetLocale === params.sourceLocale) {
|
|
1715
|
-
console.log(
|
|
1716
|
-
"\u2139\uFE0F Lingo.dev returns source dictionary - source and target locales are the same"
|
|
1717
|
-
);
|
|
1718
|
-
await LCPCache.writeLocaleDictionary(sourceDictionary, cacheParams);
|
|
1719
|
-
return sourceDictionary;
|
|
1720
|
-
} else {
|
|
1721
|
-
newTranslations = await LCPAPI.translate(
|
|
1722
|
-
params.models,
|
|
1723
|
-
uncachedSourceDictionary,
|
|
1724
|
-
params.sourceLocale,
|
|
1725
|
-
params.targetLocale
|
|
1726
|
-
);
|
|
1727
|
-
targetDictionary = this._mergeDictionaries(newTranslations, cache);
|
|
1728
|
-
targetDictionary = {
|
|
1729
|
-
...targetDictionary,
|
|
1730
|
-
locale: params.targetLocale
|
|
1731
|
-
};
|
|
1732
|
-
await LCPCache.writeLocaleDictionary(targetDictionary, cacheParams);
|
|
1733
|
-
}
|
|
1734
|
-
const targetDictionaryWithFallback = this._mergeDictionaries(
|
|
1735
|
-
targetDictionary,
|
|
1736
|
-
sourceDictionary,
|
|
1737
|
-
true
|
|
1738
|
-
);
|
|
1739
|
-
const result = this._addOverridesToDictionary(
|
|
1740
|
-
targetDictionaryWithFallback,
|
|
1741
|
-
params.lcp,
|
|
1742
|
-
params.targetLocale
|
|
1743
|
-
);
|
|
1744
|
-
if (newTranslations) {
|
|
1745
|
-
console.log(
|
|
1746
|
-
`\u2139\uFE0F Lingo.dev dictionary for ${params.targetLocale}:
|
|
1747
|
-
- %d entries
|
|
1748
|
-
- %d cached
|
|
1749
|
-
- %d uncached
|
|
1750
|
-
- %d translated
|
|
1751
|
-
- %d overrides`,
|
|
1752
|
-
this._countDictionaryEntries(result),
|
|
1753
|
-
this._countDictionaryEntries(cache),
|
|
1754
|
-
this._countDictionaryEntries(uncachedSourceDictionary),
|
|
1755
|
-
newTranslations ? this._countDictionaryEntries(newTranslations) : 0,
|
|
1756
|
-
this._countDictionaryEntries(result) - this._countDictionaryEntries(targetDictionary)
|
|
1757
|
-
);
|
|
1758
|
-
}
|
|
1759
|
-
return result;
|
|
1760
|
-
}
|
|
1761
|
-
static _extractSourceDictionary(lcp, sourceLocale, targetLocale) {
|
|
1762
|
-
const dictionary = {
|
|
1763
|
-
version: 0.1,
|
|
1764
|
-
locale: sourceLocale,
|
|
1765
|
-
files: {}
|
|
1766
|
-
};
|
|
1767
|
-
for (const [fileKey, fileData] of Object.entries(lcp.files || {})) {
|
|
1768
|
-
for (const [scopeKey, scopeData] of Object.entries(
|
|
1769
|
-
fileData.scopes || {}
|
|
1770
|
-
)) {
|
|
1771
|
-
if (scopeData.skip) {
|
|
1772
|
-
continue;
|
|
1773
|
-
}
|
|
1774
|
-
if (this._getScopeLocaleOverride(scopeData, targetLocale)) {
|
|
1775
|
-
continue;
|
|
1776
|
-
}
|
|
1777
|
-
_7.set(
|
|
1778
|
-
dictionary,
|
|
1779
|
-
[
|
|
1780
|
-
"files",
|
|
1781
|
-
fileKey,
|
|
1782
|
-
"entries",
|
|
1783
|
-
scopeKey
|
|
1784
|
-
],
|
|
1785
|
-
scopeData.content
|
|
1786
|
-
);
|
|
1787
|
-
}
|
|
1788
|
-
}
|
|
1789
|
-
return dictionary;
|
|
1790
|
-
}
|
|
1791
|
-
static _addOverridesToDictionary(dictionary, lcp, targetLocale) {
|
|
1792
|
-
for (const [fileKey, fileData] of Object.entries(lcp.files || {})) {
|
|
1793
|
-
for (const [scopeKey, scopeData] of Object.entries(
|
|
1794
|
-
fileData.scopes || {}
|
|
1795
|
-
)) {
|
|
1796
|
-
const override = this._getScopeLocaleOverride(scopeData, targetLocale);
|
|
1797
|
-
if (!override) {
|
|
1798
|
-
continue;
|
|
1799
|
-
}
|
|
1800
|
-
_7.set(
|
|
1801
|
-
dictionary,
|
|
1802
|
-
[
|
|
1803
|
-
"files",
|
|
1804
|
-
fileKey,
|
|
1805
|
-
"entries",
|
|
1806
|
-
scopeKey
|
|
1807
|
-
],
|
|
1808
|
-
override
|
|
1809
|
-
);
|
|
1810
|
-
}
|
|
1811
|
-
}
|
|
1812
|
-
return dictionary;
|
|
1813
|
-
}
|
|
1814
|
-
static _getScopeLocaleOverride(scopeData, locale) {
|
|
1815
|
-
return _7.get(scopeData.overrides, locale) ?? null;
|
|
1816
|
-
}
|
|
1817
|
-
static _getDictionaryDiff(sourceDictionary, targetDictionary) {
|
|
1818
|
-
if (this._countDictionaryEntries(targetDictionary) === 0) {
|
|
1819
|
-
return sourceDictionary;
|
|
1820
|
-
}
|
|
1821
|
-
const files = _7(sourceDictionary.files).mapValues((file, fileName) => ({
|
|
1822
|
-
...file,
|
|
1823
|
-
entries: _7(file.entries).mapValues((entry, entryName) => {
|
|
1824
|
-
const targetEntry = _7.get(targetDictionary.files, [
|
|
1825
|
-
fileName,
|
|
1826
|
-
"entries",
|
|
1827
|
-
entryName
|
|
1828
|
-
]);
|
|
1829
|
-
if (targetEntry !== void 0) {
|
|
1830
|
-
return void 0;
|
|
1831
|
-
}
|
|
1832
|
-
return entry;
|
|
1833
|
-
}).pickBy((value) => value !== void 0).value()
|
|
1834
|
-
})).pickBy((value) => Object.keys(value.entries).length > 0).value();
|
|
1835
|
-
const dictionary = {
|
|
1836
|
-
version: sourceDictionary.version,
|
|
1837
|
-
locale: sourceDictionary.locale,
|
|
1838
|
-
files
|
|
1839
|
-
};
|
|
1840
|
-
return dictionary;
|
|
1841
|
-
}
|
|
1842
|
-
static _mergeDictionaries(sourceDictionary, targetDictionary, removeEmptyEntries = false) {
|
|
1843
|
-
const fileNames = _7.uniq([
|
|
1844
|
-
...Object.keys(sourceDictionary.files),
|
|
1845
|
-
...Object.keys(targetDictionary.files)
|
|
1846
|
-
]);
|
|
1847
|
-
const files = _7(fileNames).map((fileName) => {
|
|
1848
|
-
const sourceFile = _7.get(sourceDictionary.files, fileName);
|
|
1849
|
-
const targetFile = _7.get(targetDictionary.files, fileName);
|
|
1850
|
-
const entries = removeEmptyEntries ? _7.pickBy(
|
|
1851
|
-
sourceFile?.entries || {},
|
|
1852
|
-
(value) => String(value || "")?.trim?.()?.length > 0
|
|
1853
|
-
) : sourceFile?.entries || {};
|
|
1854
|
-
return [
|
|
1855
|
-
fileName,
|
|
1856
|
-
{
|
|
1857
|
-
...targetFile,
|
|
1858
|
-
entries: _7.merge({}, targetFile?.entries || {}, entries)
|
|
1859
|
-
}
|
|
1860
|
-
];
|
|
1861
|
-
}).fromPairs().value();
|
|
1862
|
-
const dictionary = {
|
|
1863
|
-
version: sourceDictionary.version,
|
|
1864
|
-
locale: sourceDictionary.locale,
|
|
1865
|
-
files
|
|
1866
|
-
};
|
|
1867
|
-
return dictionary;
|
|
1868
|
-
}
|
|
1869
|
-
static _countDictionaryEntries(dict) {
|
|
1870
|
-
return Object.values(dict.files).reduce(
|
|
1871
|
-
(sum, file) => sum + Object.keys(file.entries).length,
|
|
1872
|
-
0
|
|
1873
|
-
);
|
|
1874
|
-
}
|
|
1875
|
-
};
|
|
1876
|
-
|
|
1877
|
-
// src/utils/invokations.ts
|
|
1878
|
-
import * as t11 from "@babel/types";
|
|
1879
|
-
import traverse6 from "@babel/traverse";
|
|
1880
|
-
function findInvokations(ast, params) {
|
|
1881
|
-
const result = [];
|
|
1882
|
-
traverse6(ast, {
|
|
1883
|
-
ImportDeclaration(path7) {
|
|
1884
|
-
if (!params.moduleName.includes(path7.node.source.value)) return;
|
|
1885
|
-
const importNames = /* @__PURE__ */ new Map();
|
|
1886
|
-
const specifiers = path7.node.specifiers;
|
|
1887
|
-
specifiers.forEach((specifier) => {
|
|
1888
|
-
if (t11.isImportSpecifier(specifier) && t11.isIdentifier(specifier.imported) && specifier.imported.name === params.functionName) {
|
|
1889
|
-
importNames.set(specifier.local.name, true);
|
|
1890
|
-
} else if (t11.isImportDefaultSpecifier(specifier) && params.functionName === "default") {
|
|
1891
|
-
importNames.set(specifier.local.name, true);
|
|
1892
|
-
} else if (t11.isImportNamespaceSpecifier(specifier)) {
|
|
1893
|
-
importNames.set(specifier.local.name, "namespace");
|
|
1894
|
-
}
|
|
1895
|
-
});
|
|
1896
|
-
collectCallExpressions(path7, importNames, result, params.functionName);
|
|
1897
|
-
}
|
|
1898
|
-
});
|
|
1899
|
-
return result;
|
|
1900
|
-
}
|
|
1901
|
-
function collectCallExpressions(path7, importNames, result, functionName) {
|
|
1902
|
-
const program = path7.findParent(
|
|
1903
|
-
(p) => p.isProgram()
|
|
1904
|
-
);
|
|
1905
|
-
if (!program) return;
|
|
1906
|
-
program.traverse({
|
|
1907
|
-
CallExpression(callPath) {
|
|
1908
|
-
const callee = callPath.node.callee;
|
|
1909
|
-
if (t11.isIdentifier(callee) && importNames.has(callee.name)) {
|
|
1910
|
-
result.push(callPath.node);
|
|
1911
|
-
} else if (t11.isMemberExpression(callee) && t11.isIdentifier(callee.object) && importNames.get(callee.object.name) === "namespace" && t11.isIdentifier(callee.property) && callee.property.name === functionName) {
|
|
1912
|
-
result.push(callPath.node);
|
|
1913
|
-
}
|
|
1914
|
-
}
|
|
1915
|
-
});
|
|
1916
|
-
}
|
|
1917
|
-
|
|
1918
|
-
// src/rsc-dictionary-loader.ts
|
|
1919
|
-
import * as t13 from "@babel/types";
|
|
1920
|
-
|
|
1921
|
-
// src/_utils.ts
|
|
1922
|
-
import path5 from "path";
|
|
1923
|
-
var getDictionaryPath = (params) => {
|
|
1924
|
-
const absolute = path5.resolve(
|
|
1925
|
-
params.sourceRoot,
|
|
1926
|
-
params.lingoDir,
|
|
1927
|
-
LCP_DICTIONARY_FILE_NAME
|
|
1928
|
-
);
|
|
1929
|
-
const rel = path5.relative(params.relativeFilePath, absolute);
|
|
1930
|
-
return rel.split(path5.sep).join(path5.posix.sep);
|
|
1931
|
-
};
|
|
1932
|
-
|
|
1933
|
-
// src/utils/create-locale-import-map.ts
|
|
1934
|
-
import * as t12 from "@babel/types";
|
|
1935
|
-
function createLocaleImportMap(allLocales, dictionaryPath) {
|
|
1936
|
-
return t12.objectExpression(
|
|
1937
|
-
allLocales.map(
|
|
1938
|
-
(locale) => t12.objectProperty(
|
|
1939
|
-
t12.stringLiteral(locale),
|
|
1940
|
-
t12.arrowFunctionExpression(
|
|
1941
|
-
[],
|
|
1942
|
-
t12.callExpression(t12.identifier("import"), [
|
|
1943
|
-
t12.stringLiteral(`${dictionaryPath}?locale=${locale}`)
|
|
1944
|
-
])
|
|
1945
|
-
)
|
|
1946
|
-
)
|
|
1947
|
-
)
|
|
1948
|
-
);
|
|
1949
|
-
}
|
|
1950
|
-
|
|
1951
|
-
// src/rsc-dictionary-loader.ts
|
|
1952
|
-
var rscDictionaryLoaderMutation = createCodeMutation((payload) => {
|
|
1953
|
-
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
|
|
1954
|
-
if (mode === "client") {
|
|
1955
|
-
return payload;
|
|
1956
|
-
}
|
|
1957
|
-
const invokations = findInvokations(payload.ast, {
|
|
1958
|
-
moduleName: ModuleId.ReactRSC,
|
|
1959
|
-
functionName: "loadDictionary"
|
|
1960
|
-
});
|
|
1961
|
-
const allLocales = Array.from(
|
|
1962
|
-
/* @__PURE__ */ new Set([payload.params.sourceLocale, ...payload.params.targetLocales])
|
|
1963
|
-
);
|
|
1964
|
-
for (const invokation of invokations) {
|
|
1965
|
-
const internalDictionaryLoader = getOrCreateImport(payload.ast, {
|
|
1966
|
-
moduleName: ModuleId.ReactRSC,
|
|
1967
|
-
exportedName: "loadDictionary_internal"
|
|
1968
|
-
});
|
|
1969
|
-
if (t13.isIdentifier(invokation.callee)) {
|
|
1970
|
-
invokation.callee.name = internalDictionaryLoader.importedName;
|
|
1971
|
-
}
|
|
1972
|
-
const dictionaryPath = getDictionaryPath({
|
|
1973
|
-
sourceRoot: payload.params.sourceRoot,
|
|
1974
|
-
lingoDir: payload.params.lingoDir,
|
|
1975
|
-
relativeFilePath: payload.relativeFilePath
|
|
1976
|
-
});
|
|
1977
|
-
const localeImportMap = createLocaleImportMap(allLocales, dictionaryPath);
|
|
1978
|
-
invokation.arguments.push(localeImportMap);
|
|
1979
|
-
}
|
|
1980
|
-
return payload;
|
|
1981
|
-
});
|
|
1982
|
-
|
|
1983
|
-
// src/react-router-dictionary-loader.ts
|
|
1984
|
-
import * as t14 from "@babel/types";
|
|
1985
|
-
var reactRouterDictionaryLoaderMutation = createCodeMutation(
|
|
1986
|
-
(payload) => {
|
|
1987
|
-
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
|
|
1988
|
-
if (mode === "server") {
|
|
1989
|
-
return payload;
|
|
1990
|
-
}
|
|
1991
|
-
const invokations = findInvokations(payload.ast, {
|
|
1992
|
-
moduleName: ModuleId.ReactRouter,
|
|
1993
|
-
functionName: "loadDictionary"
|
|
1994
|
-
});
|
|
1995
|
-
const allLocales = Array.from(
|
|
1996
|
-
/* @__PURE__ */ new Set([payload.params.sourceLocale, ...payload.params.targetLocales])
|
|
1997
|
-
);
|
|
1998
|
-
for (const invokation of invokations) {
|
|
1999
|
-
const internalDictionaryLoader = getOrCreateImport(payload.ast, {
|
|
2000
|
-
moduleName: ModuleId.ReactRouter,
|
|
2001
|
-
exportedName: "loadDictionary_internal"
|
|
2002
|
-
});
|
|
2003
|
-
if (t14.isIdentifier(invokation.callee)) {
|
|
2004
|
-
invokation.callee.name = internalDictionaryLoader.importedName;
|
|
2005
|
-
}
|
|
2006
|
-
const dictionaryPath = getDictionaryPath({
|
|
2007
|
-
sourceRoot: payload.params.sourceRoot,
|
|
2008
|
-
lingoDir: payload.params.lingoDir,
|
|
2009
|
-
relativeFilePath: payload.relativeFilePath
|
|
2010
|
-
});
|
|
2011
|
-
const localeImportMap = createLocaleImportMap(allLocales, dictionaryPath);
|
|
2012
|
-
invokation.arguments.push(localeImportMap);
|
|
2013
|
-
}
|
|
2014
|
-
return payload;
|
|
2015
|
-
}
|
|
2016
|
-
);
|
|
2017
|
-
|
|
2018
|
-
// src/jsx-fragment.ts
|
|
2019
|
-
import traverse7 from "@babel/traverse";
|
|
2020
|
-
import * as t15 from "@babel/types";
|
|
2021
|
-
function jsxFragmentMutation(payload) {
|
|
2022
|
-
const { ast } = payload;
|
|
2023
|
-
let foundFragments = false;
|
|
2024
|
-
let fragmentImportName = null;
|
|
2025
|
-
traverse7(ast, {
|
|
2026
|
-
ImportDeclaration(path7) {
|
|
2027
|
-
if (path7.node.source.value !== "react") return;
|
|
2028
|
-
for (const specifier of path7.node.specifiers) {
|
|
2029
|
-
if (t15.isImportSpecifier(specifier) && t15.isIdentifier(specifier.imported) && specifier.imported.name === "Fragment") {
|
|
2030
|
-
fragmentImportName = specifier.local.name;
|
|
2031
|
-
path7.stop();
|
|
2032
|
-
}
|
|
2033
|
-
}
|
|
2034
|
-
}
|
|
2035
|
-
});
|
|
2036
|
-
traverse7(ast, {
|
|
2037
|
-
JSXFragment(path7) {
|
|
2038
|
-
foundFragments = true;
|
|
2039
|
-
if (!fragmentImportName) {
|
|
2040
|
-
const result = getOrCreateImport(ast, {
|
|
2041
|
-
exportedName: "Fragment",
|
|
2042
|
-
moduleName: ["react"]
|
|
2043
|
-
});
|
|
2044
|
-
fragmentImportName = result.importedName;
|
|
2045
|
-
}
|
|
2046
|
-
const fragmentElement = t15.jsxElement(
|
|
2047
|
-
t15.jsxOpeningElement(t15.jsxIdentifier(fragmentImportName), [], false),
|
|
2048
|
-
t15.jsxClosingElement(t15.jsxIdentifier(fragmentImportName)),
|
|
2049
|
-
path7.node.children,
|
|
2050
|
-
false
|
|
2051
|
-
);
|
|
2052
|
-
path7.replaceWith(fragmentElement);
|
|
2053
|
-
}
|
|
2054
|
-
});
|
|
2055
|
-
return payload;
|
|
2056
|
-
}
|
|
2057
|
-
|
|
2058
|
-
// src/jsx-html-lang.ts
|
|
2059
|
-
import traverse8 from "@babel/traverse";
|
|
2060
|
-
import * as t16 from "@babel/types";
|
|
2061
|
-
var jsxHtmlLangMutation = createCodeMutation((payload) => {
|
|
2062
|
-
traverse8(payload.ast, {
|
|
2063
|
-
JSXElement: (path7) => {
|
|
2064
|
-
if (getJsxElementName(path7)?.toLowerCase() === "html") {
|
|
2065
|
-
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
|
|
2066
|
-
const packagePath = mode === "client" ? ModuleId.ReactClient : ModuleId.ReactRSC;
|
|
2067
|
-
const lingoHtmlComponentImport = getOrCreateImport(payload.ast, {
|
|
2068
|
-
moduleName: packagePath,
|
|
2069
|
-
exportedName: "LingoHtmlComponent"
|
|
2070
|
-
});
|
|
2071
|
-
path7.node.openingElement.name = t16.jsxIdentifier(
|
|
2072
|
-
lingoHtmlComponentImport.importedName
|
|
2073
|
-
);
|
|
2074
|
-
if (path7.node.closingElement) {
|
|
2075
|
-
path7.node.closingElement.name = t16.jsxIdentifier(
|
|
2076
|
-
lingoHtmlComponentImport.importedName
|
|
2077
|
-
);
|
|
2078
|
-
}
|
|
2079
|
-
path7.skip();
|
|
2080
|
-
}
|
|
2081
|
-
}
|
|
2082
|
-
});
|
|
2083
|
-
return payload;
|
|
2084
|
-
});
|
|
2085
|
-
|
|
2086
|
-
// src/utils/hash.ts
|
|
2087
|
-
import { MD5 } from "object-hash";
|
|
2088
|
-
function getJsxElementHash(nodePath) {
|
|
2089
|
-
if (!nodePath.node) {
|
|
2090
|
-
return "";
|
|
2091
|
-
}
|
|
2092
|
-
const content = nodePath.node.children.map((child) => child.value).join("");
|
|
2093
|
-
const result = MD5(content);
|
|
2094
|
-
return result;
|
|
2095
|
-
}
|
|
2096
|
-
function getJsxAttributeValueHash(attributeValue) {
|
|
2097
|
-
if (!attributeValue) {
|
|
2098
|
-
return "";
|
|
2099
|
-
}
|
|
2100
|
-
const result = MD5(attributeValue);
|
|
2101
|
-
return result;
|
|
2102
|
-
}
|
|
2103
|
-
|
|
2104
|
-
// src/jsx-attribute-scopes-export.ts
|
|
2105
|
-
import _8 from "lodash";
|
|
2106
|
-
function jsxAttributeScopesExportMutation(payload) {
|
|
2107
|
-
const attributeScopes = collectJsxAttributeScopes(payload.ast);
|
|
2108
|
-
if (_8.isEmpty(attributeScopes)) {
|
|
2109
|
-
return payload;
|
|
2110
|
-
}
|
|
2111
|
-
const lcp = LCP.getInstance({
|
|
2112
|
-
sourceRoot: payload.params.sourceRoot,
|
|
2113
|
-
lingoDir: payload.params.lingoDir
|
|
2114
|
-
});
|
|
2115
|
-
for (const [scope, attributes] of attributeScopes) {
|
|
2116
|
-
for (const attributeDefinition of attributes) {
|
|
2117
|
-
const [attribute, scopeKey] = attributeDefinition.split(":");
|
|
2118
|
-
lcp.resetScope(payload.relativeFilePath, scopeKey);
|
|
2119
|
-
const attributeValue = getJsxAttributeValue(scope, attribute);
|
|
2120
|
-
if (!attributeValue) {
|
|
2121
|
-
continue;
|
|
2122
|
-
}
|
|
2123
|
-
lcp.setScopeType(payload.relativeFilePath, scopeKey, "attribute");
|
|
2124
|
-
const hash = getJsxAttributeValueHash(String(attributeValue));
|
|
2125
|
-
lcp.setScopeHash(payload.relativeFilePath, scopeKey, hash);
|
|
2126
|
-
lcp.setScopeContext(payload.relativeFilePath, scopeKey, "");
|
|
2127
|
-
lcp.setScopeSkip(payload.relativeFilePath, scopeKey, false);
|
|
2128
|
-
lcp.setScopeOverrides(payload.relativeFilePath, scopeKey, {});
|
|
2129
|
-
lcp.setScopeContent(
|
|
2130
|
-
payload.relativeFilePath,
|
|
2131
|
-
scopeKey,
|
|
2132
|
-
String(attributeValue)
|
|
2133
|
-
);
|
|
2134
|
-
}
|
|
2135
|
-
}
|
|
2136
|
-
lcp.save();
|
|
2137
|
-
return payload;
|
|
2138
|
-
}
|
|
2139
|
-
|
|
2140
|
-
// src/jsx-scopes-export.ts
|
|
2141
|
-
import _10 from "lodash";
|
|
2142
|
-
|
|
2143
|
-
// src/utils/jsx-content.ts
|
|
2144
|
-
import * as t17 from "@babel/types";
|
|
2145
|
-
import _9 from "lodash";
|
|
2146
|
-
var WHITESPACE_PLACEHOLDER = "[lingo-whitespace-placeholder]";
|
|
2147
|
-
function extractJsxContent(nodePath, replaceWhitespacePlaceholders = true) {
|
|
2148
|
-
const chunks = [];
|
|
2149
|
-
nodePath.traverse({
|
|
2150
|
-
JSXElement(path7) {
|
|
2151
|
-
if (path7.parent === nodePath.node) {
|
|
2152
|
-
const content = extractJsxContent(path7, false);
|
|
2153
|
-
const name = getJsxElementName(path7);
|
|
2154
|
-
chunks.push(`<element:${name}>${content}</element:${name}>`);
|
|
2155
|
-
path7.skip();
|
|
2156
|
-
}
|
|
2157
|
-
},
|
|
2158
|
-
JSXText(path7) {
|
|
2159
|
-
chunks.push(path7.node.value);
|
|
2160
|
-
},
|
|
2161
|
-
JSXExpressionContainer(path7) {
|
|
2162
|
-
if (path7.parent !== nodePath.node) {
|
|
2163
|
-
return;
|
|
2164
|
-
}
|
|
2165
|
-
const expr = path7.node.expression;
|
|
2166
|
-
if (t17.isCallExpression(expr)) {
|
|
2167
|
-
let key = "";
|
|
2168
|
-
if (t17.isIdentifier(expr.callee)) {
|
|
2169
|
-
key = `${expr.callee.name}`;
|
|
2170
|
-
} else if (t17.isMemberExpression(expr.callee)) {
|
|
2171
|
-
let firstCallee = expr.callee;
|
|
2172
|
-
while (t17.isMemberExpression(firstCallee) && t17.isCallExpression(firstCallee.object)) {
|
|
2173
|
-
firstCallee = firstCallee.object.callee;
|
|
2174
|
-
}
|
|
2175
|
-
let current = firstCallee;
|
|
2176
|
-
const parts = [];
|
|
2177
|
-
while (t17.isMemberExpression(current)) {
|
|
2178
|
-
if (t17.isIdentifier(current.property)) {
|
|
2179
|
-
parts.unshift(current.property.name);
|
|
2180
|
-
}
|
|
2181
|
-
current = current.object;
|
|
2182
|
-
}
|
|
2183
|
-
if (t17.isIdentifier(current)) {
|
|
2184
|
-
parts.unshift(current.name);
|
|
2185
|
-
}
|
|
2186
|
-
if (t17.isMemberExpression(firstCallee) && t17.isNewExpression(firstCallee.object) && t17.isIdentifier(firstCallee.object.callee)) {
|
|
2187
|
-
parts.unshift(firstCallee.object.callee.name);
|
|
2188
|
-
}
|
|
2189
|
-
key = parts.join(".");
|
|
2190
|
-
}
|
|
2191
|
-
chunks.push(`<function:${key}/>`);
|
|
2192
|
-
} else if (t17.isIdentifier(expr)) {
|
|
2193
|
-
chunks.push(`{${expr.name}}`);
|
|
2194
|
-
} else if (t17.isMemberExpression(expr)) {
|
|
2195
|
-
let current = expr;
|
|
2196
|
-
const parts = [];
|
|
2197
|
-
while (t17.isMemberExpression(current)) {
|
|
2198
|
-
if (t17.isIdentifier(current.property)) {
|
|
2199
|
-
if (current.computed) {
|
|
2200
|
-
parts.unshift(`[${current.property.name}]`);
|
|
2201
|
-
} else {
|
|
2202
|
-
parts.unshift(current.property.name);
|
|
2203
|
-
}
|
|
2204
|
-
}
|
|
2205
|
-
current = current.object;
|
|
2206
|
-
}
|
|
2207
|
-
if (t17.isIdentifier(current)) {
|
|
2208
|
-
parts.unshift(current.name);
|
|
2209
|
-
chunks.push(`{${parts.join(".").replaceAll(".[", "[")}}`);
|
|
2210
|
-
}
|
|
2211
|
-
} else if (isWhitespace(path7)) {
|
|
2212
|
-
chunks.push(WHITESPACE_PLACEHOLDER);
|
|
2213
|
-
} else if (isExpression2(path7)) {
|
|
2214
|
-
chunks.push("<expression/>");
|
|
2215
|
-
}
|
|
2216
|
-
path7.skip();
|
|
2217
|
-
}
|
|
2218
|
-
});
|
|
2219
|
-
const result = chunks.join("");
|
|
2220
|
-
const normalized = normalizeJsxWhitespace(result);
|
|
2221
|
-
if (replaceWhitespacePlaceholders) {
|
|
2222
|
-
return normalized.replaceAll(WHITESPACE_PLACEHOLDER, " ");
|
|
2223
|
-
}
|
|
2224
|
-
return normalized;
|
|
2225
|
-
}
|
|
2226
|
-
var compilerProps = ["data-jsx-attribute-scope", "data-jsx-scope"];
|
|
2227
|
-
function isExpression2(nodePath) {
|
|
2228
|
-
const isCompilerExpression = !_9.isArray(nodePath.container) && t17.isJSXAttribute(nodePath.container) && t17.isJSXIdentifier(nodePath.container.name) && compilerProps.includes(nodePath.container.name.name);
|
|
2229
|
-
return !isCompilerExpression && !t17.isJSXEmptyExpression(nodePath.node.expression);
|
|
2230
|
-
}
|
|
2231
|
-
function isWhitespace(nodePath) {
|
|
2232
|
-
const expr = nodePath.node.expression;
|
|
2233
|
-
return t17.isStringLiteral(expr) && expr.value === " ";
|
|
2234
|
-
}
|
|
2235
|
-
function normalizeJsxWhitespace(input) {
|
|
2236
|
-
const lines = input.split("\n");
|
|
2237
|
-
let result = "";
|
|
2238
|
-
for (let i = 0; i < lines.length; i++) {
|
|
2239
|
-
const line = lines[i].trim();
|
|
2240
|
-
if (line === "") continue;
|
|
2241
|
-
if (i > 0 && (line.startsWith("<element:") || line.startsWith("<function:") || line.startsWith("{") || line.startsWith("<expression/>"))) {
|
|
2242
|
-
result += line;
|
|
2243
|
-
} else {
|
|
2244
|
-
if (result && !result.endsWith(" ")) result += " ";
|
|
2245
|
-
result += line;
|
|
2246
|
-
}
|
|
2247
|
-
}
|
|
2248
|
-
result = result.replace(/\s+/g, " ");
|
|
2249
|
-
return result.trim();
|
|
2250
|
-
}
|
|
2251
|
-
|
|
2252
|
-
// src/jsx-scopes-export.ts
|
|
2253
|
-
function jsxScopesExportMutation(payload) {
|
|
2254
|
-
const scopes = collectJsxScopes(payload.ast);
|
|
2255
|
-
if (_10.isEmpty(scopes)) {
|
|
2256
|
-
return payload;
|
|
2257
|
-
}
|
|
2258
|
-
const lcp = LCP.getInstance({
|
|
2259
|
-
sourceRoot: payload.params.sourceRoot,
|
|
2260
|
-
lingoDir: payload.params.lingoDir
|
|
2261
|
-
});
|
|
2262
|
-
for (const scope of scopes) {
|
|
2263
|
-
const scopeKey = getAstKey(scope);
|
|
2264
|
-
lcp.resetScope(payload.relativeFilePath, scopeKey);
|
|
2265
|
-
lcp.setScopeType(payload.relativeFilePath, scopeKey, "element");
|
|
2266
|
-
const hash = getJsxElementHash(scope);
|
|
2267
|
-
lcp.setScopeHash(payload.relativeFilePath, scopeKey, hash);
|
|
2268
|
-
const context = getJsxAttributeValue(scope, "data-lingo-context");
|
|
2269
|
-
lcp.setScopeContext(
|
|
2270
|
-
payload.relativeFilePath,
|
|
2271
|
-
scopeKey,
|
|
2272
|
-
String(context || "")
|
|
2273
|
-
);
|
|
2274
|
-
const skip = getJsxAttributeValue(scope, "data-lingo-skip");
|
|
2275
|
-
lcp.setScopeSkip(
|
|
2276
|
-
payload.relativeFilePath,
|
|
2277
|
-
scopeKey,
|
|
2278
|
-
Boolean(skip || false)
|
|
2279
|
-
);
|
|
2280
|
-
const attributesMap = getJsxAttributesMap(scope);
|
|
2281
|
-
const overrides = _10.chain(attributesMap).entries().filter(
|
|
2282
|
-
([attributeKey]) => attributeKey.startsWith("data-lingo-override-")
|
|
2283
|
-
).map(([k, v]) => [k.split("data-lingo-override-")[1], v]).filter(([k]) => !!k).filter(([, v]) => !!v).fromPairs().value();
|
|
2284
|
-
lcp.setScopeOverrides(payload.relativeFilePath, scopeKey, overrides);
|
|
2285
|
-
const content = extractJsxContent(scope);
|
|
2286
|
-
lcp.setScopeContent(payload.relativeFilePath, scopeKey, content);
|
|
2287
|
-
}
|
|
2288
|
-
lcp.save();
|
|
2289
|
-
return payload;
|
|
2290
|
-
}
|
|
2291
|
-
|
|
2292
|
-
// src/jsx-attribute-scope-inject.ts
|
|
2293
|
-
import * as t18 from "@babel/types";
|
|
2294
|
-
var lingoJsxAttributeScopeInjectMutation = createCodeMutation(
|
|
2295
|
-
(payload) => {
|
|
2296
|
-
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
|
|
2297
|
-
const jsxAttributeScopes = collectJsxAttributeScopes(payload.ast);
|
|
2298
|
-
for (const [jsxScope, attributes] of jsxAttributeScopes) {
|
|
2299
|
-
const packagePath = mode === "client" ? ModuleId.ReactClient : ModuleId.ReactRSC;
|
|
2300
|
-
const lingoComponentImport = getOrCreateImport(payload.ast, {
|
|
2301
|
-
moduleName: packagePath,
|
|
2302
|
-
exportedName: "LingoAttributeComponent"
|
|
2303
|
-
});
|
|
2304
|
-
const originalJsxElementName = getJsxElementName(jsxScope);
|
|
2305
|
-
if (!originalJsxElementName) {
|
|
2306
|
-
continue;
|
|
2307
|
-
}
|
|
2308
|
-
jsxScope.node.openingElement.name = t18.jsxIdentifier(
|
|
2309
|
-
lingoComponentImport.importedName
|
|
2310
|
-
);
|
|
2311
|
-
if (jsxScope.node.closingElement) {
|
|
2312
|
-
jsxScope.node.closingElement.name = t18.jsxIdentifier(
|
|
2313
|
-
lingoComponentImport.importedName
|
|
2314
|
-
);
|
|
2315
|
-
}
|
|
2316
|
-
const as = /^[A-Z]/.test(originalJsxElementName) ? t18.jsxExpressionContainer(t18.identifier(originalJsxElementName)) : t18.stringLiteral(originalJsxElementName);
|
|
2317
|
-
jsxScope.node.openingElement.attributes.push(
|
|
2318
|
-
t18.jsxAttribute(t18.jsxIdentifier("$attrAs"), as)
|
|
2319
|
-
);
|
|
2320
|
-
jsxScope.node.openingElement.attributes.push(
|
|
2321
|
-
t18.jsxAttribute(
|
|
2322
|
-
t18.jsxIdentifier("$fileKey"),
|
|
2323
|
-
t18.stringLiteral(payload.relativeFilePath)
|
|
2324
|
-
)
|
|
2325
|
-
);
|
|
2326
|
-
jsxScope.node.openingElement.attributes.push(
|
|
2327
|
-
t18.jsxAttribute(
|
|
2328
|
-
t18.jsxIdentifier("$attributes"),
|
|
2329
|
-
t18.jsxExpressionContainer(
|
|
2330
|
-
t18.objectExpression(
|
|
2331
|
-
attributes.map((attributeDefinition) => {
|
|
2332
|
-
const [attribute, key = ""] = attributeDefinition.split(":");
|
|
2333
|
-
return t18.objectProperty(
|
|
2334
|
-
t18.stringLiteral(attribute),
|
|
2335
|
-
t18.stringLiteral(key)
|
|
2336
|
-
);
|
|
2337
|
-
})
|
|
2338
|
-
)
|
|
2339
|
-
)
|
|
2340
|
-
)
|
|
2341
|
-
);
|
|
2342
|
-
if (mode === "server") {
|
|
2343
|
-
const loadDictionaryImport = getOrCreateImport(payload.ast, {
|
|
2344
|
-
exportedName: "loadDictionary",
|
|
2345
|
-
moduleName: ModuleId.ReactRSC
|
|
2346
|
-
});
|
|
2347
|
-
jsxScope.node.openingElement.attributes.push(
|
|
2348
|
-
t18.jsxAttribute(
|
|
2349
|
-
t18.jsxIdentifier("$loadDictionary"),
|
|
2350
|
-
t18.jsxExpressionContainer(
|
|
2351
|
-
t18.arrowFunctionExpression(
|
|
2352
|
-
[t18.identifier("locale")],
|
|
2353
|
-
t18.callExpression(
|
|
2354
|
-
t18.identifier(loadDictionaryImport.importedName),
|
|
2355
|
-
[t18.identifier("locale")]
|
|
2356
|
-
)
|
|
2357
|
-
)
|
|
2358
|
-
)
|
|
2359
|
-
)
|
|
2360
|
-
);
|
|
2361
|
-
}
|
|
2362
|
-
}
|
|
2363
|
-
return payload;
|
|
2364
|
-
}
|
|
2365
|
-
);
|
|
2366
|
-
|
|
2367
|
-
// src/jsx-scope-inject.ts
|
|
2368
|
-
import * as t22 from "@babel/types";
|
|
2369
|
-
|
|
2370
|
-
// src/utils/jsx-variables.ts
|
|
2371
|
-
import * as t19 from "@babel/types";
|
|
2372
|
-
var getJsxVariables = (nodePath) => {
|
|
2373
|
-
const variables = /* @__PURE__ */ new Set();
|
|
2374
|
-
nodePath.traverse({
|
|
2375
|
-
JSXOpeningElement(path7) {
|
|
2376
|
-
path7.skip();
|
|
2377
|
-
},
|
|
2378
|
-
JSXExpressionContainer(path7) {
|
|
2379
|
-
if (t19.isIdentifier(path7.node.expression)) {
|
|
2380
|
-
variables.add(path7.node.expression.name);
|
|
2381
|
-
} else if (t19.isMemberExpression(path7.node.expression)) {
|
|
2382
|
-
let current = path7.node.expression;
|
|
2383
|
-
const parts = [];
|
|
2384
|
-
while (t19.isMemberExpression(current)) {
|
|
2385
|
-
if (t19.isIdentifier(current.property)) {
|
|
2386
|
-
if (current.computed) {
|
|
2387
|
-
parts.unshift(`[${current.property.name}]`);
|
|
2388
|
-
} else {
|
|
2389
|
-
parts.unshift(current.property.name);
|
|
2390
|
-
}
|
|
2391
|
-
}
|
|
2392
|
-
current = current.object;
|
|
2393
|
-
}
|
|
2394
|
-
if (t19.isIdentifier(current)) {
|
|
2395
|
-
parts.unshift(current.name);
|
|
2396
|
-
variables.add(parts.join(".").replaceAll(".[", "["));
|
|
2397
|
-
}
|
|
2398
|
-
}
|
|
2399
|
-
path7.skip();
|
|
2400
|
-
}
|
|
2401
|
-
});
|
|
2402
|
-
const properties = Array.from(variables).map(
|
|
2403
|
-
(name) => t19.objectProperty(t19.stringLiteral(name), t19.identifier(name))
|
|
2404
|
-
);
|
|
2405
|
-
const result = t19.objectExpression(properties);
|
|
2406
|
-
return result;
|
|
2407
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
LCPCache,
|
|
3
|
+
LCP_DICTIONARY_FILE_NAME,
|
|
4
|
+
__require,
|
|
5
|
+
defaultParams,
|
|
6
|
+
getGoogleKeyFromEnv,
|
|
7
|
+
getGoogleKeyFromRc,
|
|
8
|
+
getGroqKeyFromEnv,
|
|
9
|
+
getGroqKeyFromRc,
|
|
10
|
+
getInvalidLocales,
|
|
11
|
+
getLingoDotDevKeyFromEnv,
|
|
12
|
+
getLingoDotDevKeyFromRc,
|
|
13
|
+
isRunningInCIOrDocker,
|
|
14
|
+
loadDictionary,
|
|
15
|
+
providerDetails,
|
|
16
|
+
transformComponent
|
|
17
|
+
} from "./chunk-YT6JFNHK.mjs";
|
|
2408
18
|
|
|
2409
|
-
// src/
|
|
2410
|
-
import
|
|
2411
|
-
var getJsxFunctions = (nodePath) => {
|
|
2412
|
-
const functions = /* @__PURE__ */ new Map();
|
|
2413
|
-
let fnCounter = 0;
|
|
2414
|
-
nodePath.traverse({
|
|
2415
|
-
JSXOpeningElement(path7) {
|
|
2416
|
-
path7.skip();
|
|
2417
|
-
},
|
|
2418
|
-
JSXExpressionContainer(path7) {
|
|
2419
|
-
if (t20.isCallExpression(path7.node.expression)) {
|
|
2420
|
-
let key = "";
|
|
2421
|
-
if (t20.isIdentifier(path7.node.expression.callee)) {
|
|
2422
|
-
key = `${path7.node.expression.callee.name}`;
|
|
2423
|
-
} else if (t20.isMemberExpression(path7.node.expression.callee)) {
|
|
2424
|
-
let firstCallee = path7.node.expression.callee;
|
|
2425
|
-
while (t20.isMemberExpression(firstCallee) && t20.isCallExpression(firstCallee.object)) {
|
|
2426
|
-
firstCallee = firstCallee.object.callee;
|
|
2427
|
-
}
|
|
2428
|
-
let current = firstCallee;
|
|
2429
|
-
const parts = [];
|
|
2430
|
-
while (t20.isMemberExpression(current)) {
|
|
2431
|
-
if (t20.isIdentifier(current.property)) {
|
|
2432
|
-
parts.unshift(current.property.name);
|
|
2433
|
-
}
|
|
2434
|
-
current = current.object;
|
|
2435
|
-
}
|
|
2436
|
-
if (t20.isIdentifier(current)) {
|
|
2437
|
-
parts.unshift(current.name);
|
|
2438
|
-
}
|
|
2439
|
-
if (t20.isMemberExpression(firstCallee) && t20.isNewExpression(firstCallee.object) && t20.isIdentifier(firstCallee.object.callee)) {
|
|
2440
|
-
parts.unshift(firstCallee.object.callee.name);
|
|
2441
|
-
}
|
|
2442
|
-
key = parts.join(".");
|
|
2443
|
-
}
|
|
2444
|
-
const existing = functions.get(key) ?? [];
|
|
2445
|
-
functions.set(key, [...existing, path7.node.expression]);
|
|
2446
|
-
fnCounter++;
|
|
2447
|
-
}
|
|
2448
|
-
path7.skip();
|
|
2449
|
-
}
|
|
2450
|
-
});
|
|
2451
|
-
const properties = Array.from(functions.entries()).map(
|
|
2452
|
-
([name, callExpr]) => t20.objectProperty(t20.stringLiteral(name), t20.arrayExpression(callExpr))
|
|
2453
|
-
);
|
|
2454
|
-
return t20.objectExpression(properties);
|
|
2455
|
-
};
|
|
19
|
+
// src/index.ts
|
|
20
|
+
import { createUnplugin } from "unplugin";
|
|
2456
21
|
|
|
2457
|
-
//
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
22
|
+
// package.json
|
|
23
|
+
var package_default = {
|
|
24
|
+
name: "@lingo.dev/_compiler",
|
|
25
|
+
version: "0.4.0",
|
|
26
|
+
description: "Lingo.dev Compiler",
|
|
27
|
+
private: false,
|
|
28
|
+
publishConfig: {
|
|
29
|
+
access: "public"
|
|
30
|
+
},
|
|
31
|
+
sideEffects: false,
|
|
32
|
+
type: "module",
|
|
33
|
+
main: "build/index.cjs",
|
|
34
|
+
types: "build/index.d.ts",
|
|
35
|
+
module: "build/index.mjs",
|
|
36
|
+
files: [
|
|
37
|
+
"build"
|
|
38
|
+
],
|
|
39
|
+
scripts: {
|
|
40
|
+
dev: "tsup --watch",
|
|
41
|
+
build: "tsc --noEmit && tsup",
|
|
42
|
+
clean: "rm -rf build",
|
|
43
|
+
test: "vitest --run",
|
|
44
|
+
"test:watch": "vitest -w"
|
|
45
|
+
},
|
|
46
|
+
keywords: [],
|
|
47
|
+
author: "",
|
|
48
|
+
license: "ISC",
|
|
49
|
+
devDependencies: {
|
|
50
|
+
"@types/babel__generator": "^7.6.8",
|
|
51
|
+
"@types/babel__traverse": "^7.20.6",
|
|
52
|
+
"@types/ini": "^4.1.1",
|
|
53
|
+
"@types/lodash": "^4.17.4",
|
|
54
|
+
"@types/object-hash": "^3.0.6",
|
|
55
|
+
"@types/react": "^18.3.18",
|
|
56
|
+
next: "15.2.4",
|
|
57
|
+
tsup: "^8.3.5",
|
|
58
|
+
typescript: "^5.4.5"
|
|
59
|
+
},
|
|
60
|
+
dependencies: {
|
|
61
|
+
"@ai-sdk/google": "^1.2.19",
|
|
62
|
+
"@ai-sdk/groq": "^1.2.3",
|
|
63
|
+
"@babel/generator": "^7.26.5",
|
|
64
|
+
"@babel/parser": "^7.26.7",
|
|
65
|
+
"@babel/traverse": "^7.27.4",
|
|
66
|
+
"@babel/types": "^7.26.7",
|
|
67
|
+
"@lingo.dev/_sdk": "workspace:*",
|
|
68
|
+
"@openrouter/ai-sdk-provider": "^0.7.1",
|
|
69
|
+
ai: "^4.2.10",
|
|
70
|
+
dedent: "^1.6.0",
|
|
71
|
+
dotenv: "^16.4.5",
|
|
72
|
+
"fast-xml-parser": "^5.0.8",
|
|
73
|
+
ini: "^5.0.0",
|
|
74
|
+
lodash: "^4.17.21",
|
|
75
|
+
"object-hash": "^3.0.0",
|
|
76
|
+
"ollama-ai-provider": "^1.2.0",
|
|
77
|
+
prettier: "^3.4.2",
|
|
78
|
+
unplugin: "^2.1.2",
|
|
79
|
+
vitest: "^2.1.4",
|
|
80
|
+
zod: "^3.24.1"
|
|
81
|
+
},
|
|
82
|
+
packageManager: "pnpm@9.12.3"
|
|
2474
83
|
};
|
|
2475
84
|
|
|
2476
|
-
// src/jsx-scope-inject.ts
|
|
2477
|
-
var lingoJsxScopeInjectMutation = createCodeMutation((payload) => {
|
|
2478
|
-
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
|
|
2479
|
-
const jsxScopes = collectJsxScopes(payload.ast);
|
|
2480
|
-
for (const jsxScope of jsxScopes) {
|
|
2481
|
-
const skip = getJsxAttributeValue(jsxScope, "data-lingo-skip");
|
|
2482
|
-
if (skip) {
|
|
2483
|
-
continue;
|
|
2484
|
-
}
|
|
2485
|
-
const packagePath = mode === "client" ? ModuleId.ReactClient : ModuleId.ReactRSC;
|
|
2486
|
-
const lingoComponentImport = getOrCreateImport(payload.ast, {
|
|
2487
|
-
moduleName: packagePath,
|
|
2488
|
-
exportedName: "LingoComponent"
|
|
2489
|
-
});
|
|
2490
|
-
const originalJsxElementName = getJsxElementName(jsxScope);
|
|
2491
|
-
if (!originalJsxElementName) {
|
|
2492
|
-
continue;
|
|
2493
|
-
}
|
|
2494
|
-
const originalAttributes = jsxScope.node.openingElement.attributes.slice();
|
|
2495
|
-
const as = /^[A-Z]/.test(originalJsxElementName) ? t22.jsxExpressionContainer(t22.identifier(originalJsxElementName)) : t22.stringLiteral(originalJsxElementName);
|
|
2496
|
-
originalAttributes.push(t22.jsxAttribute(t22.jsxIdentifier("$as"), as));
|
|
2497
|
-
originalAttributes.push(
|
|
2498
|
-
t22.jsxAttribute(
|
|
2499
|
-
t22.jsxIdentifier("$fileKey"),
|
|
2500
|
-
t22.stringLiteral(payload.relativeFilePath)
|
|
2501
|
-
)
|
|
2502
|
-
);
|
|
2503
|
-
originalAttributes.push(
|
|
2504
|
-
t22.jsxAttribute(
|
|
2505
|
-
t22.jsxIdentifier("$entryKey"),
|
|
2506
|
-
t22.stringLiteral(getJsxScopeAttribute(jsxScope))
|
|
2507
|
-
)
|
|
2508
|
-
);
|
|
2509
|
-
const $variables = getJsxVariables(jsxScope);
|
|
2510
|
-
if ($variables.properties.length > 0) {
|
|
2511
|
-
originalAttributes.push(
|
|
2512
|
-
t22.jsxAttribute(
|
|
2513
|
-
t22.jsxIdentifier("$variables"),
|
|
2514
|
-
t22.jsxExpressionContainer($variables)
|
|
2515
|
-
)
|
|
2516
|
-
);
|
|
2517
|
-
}
|
|
2518
|
-
const $elements = getNestedJsxElements(jsxScope);
|
|
2519
|
-
if ($elements.elements.length > 0) {
|
|
2520
|
-
originalAttributes.push(
|
|
2521
|
-
t22.jsxAttribute(
|
|
2522
|
-
t22.jsxIdentifier("$elements"),
|
|
2523
|
-
t22.jsxExpressionContainer($elements)
|
|
2524
|
-
)
|
|
2525
|
-
);
|
|
2526
|
-
}
|
|
2527
|
-
const $functions = getJsxFunctions(jsxScope);
|
|
2528
|
-
if ($functions.properties.length > 0) {
|
|
2529
|
-
originalAttributes.push(
|
|
2530
|
-
t22.jsxAttribute(
|
|
2531
|
-
t22.jsxIdentifier("$functions"),
|
|
2532
|
-
t22.jsxExpressionContainer($functions)
|
|
2533
|
-
)
|
|
2534
|
-
);
|
|
2535
|
-
}
|
|
2536
|
-
const $expressions = getJsxExpressions(jsxScope);
|
|
2537
|
-
if ($expressions.elements.length > 0) {
|
|
2538
|
-
originalAttributes.push(
|
|
2539
|
-
t22.jsxAttribute(
|
|
2540
|
-
t22.jsxIdentifier("$expressions"),
|
|
2541
|
-
t22.jsxExpressionContainer($expressions)
|
|
2542
|
-
)
|
|
2543
|
-
);
|
|
2544
|
-
}
|
|
2545
|
-
if (mode === "server") {
|
|
2546
|
-
const loadDictionaryImport = getOrCreateImport(payload.ast, {
|
|
2547
|
-
exportedName: "loadDictionary",
|
|
2548
|
-
moduleName: ModuleId.ReactRSC
|
|
2549
|
-
});
|
|
2550
|
-
originalAttributes.push(
|
|
2551
|
-
t22.jsxAttribute(
|
|
2552
|
-
t22.jsxIdentifier("$loadDictionary"),
|
|
2553
|
-
t22.jsxExpressionContainer(
|
|
2554
|
-
t22.arrowFunctionExpression(
|
|
2555
|
-
[t22.identifier("locale")],
|
|
2556
|
-
t22.callExpression(
|
|
2557
|
-
t22.identifier(loadDictionaryImport.importedName),
|
|
2558
|
-
[t22.identifier("locale")]
|
|
2559
|
-
)
|
|
2560
|
-
)
|
|
2561
|
-
)
|
|
2562
|
-
)
|
|
2563
|
-
);
|
|
2564
|
-
}
|
|
2565
|
-
const newNode = t22.jsxElement(
|
|
2566
|
-
t22.jsxOpeningElement(
|
|
2567
|
-
t22.jsxIdentifier(lingoComponentImport.importedName),
|
|
2568
|
-
originalAttributes,
|
|
2569
|
-
true
|
|
2570
|
-
// selfClosing
|
|
2571
|
-
),
|
|
2572
|
-
null,
|
|
2573
|
-
// no closing element
|
|
2574
|
-
[],
|
|
2575
|
-
// no children
|
|
2576
|
-
true
|
|
2577
|
-
// selfClosing
|
|
2578
|
-
);
|
|
2579
|
-
jsxScope.replaceWith(newNode);
|
|
2580
|
-
}
|
|
2581
|
-
return payload;
|
|
2582
|
-
});
|
|
2583
|
-
|
|
2584
|
-
// src/jsx-remove-attributes.ts
|
|
2585
|
-
import * as t23 from "@babel/types";
|
|
2586
|
-
import traverse9 from "@babel/traverse";
|
|
2587
|
-
var jsxRemoveAttributesMutation = createCodeMutation(
|
|
2588
|
-
(payload) => {
|
|
2589
|
-
const ATTRIBUTES_TO_REMOVE = [
|
|
2590
|
-
"data-jsx-root",
|
|
2591
|
-
"data-jsx-scope",
|
|
2592
|
-
"data-jsx-attribute-scope"
|
|
2593
|
-
];
|
|
2594
|
-
traverse9(payload.ast, {
|
|
2595
|
-
JSXElement(path7) {
|
|
2596
|
-
const openingElement = path7.node.openingElement;
|
|
2597
|
-
openingElement.attributes = openingElement.attributes.filter((attr) => {
|
|
2598
|
-
const removeAttr = t23.isJSXAttribute(attr) && t23.isJSXIdentifier(attr.name) && ATTRIBUTES_TO_REMOVE.includes(attr.name.name);
|
|
2599
|
-
return !removeAttr;
|
|
2600
|
-
});
|
|
2601
|
-
}
|
|
2602
|
-
});
|
|
2603
|
-
return {
|
|
2604
|
-
...payload
|
|
2605
|
-
};
|
|
2606
|
-
}
|
|
2607
|
-
);
|
|
2608
|
-
|
|
2609
|
-
// src/client-dictionary-loader.ts
|
|
2610
|
-
import * as t24 from "@babel/types";
|
|
2611
|
-
var clientDictionaryLoaderMutation = createCodeMutation((payload) => {
|
|
2612
|
-
const invokations = findInvokations(payload.ast, {
|
|
2613
|
-
moduleName: ModuleId.ReactClient,
|
|
2614
|
-
functionName: "loadDictionary"
|
|
2615
|
-
});
|
|
2616
|
-
const allLocales = Array.from(
|
|
2617
|
-
/* @__PURE__ */ new Set([payload.params.sourceLocale, ...payload.params.targetLocales])
|
|
2618
|
-
);
|
|
2619
|
-
for (const invokation of invokations) {
|
|
2620
|
-
const internalDictionaryLoader = getOrCreateImport(payload.ast, {
|
|
2621
|
-
moduleName: ModuleId.ReactClient,
|
|
2622
|
-
exportedName: "loadDictionary_internal"
|
|
2623
|
-
});
|
|
2624
|
-
if (t24.isIdentifier(invokation.callee)) {
|
|
2625
|
-
invokation.callee.name = internalDictionaryLoader.importedName;
|
|
2626
|
-
}
|
|
2627
|
-
const dictionaryPath = getDictionaryPath({
|
|
2628
|
-
sourceRoot: payload.params.sourceRoot,
|
|
2629
|
-
lingoDir: payload.params.lingoDir,
|
|
2630
|
-
relativeFilePath: payload.relativeFilePath
|
|
2631
|
-
});
|
|
2632
|
-
const localeImportMap = createLocaleImportMap(allLocales, dictionaryPath);
|
|
2633
|
-
invokation.arguments.push(localeImportMap);
|
|
2634
|
-
}
|
|
2635
|
-
return payload;
|
|
2636
|
-
});
|
|
2637
|
-
|
|
2638
85
|
// src/index.ts
|
|
86
|
+
import _ from "lodash";
|
|
87
|
+
import dedent from "dedent";
|
|
2639
88
|
var keyCheckers = {
|
|
2640
89
|
groq: {
|
|
2641
90
|
checkEnv: getGroqKeyFromEnv,
|
|
@@ -2653,7 +102,7 @@ var keyCheckers = {
|
|
|
2653
102
|
var unplugin = createUnplugin(
|
|
2654
103
|
(_params, _meta) => {
|
|
2655
104
|
console.log("\u2139\uFE0F Starting Lingo.dev compiler...");
|
|
2656
|
-
const params =
|
|
105
|
+
const params = _.defaults(_params, defaultParams);
|
|
2657
106
|
if (!isRunningInCIOrDocker()) {
|
|
2658
107
|
if (params.models === "lingo.dev") {
|
|
2659
108
|
validateLLMKeyDetails(["lingo.dev"]);
|
|
@@ -2666,15 +115,15 @@ var unplugin = createUnplugin(
|
|
|
2666
115
|
params.targetLocales
|
|
2667
116
|
);
|
|
2668
117
|
if (invalidLocales.length > 0) {
|
|
2669
|
-
console.log(
|
|
118
|
+
console.log(dedent`
|
|
2670
119
|
\n
|
|
2671
120
|
⚠️ Lingo.dev Localization Compiler requires LLM model setup for the following locales: ${invalidLocales.join(", ")}.
|
|
2672
|
-
|
|
121
|
+
|
|
2673
122
|
⭐️ Next steps:
|
|
2674
123
|
1. Refer to documentation for help: https://lingo.dev/compiler
|
|
2675
124
|
2. If you want to use a different LLM, raise an issue in our open-source repo: https://lingo.dev/go/gh
|
|
2676
125
|
3. If you have questions, feature requests, or would like to contribute, join our Discord: https://lingo.dev/go/discord
|
|
2677
|
-
|
|
126
|
+
|
|
2678
127
|
✨
|
|
2679
128
|
`);
|
|
2680
129
|
process.exit(1);
|
|
@@ -2690,23 +139,22 @@ var unplugin = createUnplugin(
|
|
|
2690
139
|
name: package_default.name,
|
|
2691
140
|
loadInclude: (id) => !!id.match(LCP_DICTIONARY_FILE_NAME),
|
|
2692
141
|
async load(id) {
|
|
2693
|
-
const
|
|
2694
|
-
|
|
142
|
+
const dictionary = await loadDictionary({
|
|
143
|
+
resourcePath: id,
|
|
144
|
+
resourceQuery: "",
|
|
145
|
+
params: {
|
|
146
|
+
...params,
|
|
147
|
+
models: params.models,
|
|
148
|
+
sourceLocale: params.sourceLocale,
|
|
149
|
+
targetLocales: params.targetLocales
|
|
150
|
+
},
|
|
2695
151
|
sourceRoot: params.sourceRoot,
|
|
2696
152
|
lingoDir: params.lingoDir,
|
|
2697
153
|
isDev
|
|
2698
|
-
};
|
|
2699
|
-
await LCP.ready(lcpParams);
|
|
2700
|
-
const lcp = LCP.getInstance(lcpParams);
|
|
2701
|
-
const dictionaries = await LCPServer.loadDictionaries({
|
|
2702
|
-
models: params.models,
|
|
2703
|
-
lcp: lcp.data,
|
|
2704
|
-
sourceLocale: params.sourceLocale,
|
|
2705
|
-
targetLocales: params.targetLocales,
|
|
2706
|
-
sourceRoot: params.sourceRoot,
|
|
2707
|
-
lingoDir: params.lingoDir
|
|
2708
154
|
});
|
|
2709
|
-
|
|
155
|
+
if (!dictionary) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
2710
158
|
console.log(JSON.stringify(dictionary, null, 2));
|
|
2711
159
|
return {
|
|
2712
160
|
code: `export default ${JSON.stringify(dictionary, null, 2)}`
|
|
@@ -2716,36 +164,12 @@ var unplugin = createUnplugin(
|
|
|
2716
164
|
enforce: "pre",
|
|
2717
165
|
transform(code, id) {
|
|
2718
166
|
try {
|
|
2719
|
-
const result =
|
|
167
|
+
const result = transformComponent({
|
|
2720
168
|
code,
|
|
2721
169
|
params,
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
})
|
|
2725
|
-
composeMutations(
|
|
2726
|
-
i18n_directive_default,
|
|
2727
|
-
jsxFragmentMutation,
|
|
2728
|
-
jsx_attribute_flag_default,
|
|
2729
|
-
// log here to see transformedfiles
|
|
2730
|
-
// (input) => {
|
|
2731
|
-
// console.log(`transform ${id}`);
|
|
2732
|
-
// return input;
|
|
2733
|
-
// },
|
|
2734
|
-
jsx_provider_default,
|
|
2735
|
-
jsxHtmlLangMutation,
|
|
2736
|
-
jsx_root_flag_default,
|
|
2737
|
-
jsx_scope_flag_default,
|
|
2738
|
-
jsx_attribute_flag_default,
|
|
2739
|
-
jsxAttributeScopesExportMutation,
|
|
2740
|
-
jsxScopesExportMutation,
|
|
2741
|
-
lingoJsxAttributeScopeInjectMutation,
|
|
2742
|
-
lingoJsxScopeInjectMutation,
|
|
2743
|
-
rscDictionaryLoaderMutation,
|
|
2744
|
-
reactRouterDictionaryLoaderMutation,
|
|
2745
|
-
jsxRemoveAttributesMutation,
|
|
2746
|
-
clientDictionaryLoaderMutation
|
|
2747
|
-
)
|
|
2748
|
-
).thru(createOutput).value();
|
|
170
|
+
resourcePath: id,
|
|
171
|
+
sourceRoot: params.sourceRoot
|
|
172
|
+
});
|
|
2749
173
|
return result;
|
|
2750
174
|
} catch (error) {
|
|
2751
175
|
console.error("\u26A0\uFE0F Lingo.dev compiler failed to localize your app");
|
|
@@ -2757,27 +181,79 @@ var unplugin = createUnplugin(
|
|
|
2757
181
|
}
|
|
2758
182
|
);
|
|
2759
183
|
var src_default = {
|
|
2760
|
-
next: (compilerParams) => (nextConfig) =>
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
184
|
+
next: (compilerParams) => (nextConfig = {}) => {
|
|
185
|
+
const mergedParams = _.merge(
|
|
186
|
+
{},
|
|
187
|
+
defaultParams,
|
|
188
|
+
{
|
|
189
|
+
rsc: true,
|
|
190
|
+
turbopack: {
|
|
191
|
+
enabled: "auto",
|
|
192
|
+
useLegacyTurbo: false
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
compilerParams
|
|
196
|
+
);
|
|
197
|
+
let turbopackEnabled;
|
|
198
|
+
if (mergedParams.turbopack?.enabled === "auto") {
|
|
199
|
+
turbopackEnabled = process.env.TURBOPACK === "1" || process.env.TURBOPACK === "true";
|
|
200
|
+
} else {
|
|
201
|
+
turbopackEnabled = mergedParams.turbopack?.enabled === true;
|
|
202
|
+
}
|
|
203
|
+
const supportLegacyTurbo = mergedParams.turbopack?.useLegacyTurbo === true;
|
|
204
|
+
const hasWebpackConfig = typeof nextConfig.webpack === "function";
|
|
205
|
+
const hasTurbopackConfig = typeof nextConfig.turbopack === "function";
|
|
206
|
+
if (hasWebpackConfig && turbopackEnabled) {
|
|
207
|
+
console.warn(
|
|
208
|
+
"\u26A0\uFE0F Turbopack is enabled in the Lingo.dev compiler, but you have webpack config. Lingo.dev will still apply turbopack configuration."
|
|
2768
209
|
);
|
|
2769
|
-
return config2;
|
|
2770
210
|
}
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
211
|
+
if (hasTurbopackConfig && !turbopackEnabled) {
|
|
212
|
+
console.warn(
|
|
213
|
+
"\u26A0\uFE0F Turbopack is disabled in the Lingo.dev compiler, but you have turbopack config. Lingo.dev will not apply turbopack configuration."
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
const originalWebpack = nextConfig.webpack;
|
|
217
|
+
nextConfig.webpack = (config, options) => {
|
|
218
|
+
if (!turbopackEnabled) {
|
|
219
|
+
console.log("Applying Lingo.dev webpack configuration...");
|
|
220
|
+
config.plugins.unshift(unplugin.webpack(mergedParams));
|
|
221
|
+
}
|
|
222
|
+
if (typeof originalWebpack === "function") {
|
|
223
|
+
return originalWebpack(config, options);
|
|
224
|
+
}
|
|
225
|
+
return config;
|
|
226
|
+
};
|
|
227
|
+
if (turbopackEnabled) {
|
|
228
|
+
console.log("Applying Lingo.dev Turbopack configuration...");
|
|
229
|
+
let turbopackConfigPath = nextConfig.turbopack ??= {};
|
|
230
|
+
if (supportLegacyTurbo) {
|
|
231
|
+
turbopackConfigPath = (nextConfig.experimental ??= {}).turbo ??= {};
|
|
232
|
+
}
|
|
233
|
+
turbopackConfigPath.rules ??= {};
|
|
234
|
+
const rules = turbopackConfigPath.rules;
|
|
235
|
+
const lingoGlob = `**/*.{ts,tsx,js,jsx}`;
|
|
236
|
+
const lingoLoaderPath = __require.resolve("./lingo-turbopack-loader");
|
|
237
|
+
rules[lingoGlob] = {
|
|
238
|
+
loaders: [
|
|
239
|
+
{
|
|
240
|
+
loader: lingoLoaderPath,
|
|
241
|
+
options: mergedParams
|
|
242
|
+
}
|
|
243
|
+
]
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
return nextConfig;
|
|
247
|
+
},
|
|
248
|
+
vite: (compilerParams) => (config) => {
|
|
249
|
+
config.plugins.unshift(
|
|
250
|
+
unplugin.vite(_.merge({}, defaultParams, { rsc: false }, compilerParams))
|
|
2775
251
|
);
|
|
2776
|
-
return
|
|
252
|
+
return config;
|
|
2777
253
|
}
|
|
2778
254
|
};
|
|
2779
255
|
function getConfiguredProviders(models) {
|
|
2780
|
-
return
|
|
256
|
+
return _.chain(Object.values(models)).map((modelString) => modelString.split(":")[0]).filter(Boolean).uniq().filter(
|
|
2781
257
|
(providerId) => providerDetails.hasOwnProperty(providerId) && keyCheckers.hasOwnProperty(providerId)
|
|
2782
258
|
).value();
|
|
2783
259
|
}
|
|
@@ -2802,7 +278,7 @@ function validateLLMKeyDetails(configuredProviders) {
|
|
|
2802
278
|
}
|
|
2803
279
|
}
|
|
2804
280
|
if (missingProviders.length > 0) {
|
|
2805
|
-
console.log(
|
|
281
|
+
console.log(dedent`
|
|
2806
282
|
\n
|
|
2807
283
|
💡 Lingo.dev Localization Compiler is configured to use the following LLM provider(s): ${configuredProviders.join(", ")}.
|
|
2808
284
|
|
|
@@ -2811,7 +287,7 @@ function validateLLMKeyDetails(configuredProviders) {
|
|
|
2811
287
|
for (const providerId of missingProviders) {
|
|
2812
288
|
const status = keyStatuses[providerId];
|
|
2813
289
|
if (!status) continue;
|
|
2814
|
-
console.log(
|
|
290
|
+
console.log(dedent`
|
|
2815
291
|
⚠️ ${status.details.name} API key is missing. Set ${status.details.apiKeyEnvVar} environment variable.
|
|
2816
292
|
|
|
2817
293
|
👉 You can set the API key in one of the following ways:
|
|
@@ -2822,7 +298,7 @@ function validateLLMKeyDetails(configuredProviders) {
|
|
|
2822
298
|
⭐️ If you don't yet have a ${status.details.name} API key, get one for free at ${status.details.getKeyLink}
|
|
2823
299
|
`);
|
|
2824
300
|
}
|
|
2825
|
-
console.log(
|
|
301
|
+
console.log(dedent`
|
|
2826
302
|
\n
|
|
2827
303
|
⭐️ Also:
|
|
2828
304
|
1. If you want to use a different LLM, update your configuration. Refer to documentation for help: https://lingo.dev/compiler
|
|
@@ -2833,7 +309,7 @@ function validateLLMKeyDetails(configuredProviders) {
|
|
|
2833
309
|
`);
|
|
2834
310
|
process.exit(1);
|
|
2835
311
|
} else if (foundProviders.length > 0) {
|
|
2836
|
-
console.log(
|
|
312
|
+
console.log(dedent`
|
|
2837
313
|
\n
|
|
2838
314
|
🔑 LLM API keys detected for configured providers: ${foundProviders.join(", ")}.
|
|
2839
315
|
`);
|
|
@@ -2848,7 +324,7 @@ function validateLLMKeyDetails(configuredProviders) {
|
|
|
2848
324
|
} else if (status.foundInRc) {
|
|
2849
325
|
sourceMessage = `from your user-wide configuration${status.details.apiKeyConfigKey ? ` (${status.details.apiKeyConfigKey})` : ""}.`;
|
|
2850
326
|
}
|
|
2851
|
-
console.log(
|
|
327
|
+
console.log(dedent`
|
|
2852
328
|
• ${status.details.name} API key loaded ${sourceMessage}
|
|
2853
329
|
`);
|
|
2854
330
|
}
|