solid-translate 1.4.0 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/cli.js +141 -6
- package/dist/index.d.ts +7 -0
- package/dist/index.js +33 -15
- package/dist/index.js.map +1 -1
- package/dist/vite.d.ts +7 -0
- package/dist/vite.js +146 -8
- package/dist/vite.js.map +1 -1
- package/package.json +2 -1
package/dist/vite.js
CHANGED
|
@@ -14665,7 +14665,7 @@ async function translateBatch(model, entries, targetLocale, sourceLocale, system
|
|
|
14665
14665
|
|
|
14666
14666
|
// src/extract.ts
|
|
14667
14667
|
var import_parser = __toESM(require_lib(), 1);
|
|
14668
|
-
function extractStringsFromSource(code, filePath, warnings) {
|
|
14668
|
+
function extractStringsFromSource(code, filePath, warnings, options) {
|
|
14669
14669
|
const results = [];
|
|
14670
14670
|
const seen = /* @__PURE__ */ new Set();
|
|
14671
14671
|
const warn = (line, message) => {
|
|
@@ -14687,26 +14687,158 @@ function extractStringsFromSource(code, filePath, warnings) {
|
|
|
14687
14687
|
seen.add(entry.key);
|
|
14688
14688
|
results.push(entry);
|
|
14689
14689
|
};
|
|
14690
|
+
const bindings = collectModuleBindings(ast);
|
|
14691
|
+
const shadowStack = [];
|
|
14692
|
+
const resolveMarker = (localName) => {
|
|
14693
|
+
for (let i = shadowStack.length - 1; i >= 0; i--) {
|
|
14694
|
+
if (shadowStack[i].has(localName)) return null;
|
|
14695
|
+
}
|
|
14696
|
+
const imported = bindings.imports.get(localName);
|
|
14697
|
+
if (imported) {
|
|
14698
|
+
if (!isAcceptedImportSource(imported.source, options?.importSources)) {
|
|
14699
|
+
return null;
|
|
14700
|
+
}
|
|
14701
|
+
return MARKER_NAMES.has(imported.imported) ? imported.imported : null;
|
|
14702
|
+
}
|
|
14703
|
+
if (bindings.moduleLocals.has(localName)) return null;
|
|
14704
|
+
return MARKER_NAMES.has(localName) ? localName : null;
|
|
14705
|
+
};
|
|
14690
14706
|
const visit = (node) => {
|
|
14707
|
+
const scopeBindings = collectScopeBindings(node);
|
|
14708
|
+
if (scopeBindings) shadowStack.push(scopeBindings);
|
|
14691
14709
|
if (node.type === "JSXElement") {
|
|
14692
14710
|
const name = jsxName(node);
|
|
14693
|
-
|
|
14711
|
+
const marker = name ? resolveMarker(name) : null;
|
|
14712
|
+
if (marker === "T") {
|
|
14694
14713
|
const entry = processT(node, filePath, warn);
|
|
14695
14714
|
if (entry) push(entry);
|
|
14696
|
-
} else if (
|
|
14715
|
+
} else if (marker === "Plural") {
|
|
14697
14716
|
for (const entry of processPlural(node, filePath, warn)) {
|
|
14698
14717
|
push(entry);
|
|
14699
14718
|
}
|
|
14700
14719
|
}
|
|
14701
|
-
} else if (node.type === "CallExpression" && node.callee?.type === "Identifier" && node.callee.name === "msg") {
|
|
14720
|
+
} else if (node.type === "CallExpression" && node.callee?.type === "Identifier" && resolveMarker(node.callee.name) === "msg") {
|
|
14702
14721
|
const entry = processMsg(node, filePath, warn);
|
|
14703
14722
|
if (entry) push(entry);
|
|
14704
14723
|
}
|
|
14705
14724
|
walkChildren(node, visit);
|
|
14725
|
+
if (scopeBindings) shadowStack.pop();
|
|
14706
14726
|
};
|
|
14707
14727
|
visit(ast);
|
|
14708
14728
|
return results;
|
|
14709
14729
|
}
|
|
14730
|
+
var MARKER_NAMES = /* @__PURE__ */ new Set([
|
|
14731
|
+
"msg",
|
|
14732
|
+
"T",
|
|
14733
|
+
"Var",
|
|
14734
|
+
"Num",
|
|
14735
|
+
"Currency",
|
|
14736
|
+
"DateTime",
|
|
14737
|
+
"Plural"
|
|
14738
|
+
]);
|
|
14739
|
+
var DEFAULT_IMPORT_SOURCE_RE = /(^|\/)(solid-translate|i18n)(\.[cm]?[jt]sx?)?$/;
|
|
14740
|
+
function isAcceptedImportSource(source, importSources) {
|
|
14741
|
+
if (source === "solid-translate") return true;
|
|
14742
|
+
if (importSources) return importSources.includes(source);
|
|
14743
|
+
return DEFAULT_IMPORT_SOURCE_RE.test(source);
|
|
14744
|
+
}
|
|
14745
|
+
function collectModuleBindings(ast) {
|
|
14746
|
+
const imports = /* @__PURE__ */ new Map();
|
|
14747
|
+
const moduleLocals = /* @__PURE__ */ new Set();
|
|
14748
|
+
const body = ast.program?.body ?? [];
|
|
14749
|
+
for (const stmt of body) {
|
|
14750
|
+
if (stmt.type === "ImportDeclaration") {
|
|
14751
|
+
const source = String(stmt.source?.value ?? "");
|
|
14752
|
+
for (const spec of stmt.specifiers ?? []) {
|
|
14753
|
+
const local = spec.local?.name;
|
|
14754
|
+
if (typeof local !== "string") continue;
|
|
14755
|
+
if (spec.type === "ImportSpecifier") {
|
|
14756
|
+
const imported = spec.imported?.type === "Identifier" ? spec.imported.name : String(spec.imported?.value ?? "");
|
|
14757
|
+
imports.set(local, { imported, source });
|
|
14758
|
+
} else {
|
|
14759
|
+
imports.set(local, { imported: "*", source });
|
|
14760
|
+
}
|
|
14761
|
+
}
|
|
14762
|
+
} else {
|
|
14763
|
+
collectDeclaredNames(stmt, moduleLocals);
|
|
14764
|
+
}
|
|
14765
|
+
}
|
|
14766
|
+
return { imports, moduleLocals };
|
|
14767
|
+
}
|
|
14768
|
+
function collectDeclaredNames(stmt, into) {
|
|
14769
|
+
if (stmt.type === "ExportNamedDeclaration" || stmt.type === "ExportDefaultDeclaration") {
|
|
14770
|
+
if (stmt.declaration) collectDeclaredNames(stmt.declaration, into);
|
|
14771
|
+
return;
|
|
14772
|
+
}
|
|
14773
|
+
if (stmt.type === "VariableDeclaration") {
|
|
14774
|
+
for (const decl of stmt.declarations ?? []) {
|
|
14775
|
+
if (decl.id) collectPatternNames(decl.id, into);
|
|
14776
|
+
}
|
|
14777
|
+
return;
|
|
14778
|
+
}
|
|
14779
|
+
if ((stmt.type === "FunctionDeclaration" || stmt.type === "ClassDeclaration" || stmt.type === "TSEnumDeclaration") && stmt.id?.type === "Identifier") {
|
|
14780
|
+
addMarkerName(stmt.id.name, into);
|
|
14781
|
+
}
|
|
14782
|
+
}
|
|
14783
|
+
function collectPatternNames(pattern, into) {
|
|
14784
|
+
switch (pattern.type) {
|
|
14785
|
+
case "Identifier":
|
|
14786
|
+
addMarkerName(pattern.name, into);
|
|
14787
|
+
break;
|
|
14788
|
+
case "AssignmentPattern":
|
|
14789
|
+
collectPatternNames(pattern.left, into);
|
|
14790
|
+
break;
|
|
14791
|
+
case "RestElement":
|
|
14792
|
+
collectPatternNames(pattern.argument, into);
|
|
14793
|
+
break;
|
|
14794
|
+
case "ObjectPattern":
|
|
14795
|
+
for (const prop of pattern.properties ?? []) {
|
|
14796
|
+
if (prop.type === "ObjectProperty") {
|
|
14797
|
+
collectPatternNames(prop.value, into);
|
|
14798
|
+
} else if (prop.type === "RestElement") {
|
|
14799
|
+
collectPatternNames(prop.argument, into);
|
|
14800
|
+
}
|
|
14801
|
+
}
|
|
14802
|
+
break;
|
|
14803
|
+
case "ArrayPattern":
|
|
14804
|
+
for (const el of pattern.elements ?? []) {
|
|
14805
|
+
if (el) collectPatternNames(el, into);
|
|
14806
|
+
}
|
|
14807
|
+
break;
|
|
14808
|
+
}
|
|
14809
|
+
}
|
|
14810
|
+
function addMarkerName(name, into) {
|
|
14811
|
+
if (typeof name === "string" && MARKER_NAMES.has(name)) into.add(name);
|
|
14812
|
+
}
|
|
14813
|
+
var FUNCTION_TYPES = /* @__PURE__ */ new Set([
|
|
14814
|
+
"ArrowFunctionExpression",
|
|
14815
|
+
"FunctionExpression",
|
|
14816
|
+
"FunctionDeclaration",
|
|
14817
|
+
"ObjectMethod",
|
|
14818
|
+
"ClassMethod",
|
|
14819
|
+
"ClassPrivateMethod"
|
|
14820
|
+
]);
|
|
14821
|
+
function collectScopeBindings(node) {
|
|
14822
|
+
const bound = /* @__PURE__ */ new Set();
|
|
14823
|
+
if (FUNCTION_TYPES.has(node.type)) {
|
|
14824
|
+
if (node.id?.type === "Identifier") addMarkerName(node.id.name, bound);
|
|
14825
|
+
for (const param of node.params ?? []) {
|
|
14826
|
+
collectPatternNames(param, bound);
|
|
14827
|
+
}
|
|
14828
|
+
} else if (node.type === "CatchClause" && node.param) {
|
|
14829
|
+
collectPatternNames(node.param, bound);
|
|
14830
|
+
} else if (node.type === "BlockStatement") {
|
|
14831
|
+
for (const stmt of node.body ?? []) {
|
|
14832
|
+
collectDeclaredNames(stmt, bound);
|
|
14833
|
+
}
|
|
14834
|
+
} else if (node.type === "ForStatement" || node.type === "ForOfStatement" || node.type === "ForInStatement") {
|
|
14835
|
+
const init = node.init ?? node.left;
|
|
14836
|
+
if (init?.type === "VariableDeclaration") {
|
|
14837
|
+
collectDeclaredNames(init, bound);
|
|
14838
|
+
}
|
|
14839
|
+
}
|
|
14840
|
+
return bound.size > 0 ? bound : null;
|
|
14841
|
+
}
|
|
14710
14842
|
function walkChildren(node, visit) {
|
|
14711
14843
|
for (const key of Object.keys(node)) {
|
|
14712
14844
|
if (key === "loc" || key === "leadingComments" || key === "trailingComments" || key === "innerComments") {
|
|
@@ -15152,7 +15284,8 @@ function solidTranslate(config) {
|
|
|
15152
15284
|
batchSize = 50,
|
|
15153
15285
|
translate = true,
|
|
15154
15286
|
autoExtract = false,
|
|
15155
|
-
include = ["src/**/*.tsx", "src/**/*.ts", "src/**/*.jsx"]
|
|
15287
|
+
include = ["src/**/*.tsx", "src/**/*.ts", "src/**/*.jsx"],
|
|
15288
|
+
extractImportSources
|
|
15156
15289
|
} = config;
|
|
15157
15290
|
let root;
|
|
15158
15291
|
let resolvedLocalesDir;
|
|
@@ -15180,7 +15313,11 @@ function solidTranslate(config) {
|
|
|
15180
15313
|
);
|
|
15181
15314
|
let contexts = {};
|
|
15182
15315
|
if (autoExtract) {
|
|
15183
|
-
const extracted = await autoExtractStrings(
|
|
15316
|
+
const extracted = await autoExtractStrings(
|
|
15317
|
+
root,
|
|
15318
|
+
include,
|
|
15319
|
+
extractImportSources
|
|
15320
|
+
);
|
|
15184
15321
|
contexts = extracted.contexts;
|
|
15185
15322
|
let existingSource = {};
|
|
15186
15323
|
if (existsSync2(sourceFilePath)) {
|
|
@@ -15343,7 +15480,7 @@ function solidTranslate(config) {
|
|
|
15343
15480
|
};
|
|
15344
15481
|
}
|
|
15345
15482
|
var vite_default = solidTranslate;
|
|
15346
|
-
async function autoExtractStrings(root, patterns) {
|
|
15483
|
+
async function autoExtractStrings(root, patterns, importSources) {
|
|
15347
15484
|
const strings = {};
|
|
15348
15485
|
const contexts = {};
|
|
15349
15486
|
const warnings = [];
|
|
@@ -15356,7 +15493,8 @@ async function autoExtractStrings(root, patterns) {
|
|
|
15356
15493
|
const extracted = extractStringsFromSource(
|
|
15357
15494
|
code,
|
|
15358
15495
|
relative(root, file),
|
|
15359
|
-
warnings
|
|
15496
|
+
warnings,
|
|
15497
|
+
{ importSources }
|
|
15360
15498
|
);
|
|
15361
15499
|
for (const entry of extracted) {
|
|
15362
15500
|
strings[entry.key] = entry.source;
|