Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fes-locale-gen

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fes-locale-gen - npm Package Compare versions

Comparing version 1.0.7 to 1.0.8

3

package.json
{
"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) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc