Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
京东前端
nutui-react
提交
d8e2eab9
N
nutui-react
项目概览
京东前端
/
nutui-react
通知
0
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
nutui-react
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d8e2eab9
编写于
5月 16, 2022
作者:
O
oasis-cloud
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
chore: 抽取中文文案
上级
ea234243
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
29 addition
and
36 deletion
+29
-36
scripts/extract-language.js
scripts/extract-language.js
+29
-36
未找到文件。
scripts/extract-language.js
浏览文件 @
d8e2eab9
...
...
@@ -5,78 +5,71 @@ const crypto = require('crypto')
const
fse
=
require
(
'
fs-extra
'
)
const
shelljs
=
require
(
'
shelljs
'
)
// 输入组件名称,例如 Input 组件, node scripts/extract-language.js Input
// 组件的 demo.tsx 中 demo 组件名字应该为 组件名称 + Demo,例如:InputDemo,CheckboxDemo 等
const
pkg
=
process
.
argv
[
2
]
// utils
function
unique
(
arr
)
{
return
Array
.
from
(
new
Set
(
arr
))
}
const
locales
=
[
'
zh-CN
'
,
'
zh-TW
'
,
'
en-US
'
]
let
fileContent
=
fs
.
readFileSync
(
path
.
join
(
process
.
cwd
(),
`src/packages/
${
pkg
.
toLocaleLowerCase
()}
/demo.tsx`
))
.
toString
()
console
.
log
(
fileContent
.
match
(
/
([
0-9a-z
]
*
[\u
4e00-
\u
9fa5
]
+
[
0-9
]
*
[\u
4e00-
\u
9fa5()()0-9a-z
\/]
*
)
/gi
))
const
zh_CN
=
unique
(
fileContent
.
match
(
/
([
0-9a-z
]
*
[\u
4e00-
\u
9fa5
]
+
[
0-9
]
*
[\u
4e00-
\u
9fa5()()0-9a-z
\/]
*
)
/gi
)
)
function
hash
(
str
)
{
const
hash
=
crypto
.
createHash
(
'
sha256
'
)
hash
.
update
(
str
)
return
hash
.
digest
(
'
hex
'
).
slice
(
0
,
8
)
}
const
lang
=
{}
const
inter
=
[]
// 输入组件名称,例如 Input 组件, node scripts/extract-language.js Input
// 组件的 demo.tsx 中 demo 组件名字应该为 组件名称 + Demo,例如:InputDemo,CheckboxDemo 等
const
pkg
=
process
.
argv
[
2
]
const
locals
=
[
'
zh-CN
'
,
'
zh-TW
'
,
'
en-US
'
]
const
demoFile
=
path
.
join
(
process
.
cwd
(),
`src/packages/
${
pkg
.
toLocaleLowerCase
()}
/demo.tsx`
)
let
demoContent
=
fs
.
readFileSync
(
demoFile
).
toString
()
console
.
log
(
demoContent
.
match
(
/
([
0-9a-z
]
*
[\u
4e00-
\u
9fa5
]
+
[
0-9
]
*
[\u
4e00-
\u
9fa5()()0-9a-z
\/]
*
)
/gi
))
zh_CN
.
forEach
((
item
)
=>
{
const
matchingChinese
=
/
([
0-9a-z
]
*
[\u
4e00-
\u
9fa5
]
+
[
0-9
]
*
[\u
4e00-
\u
9fa5()()0-9a-z
\/]
*
)
/gi
const
unrepeatedChinese
=
unique
(
demoContent
.
match
(
matchingChinese
))
const
localTable
=
{}
const
interfaceType
=
[]
unrepeatedChinese
.
forEach
((
item
)
=>
{
const
k
=
hash
(
item
)
l
ang
[
k
]
=
item
inter
.
push
(
`"
${
k
}
": string`
)
l
ocalTable
[
k
]
=
item
inter
faceType
.
push
(
`"
${
k
}
": string`
)
// 要处理重复
item
=
item
.
replace
(
'
(
'
,
'
\\
(
'
).
replace
(
'
)
'
,
'
\\
)
'
).
replace
(
'
(
'
,
'
\\
(
'
).
replace
(
'
)
'
,
'
\\
)
'
)
fileContent
=
file
Content
demoContent
=
demo
Content
.
replace
(
new
RegExp
(
`=\"
${
item
}
\"`
,
'
g
'
),
`={translated['
${
k
}
']}`
)
.
replace
(
new
RegExp
(
`>\\s*
${
item
}
\\s*<`
,
'
g
'
),
`>{translated['
${
k
}
']}<`
)
.
replace
(
new
RegExp
(
`\'
${
item
}
\'`
,
'
g
'
),
`translated['
${
k
}
']`
)
})
// interface
const
interfaceT
pl
=
`interface T {
${
inter
.
join
(
'
\n
'
)}
const
interfaceT
emplate
=
`interface T {
${
inter
faceType
.
join
(
'
\n
'
)}
}`
// console.log(interfaceTpl)
const
translate
=
{}
locales
.
forEach
((
item
)
=>
(
translate
[
item
]
=
lang
))
// hooks
const
injectTpl
=
`
locals
.
forEach
((
item
)
=>
(
translate
[
item
]
=
localTable
))
const
translateHookTemplate
=
`
const [translated] = useTranslate<T>(
${
JSON
.
stringify
(
translate
,
'
'
,
2
)}
)
`
// console.log('const ${pkg}Demo = () => {')
file
Content
=
demo
Content
=
`import { useTranslate } from '../../sites/assets/locale'\n`
+
file
Content
.
replace
(
demo
Content
.
replace
(
`const
${
pkg
}
Demo = () => {`
,
`
${
interfaceTpl
}
${
interfaceTemplate
}
const
${
pkg
}
Demo = () => {
${
injectTpl
}
${
translateHookTemplate
}
`
)
// console.log(fileContent)
fse
.
outputFile
(
path
.
join
(
process
.
cwd
(),
`src/packages/
${
pkg
.
toLocaleLowerCase
()}
/demo.locale.tsx`
),
file
Content
,
demo
Content
,
'
utf8
'
)
.
then
(()
=>
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录