Skip to content

Commit

Permalink
feat: add i18n-pick tool
Browse files Browse the repository at this point in the history
  • Loading branch information
visiky committed Mar 14, 2023
1 parent f421149 commit 8b6e4fe
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 2 deletions.
3 changes: 3 additions & 0 deletions i18n.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
exclude: ['**/locales/**', '**/datas/**']
};
87 changes: 87 additions & 0 deletions i18n/tmp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"下载 PDF": "下载 PDF",
"\n 在线版本\n ": "\n 在线版本\n ",
"在线版本": "在线版本",
" 项目代码\n ": " 项目代码\n ",
" 项目代码": " 项目代码",
"从模板中获取": "从模板中获取",
"已缓存在本地": "已缓存在本地",
"复制成功": "复制成功",
"头像设置": "头像设置",
"个人信息": "个人信息",
"教育背景": "教育背景",
"自我介绍": "自我介绍",
"更多信息": "更多信息",
"个人作品": "个人作品",
"专业技能": "专业技能",
"工作经历": "工作经历",
"项目经历": "项目经历",
"隐藏头像": "隐藏头像",
"头像形状": "头像形状",
"圆形": "圆形",
"方形": "方形",
"姓名": "姓名",
"手机号码": "手机号码",
"邮箱": "邮箱",
"知乎": "知乎",
"工作经验": "工作经验",
"期望工作地": "期望工作地",
"职位": "职位",
"起始时间": "起始时间",
"学校": "学校",
"专业": "专业",
"起止时间": "起止时间",
"项目名称": "项目名称",
"担任角色": "担任角色",
"项目描述": "项目描述",
"主要工作": "主要工作",
"公司名称": "公司名称",
"部门": "部门",
"职位或描述": "职位或描述",
"作品名称": "作品名称",
"作品描述": "作品描述",
"作品链接": "作品链接",
"技能项": "技能项",
"掌握程度": "掌握程度",
"技能描述": "技能描述",
"获奖时间": "获奖时间",
"奖项内容": "奖项内容",
"获取简历信息失败": "获取简历信息失败",
"\n 请检查用户名 ": "\n 请检查用户名 ",
" 是否正确或者简历信息是否在\n ": " 是否正确或者简历信息是否在\n ",
"下\n ": "\n ",
"请检查用户名 ": "请检查用户名 ",
" 是否正确或者简历信息是否在": " 是否正确或者简历信息是否在",
"下": "",
"进入在线编辑": "进入在线编辑",
"移动端只提供查看功能,在线制作请前往 PC 端": "移动端只提供查看功能,在线制作请前往 PC 端",
"上传配置已应用": "上传配置已应用",
"上传文件有误,请重新上传": "上传文件有误,请重新上传",
"您当前浏览器不支持 FileReader,建议使用谷歌浏览器": "您当前浏览器不支持 FileReader,建议使用谷歌浏览器",
"参考:": "参考:",
"复制配置": "复制配置",
"保存简历": "保存简历",
"导入配置": "导入配置",
"PDF 下载": "PDF 下载",
"\n\n ": "\n\n ",
"中": "",
"编辑模式下, 切换国际化会导致正在配置的内容丢失,请及时保存": "编辑模式下, 切换国际化会导致正在配置的内容丢失,请及时保存",
"编辑": "编辑",
"无用户信息,不允许预览": "无用户信息,不允许预览",
"预览": "预览",
"提交": "提交",
"至今": "至今",
"访问链接": "访问链接",
"确认删除": "确认删除",
"继续添加": "继续添加",
"进行配置": "进行配置",
"移动端模式下,只支持预览,不支持配置": "移动端模式下,只支持预览,不支持配置",
"选择模板": "选择模板",
"配置简历": "配置简历",
" 至今": " 至今",
"默认模板(适用于单页)": "默认模板(适用于单页)",
"简易模板": "简易模板",
"简易模板(适用于多页)": "简易模板(适用于多页)",
"主题色": "主题色",
"tag 标签色": "tag 标签色"
}
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
"react-svg": "^14.1.6"
},
"devDependencies": {
"@babel/core": "^7.21.0",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-decorators": "^7.21.0",
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.21.0",
"@types/lodash-es": "^4.17.6",
"@types/react": "^17.0.3",
"babel-plugin-import": "^1.13.3",
Expand All @@ -30,6 +36,7 @@
"gatsby-plugin-less": "^5.2.0",
"gatsby-plugin-pnpm": "^1.2.10",
"gh-pages": "^3.1.0",
"glob": "^9.2.1",
"husky": "^7.0.4",
"less": "^4.1.0",
"lint-staged": "^12.3.5",
Expand All @@ -46,7 +53,8 @@
"clean": "gatsby clean",
"deploy": "npm run clean && gatsby build --prefix-paths && gh-pages -d public",
"prepare": "husky install",
"lint-staged": "lint-staged"
"lint-staged": "lint-staged",
"i18n-pick": "node ./scripts/i18n-pick.js"
},
"lint-staged": {
"*.{ts,tsx}": [
Expand Down
76 changes: 76 additions & 0 deletions scripts/i18n-pick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const glob = require('glob');
const { transformFileAsync } = require('@babel/core');
const fs = require('fs');
const path = require('path');
const config = require('../i18n.config.js');

const wordSet = new Set();

/** unicode cjk 中日韩文 范围 */
const DOUBLE_BYTE_REGEX = /[\u4E00-\u9FFF]/;
function detectChinese(text) {
if (DOUBLE_BYTE_REGEX.test(text)) {
wordSet.add(text);
}
}

function scan({ types: t }) {
return {
visitor: {
// 匹配: const text = '中文文案'; new Person('小红')
StringLiteral(path) {
const { node } = path;
detectChinese(node.value);
},
// 匹配: jsx 文本 & 属性
JSXText(path) {
detectChinese(path.node.value);
},
},
};
}

const { exclude = [] } = config;

function run(dir) {
glob(`${dir}/**/*.{js,jsx,ts,tsx}`, {
ignore: exclude.concat('node_modules/**'),
})
.then(files => {
Promise.all(
files.map(filename => {
// todo 可以匹配一些规则,直接返回

return transformFileAsync(filename, {
plugins: [
// 装饰器插件
['@babel/plugin-proposal-decorators', { legacy: true }],
scan,
],
presets: [
[
'@babel/preset-typescript',
// 强制开启 jsx 解析,否则尖括号可能会被识别为 typescript 的类型断言。如 `var foo = <string>bar;`
{ isTSX: true, allExtensions: true },
],
['@babel/preset-env', { targets: 'chrome > 58' }],
],
});
})
).then(() => {
fs.open(path.resolve('i18n', 'tmp.json'), 'w', (error, file) => {
if (error) return console.error(error);
fs.writeFile(
file,
JSON.stringify(Object.fromEntries(wordSet.entries()), null, 2),
() => {}
);
});
});
})
.catch(err => {
console.error(err);
});
}

run('./src');
2 changes: 1 addition & 1 deletion src/locale/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const EN_US_LOCALE: Locale = {

导入配置: 'Import config',
复制配置: 'Copy config',
保存简历: 'Save ResumneInfo',
保存简历: 'Save Resume',
'下载 PDF': 'Download pdf',
进行配置: 'Enter config',
选择模板: 'Choose template',
Expand Down

0 comments on commit 8b6e4fe

Please sign in to comment.