Update public-api.js via upload script - 2026-10-19 13:42:29
This commit is contained in:
parent
ef558e5997
commit
e2edfb8d7e
|
|
@ -44,6 +44,26 @@ const FIELDS = {
|
||||||
youtube: 'youtubeUrl', // YouTube 链接,输出时只保留视频 ID
|
youtube: 'youtubeUrl', // YouTube 链接,输出时只保留视频 ID
|
||||||
description: 'webDescription', // { en, es?, ru? }:只需填 en,其他语言自动翻译;手动填了的语言优先用手动的
|
description: 'webDescription', // { en, es?, ru? }:只需填 en,其他语言自动翻译;手动填了的语言优先用手动的
|
||||||
sale: 'webSale', // { mode: 'group'|'stock', stock, minPrice, maxPrice }
|
sale: 'webSale', // { mode: 'group'|'stock', stock, minPrice, maxPrice }
|
||||||
|
// —— 离线式 / 在线互动式 UPS 的参数(迷你UPS 一般不填,空的不输出)——
|
||||||
|
acInputVoltage: 'inputVoltage', // 离线式UPS 的输入电压(迷你UPS 用上面的 inputVoltage ← voltage)
|
||||||
|
inputVoltageRange: 'inputVoltageRange',
|
||||||
|
inputFrequency: 'inputFrequency',
|
||||||
|
outputVoltage: 'outputVoltage',
|
||||||
|
outputVoltageRange: 'outputVoltageRange',
|
||||||
|
transferTime: 'transferTime',
|
||||||
|
waveform: 'waveform',
|
||||||
|
frequencyRangeBatteryMode: 'frequencyRangeBatteryMode',
|
||||||
|
batteryVoltage: 'batteryVoltage',
|
||||||
|
chargeCurrent: 'chargeCurrent',
|
||||||
|
chargeTime: 'chargeTime',
|
||||||
|
lcdDisplay: 'lcdDisplay',
|
||||||
|
ledDisplay: 'ledDisplay',
|
||||||
|
fullProtection: 'fullProtection',
|
||||||
|
alarmInfo: 'alarmInfo',
|
||||||
|
interfacePort: 'interfacePort',
|
||||||
|
humidity: 'humidity',
|
||||||
|
noiseLevel: 'noiseLevel',
|
||||||
|
sockets: 'webSockets', // 网站展示的默认插座配置 [{ type: 'SCHUKO'|'NEMA'|'UK'|'UNIVERSAL'|'IEC'|'AU', count }]
|
||||||
};
|
};
|
||||||
const PUBLISH_FLAG = 'publishToWeb'; // 新增:"发布到网站"开关
|
const PUBLISH_FLAG = 'publishToWeb'; // 新增:"发布到网站"开关
|
||||||
const IMAGES_FIELD = 'webImages'; // 新增:主图文件名数组,最多 6 张;为空时用现有的 productImageFileName
|
const IMAGES_FIELD = 'webImages'; // 新增:主图文件名数组,最多 6 张;为空时用现有的 productImageFileName
|
||||||
|
|
@ -79,10 +99,10 @@ function saveCache(c) {
|
||||||
fs.renameSync(tmp, TRANSLATION_CACHE_FILE);
|
fs.renameSync(tmp, TRANSLATION_CACHE_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function translateBatch(ai, texts, langDesc) {
|
async function translateBatch(ai, texts, langDesc, srcLang = 'English') {
|
||||||
const cfg = ai.loadAiConfig();
|
const cfg = ai.loadAiConfig();
|
||||||
if (!cfg.apiKey) throw new Error('DeepSeek API key not configured');
|
if (!cfg.apiKey) throw new Error('DeepSeek API key not configured');
|
||||||
const system = `You translate B2B product copy for a power-equipment manufacturer (UPS, mini UPS, batteries) from English into ${langDesc}.
|
const system = `You translate B2B product copy for a power-equipment manufacturer (UPS, mini UPS, batteries) from ${srcLang} into ${langDesc}.
|
||||||
Rules: keep model numbers, brand names, units and numbers exactly as written (e.g. POE-36E-LFP-48V, 24V/48V, 5000mAh, PoE, LiFePO4, USB, DC). Use natural commercial wording, not word-for-word.
|
Rules: keep model numbers, brand names, units and numbers exactly as written (e.g. POE-36E-LFP-48V, 24V/48V, 5000mAh, PoE, LiFePO4, USB, DC). Use natural commercial wording, not word-for-word.
|
||||||
Input is a JSON array of strings. Return ONLY a JSON array of the translated strings, same length and same order, no markdown, no extra text.`;
|
Input is a JSON array of strings. Return ONLY a JSON array of the translated strings, same length and same order, no markdown, no extra text.`;
|
||||||
const content = await ai.callDeepSeekWithRetry(cfg.apiKey, [
|
const content = await ai.callDeepSeekWithRetry(cfg.apiKey, [
|
||||||
|
|
@ -120,8 +140,46 @@ async function translateStrings(ai, strings) {
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 参数值里带中文的(比如"8小时恢复到90%容量"、LCD 显示说明),自动翻译成英语 / 西语 / 俄语。
|
||||||
|
// 结果放在 specI18n: { 字段: { en, es, ru } },网站按语言取用;翻译失败的字段网站上不显示(不会露出中文)。
|
||||||
|
const SPEC_TRANSLATE_FIELDS = ['inputVoltage', 'acInputVoltage', 'inputVoltageRange', 'inputFrequency', 'outputVoltage', 'outputVoltageRange', 'transferTime', 'waveform',
|
||||||
|
'frequencyRangeBatteryMode', 'batteryVoltage', 'chargeCurrent', 'chargeTime', 'lcdDisplay', 'ledDisplay', 'fullProtection', 'alarmInfo', 'interfacePort',
|
||||||
|
'humidity', 'noiseLevel', 'outputPower', 'powerFactor', 'batterySpec', 'dimensions', 'netWeight'];
|
||||||
|
const ZH_TARGETS = { en: 'English', ...WEB_TRANSLATE_LANGS };
|
||||||
|
const hasZh = (s) => /[\u4e00-\u9fff]/.test(String(s || ''));
|
||||||
|
async function translateSpecs(ai, products) {
|
||||||
|
const need = new Set();
|
||||||
|
for (const p of products) for (const f of SPEC_TRANSLATE_FIELDS) if (typeof p[f] === 'string' && hasZh(p[f])) need.add(p[f]);
|
||||||
|
if (!need.size) return;
|
||||||
|
const cache = loadCache();
|
||||||
|
let dirty = false;
|
||||||
|
if (ai) {
|
||||||
|
for (const [lang, desc] of Object.entries(ZH_TARGETS)) {
|
||||||
|
const list = [...need].filter((t) => !cache[cacheKey('zh>' + lang, t)]);
|
||||||
|
for (let i = 0; i < list.length; i += 20) {
|
||||||
|
const chunk = list.slice(i, i + 20);
|
||||||
|
try {
|
||||||
|
const out = await translateBatch(ai, chunk, desc, 'Chinese');
|
||||||
|
chunk.forEach((src, j) => { cache[cacheKey('zh>' + lang, src)] = out[j]; });
|
||||||
|
dirty = true;
|
||||||
|
} catch (e) { console.warn(`[public-api] 参数翻译成 ${lang} 失败:${e.message}`); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dirty) { try { saveCache(cache); } catch (e) {} }
|
||||||
|
for (const p of products) {
|
||||||
|
for (const f of SPEC_TRANSLATE_FIELDS) {
|
||||||
|
if (typeof p[f] !== 'string' || !hasZh(p[f])) continue;
|
||||||
|
const t = {};
|
||||||
|
for (const lang of Object.keys(ZH_TARGETS)) { const v = cache[cacheKey('zh>' + lang, p[f])]; if (v) t[lang] = v; }
|
||||||
|
if (Object.keys(t).length) (p.specI18n = p.specI18n || {})[f] = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 给一批产品补齐各语言的介绍和卖点。翻译失败不影响构建:那一项网站上显示英文。
|
// 给一批产品补齐各语言的介绍和卖点。翻译失败不影响构建:那一项网站上显示英文。
|
||||||
async function translateProducts(ai, products) {
|
async function translateProducts(ai, products) {
|
||||||
|
try { await translateSpecs(ai, products); } catch (e) { console.warn('[public-api] 参数翻译失败:', e.message); }
|
||||||
if (!ai) {
|
if (!ai) {
|
||||||
for (const p of products) { if (p._points.length) p.sellingPointsI18n = { en: p._points }; delete p._points; }
|
for (const p of products) { if (p._points.length) p.sellingPointsI18n = { en: p._points }; delete p._points; }
|
||||||
return products;
|
return products;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue