fes-locale-gen
Advanced tools
Comparing version 1.0.7 to 1.0.8
{ | ||
"name": "fes-locale-gen", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "", | ||
@@ -19,2 +19,3 @@ "main": "index.js", | ||
"@babel/traverse": "^7.25.7", | ||
"@vue/compiler-sfc": "^3.5.12", | ||
"dotenv": "^16.4.5", | ||
@@ -21,0 +22,0 @@ "glob": "^11.0.0", |
@@ -5,3 +5,3 @@ #!/usr/bin/env node | ||
Object.keys(require.cache).forEach(function (key) { delete require.cache[key] }); | ||
// const { parse: parseVue } = require("@vue/compiler-sfc") | ||
const fs = require('fs'); | ||
@@ -285,3 +285,7 @@ const { parse } = require('vue-eslint-parser'); | ||
} | ||
// TODO: 处理template中变量 | ||
// const vueParseResult = parseVue(processedContent, { | ||
// sourceType: 'module', | ||
// }); | ||
// console.log('VUE:', vueParseResult.descriptor.template.ast.children[0].props); | ||
// 处理 script 和 script setup | ||
@@ -478,3 +482,57 @@ const scriptMatch = processedContent.match(/<script(\s+setup)?[^>]*>([\s\S]*?)<\/script>/i); | ||
} | ||
let missingKeys, extraKeys; | ||
let attempts = 0; | ||
const maxAttempts = 5; | ||
do { | ||
attempts++; | ||
console.log(`Attempt ${attempts} to resolve key mismatches...`); | ||
const zhKeys = Object.keys(zhJson); | ||
const enKeys = Object.keys(translatedContent); | ||
// 比对原始中文 JSON 和翻译后的英文 JSON | ||
missingKeys = zhKeys.filter(key => !enKeys.includes(key)); | ||
extraKeys = enKeys.filter(key => !zhKeys.includes(key)); | ||
if (missingKeys.length > 0) { | ||
console.log('Translating missing keys:', missingKeys); | ||
const missingChunk = {}; | ||
missingKeys.forEach(key => { | ||
missingChunk[key] = zhJson[key]; | ||
}); | ||
const translatedMissingChunk = await translateChunk(missingChunk, openai); | ||
if (translatedMissingChunk) { | ||
const parsedMissingChunk = JSON.parse(translatedMissingChunk); | ||
translatedContent = { ...translatedContent, ...parsedMissingChunk }; | ||
} else { | ||
console.error('Failed to translate missing keys'); | ||
break; | ||
} | ||
} | ||
if (extraKeys.length > 0) { | ||
console.log('Removing extra keys:', extraKeys); | ||
extraKeys.forEach(key => { | ||
delete translatedContent[key]; | ||
}); | ||
} | ||
if (attempts >= maxAttempts) { | ||
console.error(`Reached maximum attempts (${maxAttempts}) to resolve key mismatches.`); | ||
break; | ||
} | ||
} while (missingKeys.length > 0 || extraKeys.length > 0); | ||
if (missingKeys.length > 0 || extraKeys.length > 0) { | ||
console.error('Translation mismatch detected after multiple attempts:'); | ||
if (missingKeys.length > 0) { | ||
console.error('Missing keys in translated content:', missingKeys); | ||
} | ||
if (extraKeys.length > 0) { | ||
console.error('Extra keys in translated content:', extraKeys); | ||
} | ||
console.error('Please review and correct the translation manually.'); | ||
} else { | ||
console.log('Translation completed successfully. All keys match.'); | ||
} | ||
const outputContent = `/* eslint-disable prettier/prettier */\nexport default ${JSON.stringify(translatedContent, null, 2)};\n`; | ||
@@ -549,13 +607,36 @@ fs.writeFileSync(enFilePath, outputContent); | ||
} | ||
// 移除json关键字 | ||
function removeJsonKeyword(inputString) { | ||
// Check if the input string starts with "```json" and ends with "```" | ||
if (inputString.startsWith("```json") && inputString.endsWith("```")) { | ||
// Remove the "```json" from the start and "```" from the end | ||
return inputString.slice(7, -3).trim(); | ||
} | ||
return inputString; | ||
} | ||
// 翻译函数 | ||
async function translateChunk(chunk, openai) { | ||
const prompt = `Translate the following JSON object values from Chinese to English. Keep the keys unchanged:\n${JSON.stringify(chunk)}`; | ||
// const prompt = `Translate the following JSON object values from Chinese to English. Keep the keys unchanged:\n${JSON.stringify(chunk)}`; | ||
const prompt = `{ | ||
Request: "Translate the following JSON object values from Chinese to English. Keep the keys unchanged", | ||
Restriction: "Only return the JSON object without any additional replies", | ||
Format: { | ||
"key": "value", | ||
... | ||
}, | ||
Original: ${JSON.stringify(chunk)}, | ||
Response: { | ||
// your response here | ||
"key": "value" | ||
... | ||
} | ||
}` | ||
try { | ||
const response = await openai.chat.completions.create({ | ||
model: "gpt-4o", | ||
model: "qwen-72b", | ||
messages: [{ role: "user", content: prompt }], | ||
response_format: { type: "json_object" } | ||
// response_format: { type: "json_object" } | ||
}); | ||
const translatedText = response.choices[0].message.content; | ||
const translatedText = removeJsonKeyword(response.choices[0].message.content); | ||
return translatedText; | ||
@@ -562,0 +643,0 @@ } catch (error) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48357
610
9
+ Added@vue/compiler-sfc@^3.5.12
+ Added@types/node@18.19.64(transitive)
+ Added@vue/compiler-core@3.5.13(transitive)
+ Added@vue/compiler-dom@3.5.13(transitive)
+ Added@vue/compiler-sfc@3.5.13(transitive)
+ Added@vue/compiler-ssr@3.5.13(transitive)
+ Added@vue/shared@3.5.13(transitive)
+ Addedentities@4.5.0(transitive)
+ Addedestree-walker@2.0.2(transitive)
+ Addedmagic-string@0.30.13(transitive)
+ Addednanoid@3.3.7(transitive)
+ Addedopenai@4.73.0(transitive)
+ Addedpostcss@8.4.49(transitive)
+ Addedsource-map-js@1.2.1(transitive)
- Removed@types/node@18.19.66(transitive)
- Removedopenai@4.73.1(transitive)