| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- const fs = require("fs");
- const path = require("path");
- const SRC = path.join(__dirname, "../src");
- const OUT = path.join(__dirname, "../src/i18n/flat-en.js");
- const SKIP = /copy\.vue$|weatherInfo copy|flat-en\.js|messages\.js/i;
- const CHINESE_RE = /[\u4e00-\u9fff][\u4e00-\u9fff\d,。、?!:;""''()【】\-\s%%·]*/g;
- const messagesPath = path.join(__dirname, "../src/i18n/messages.js");
- const messages = new Function(`${fs.readFileSync(messagesPath, "utf8").replace("export default", "return ")}`)();
- const PHRASES = [
- ["长势报告", "Growth Report"],
- ["农情档案", "Crop Archives"],
- ["农事规划", "Farm Planning"],
- ["有味溯源", "Traceability"],
- ["荔枝长势报告", "Lychee Growth Report"],
- ["作物长势报告", "Crop Growth Report"],
- ["气象风险", "Weather Risks"],
- ["农事建议", "Farming Advice"],
- ["巡园重点", "Patrol Priorities"],
- ["未来7-15天气象风险", "7–15 Day Weather Risks"],
- ["未来七天天气", "7-Day Forecast"],
- ["农场列表", "Farm List"],
- ["示范农场", "Demo Farm"],
- ["专属数字农场,种好卖好", "Your Digital Farm — Grow Better, Sell Better"],
- ["点击解锁一键溯源增产", "Tap to Unlock Traceability & Yield Boost"],
- ["您的农场已绑定成功", "Farm Linked Successfully"],
- ["点击展开更多", "Show More"],
- ["点击收起", "Collapse"],
- ["暂无地址", "No Address"],
- ["干旱预警", "Drought Alert"],
- ["设为默认农场", "Set as Default"],
- ["新增农场", "Add Farm"],
- ["当前农场", "Current Farm"],
- ["农场信息维护", "Farm Profile"],
- ["查看全部农场列表,并可创建农场", "View All Farms and Create New Ones"],
- ["背景描述:", "Background: "],
- ["对策建议:", "Recommendations: "],
- ["点击解锁", "Unlock"],
- ["请求错误", "Bad Request"],
- ["未授权,请登录", "Unauthorized. Please log in"],
- ["没有权限,拒绝访问", "Forbidden"],
- ["请求地址出错", "Invalid URL"],
- ["请求超时", "Request Timeout"],
- ["服务器内部错误", "Internal Server Error"],
- ["服务未实现", "Not Implemented"],
- ["网关错误", "Bad Gateway"],
- ["服务不可用", "Service Unavailable"],
- ["网关超时", "Gateway Timeout"],
- ["HTTP版本不受支持", "HTTP Version Not Supported"],
- ["农事详情", "Farm Work Details"],
- ["农事凭证", "Farm Work Certificate"],
- ["执行区域", "Execution Area"],
- ["已认证", "Verified"],
- ["农事目的", "Purpose"],
- ["注意事项", "Notes"],
- ["药物处方", "Prescription"],
- ["执行方式", "Execution Method"],
- ["异常记录", "Exception Log"],
- ["转发给执行人员", "Forward to Executor"],
- ["确认上传", "Confirm Upload"],
- ["确认信息", "Confirm"],
- ["转发记录", "Forward Record"],
- ["物候不整齐?", "Uneven Phenology?"],
- ["发现异常,拍照记录", "Report Issue with Photo"],
- ["当前现状:", "Current Status:"],
- ["点击上传照片", "Upload Photo"],
- ["点击勾画新发生区域", "Draw New Area"],
- ["暂未到达进程", "Stage Not Reached"],
- ["邀请勾画", "Invite to Draw"],
- ["区域名称", "Area Name"],
- ["物候时间", "Phenology Period"],
- ["多选", "Multi-select"],
- ["调整pH值", "Adjust pH"],
- ["物候期时间设置", "Phenology Schedule"],
- ["起始时间", "Start Time"],
- ["请从上往下按照时间顺序填写日期", "Enter dates in chronological order from top to bottom"],
- ["确认设置", "Confirm"],
- ["上传中", "Uploading"],
- ["上传失败", "Upload Failed"],
- ["上传成功", "Upload Successful"],
- ["加载中", "Loading"],
- ["暂无数据", "No Data"],
- ["请稍候", "Please Wait"],
- ["操作成功", "Success"],
- ["操作失败", "Failed"],
- ["网络错误", "Network Error"],
- ["请求失败", "Request Failed"],
- ["登录", "Log In"],
- ["退出登录", "Log Out"],
- ["确定", "OK"],
- ["取消", "Cancel"],
- ["确认", "Confirm"],
- ["提交", "Submit"],
- ["保存", "Save"],
- ["删除", "Delete"],
- ["编辑", "Edit"],
- ["返回", "Back"],
- ["搜索", "Search"],
- ["搜索农场", "Search Farms"],
- ["选择地区", "Select Region"],
- ["选择品类", "Select Category"],
- ["请选择", "Please Select"],
- ["请输入", "Please Enter"],
- ["展开更多", "Expand"],
- ["收起", "Collapse"],
- ["详情", "Details"],
- ["查看", "View"],
- ["关闭", "Close"],
- ["下一步", "Next"],
- ["上一步", "Previous"],
- ["完成", "Done"],
- ["全部", "All"],
- ["更多", "More"],
- ["首页", "Home"],
- ["个人中心", "Profile"],
- ["农事服务", "Farm Services"],
- ["用户管理", "User Management"],
- ["历史风险报告", "Historical Risk Reports"],
- ["校准物候期", "Calibrate Phenology"],
- ["进程互动", "Progress Check"],
- ["长势互动", "Growth Check"],
- ["病虫互动", "Pest Check"],
- ["病虫风险", "Pest & Disease Risk"],
- ["阴雨寡照风险", "Rain & Low-Light Risk"],
- ["根外追肥", "Foliar Feeding"],
- ["虫害防治", "Pest Control"],
- ["种植档案管理", "Planting Records"],
- ["邀请关注", "Invite to Follow"],
- ];
- const WORDS = {
- 请: "Please ",
- 选择: "Select ",
- 输入: "Enter ",
- 暂无: "No ",
- 当前: "Current ",
- 未来: "Future ",
- 查看: "View ",
- 点击: "Tap ",
- 上传: "Upload ",
- 下载: "Download ",
- 搜索: "Search ",
- 筛选: "Filter ",
- 排序: "Sort ",
- 新增: "Add ",
- 编辑: "Edit ",
- 删除: "Delete ",
- 保存: "Save ",
- 提交: "Submit ",
- 确认: "Confirm ",
- 取消: "Cancel ",
- 返回: "Back ",
- 关闭: "Close ",
- 展开: "Expand ",
- 收起: "Collapse ",
- 成功: " Success",
- 失败: " Failed",
- 错误: " Error",
- 警告: " Warning",
- 提示: " Notice",
- 农场: "Farm",
- 果园: "Orchard",
- 地块: "Plot",
- 品种: "Variety",
- 作物: "Crop",
- 荔枝: "Lychee",
- 水稻: "Rice",
- 农事: "Farm Work",
- 天气: "Weather",
- 风险: " Risk",
- 报告: " Report",
- 档案: " Archives",
- 规划: " Planning",
- 溯源: " Traceability",
- 互动: " Interaction",
- 巡园: "Patrol ",
- 物候期: "Phenological Stage",
- 生育期: "Reproductive Stage",
- 膨果期: "Fruit Expansion Stage",
- 晴天: "Sunny",
- 周日: "Sun",
- 周一: "Mon",
- 周二: "Tue",
- 周三: "Wed",
- 周四: "Thu",
- 周五: "Fri",
- 周六: "Sat",
- };
- function pairZhEn(zhTree, enTree, out = {}) {
- Object.keys(zhTree || {}).forEach((k) => {
- if (zhTree[k] && typeof zhTree[k] === "object" && !Array.isArray(zhTree[k])) {
- pairZhEn(zhTree[k], (enTree || {})[k], out);
- } else if (typeof zhTree[k] === "string" && typeof (enTree || {})[k] === "string") {
- out[zhTree[k]] = enTree[k];
- }
- });
- return out;
- }
- function extractTemplateChinese(filePath) {
- const content = fs.readFileSync(filePath, "utf8");
- const templateMatch = content.match(/<template>([\s\S]*?)<\/template>/);
- if (!templateMatch) return [];
- return (templateMatch[1].match(CHINESE_RE) || [])
- .map((s) => s.trim().replace(/\s+/g, " "))
- .filter((s) => s.length >= 2 && s.length <= 120 && /[\u4e00-\u9fff]/.test(s));
- }
- function walk(dir, files = []) {
- for (const name of fs.readdirSync(dir)) {
- const p = path.join(dir, name);
- if (fs.statSync(p).isDirectory()) {
- if (name !== "node_modules") walk(p, files);
- } else if (name.endsWith(".vue") && !SKIP.test(p)) files.push(p);
- }
- return files;
- }
- function autoTranslate(zh, cache) {
- if (cache[zh]) return cache[zh];
- for (const [key, val] of PHRASES) {
- if (zh === key || zh.includes(key)) {
- cache[zh] = zh.split(key).join(val);
- if (cache[zh] !== zh) return cache[zh];
- }
- }
- let result = zh;
- const keys = Object.keys(WORDS).sort((a, b) => b.length - a.length);
- keys.forEach((w) => {
- result = result.split(w).join(WORDS[w]);
- });
- result = result.replace(/\s+/g, " ").trim();
- if (/[\u4e00-\u9fff]/.test(result)) {
- result = `[${result}]`;
- }
- cache[zh] = result;
- return result;
- }
- const map = pairZhEn(messages.zh, messages.en);
- const cache = { ...map };
- for (const file of walk(SRC)) {
- extractTemplateChinese(file).forEach((s) => {
- if (!map[s]) map[s] = null;
- });
- }
- Object.keys(map).forEach((zh) => {
- if (!map[zh] || /[\u4e00-\u9fff]/.test(map[zh])) {
- map[zh] = autoTranslate(zh, cache);
- }
- });
- const esc = (s) => s.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
- const lines = ["export default {"];
- Object.keys(map)
- .sort()
- .forEach((zh) => {
- lines.push(` '${esc(zh)}': '${esc(map[zh])}',`);
- });
- lines.push("};");
- fs.writeFileSync(OUT, lines.join("\n"));
- console.log("Wrote", Object.keys(map).length, "entries");
|