该项目是一个支持将中文替换成 i18n 国际化标记,并支持自动翻译的命令行工具
见掘金文章
npm i @ifreeovo/i18n-extract-cli -g
转换前
import { useState } from 'react'
/*i18n-ignore*/
const b = '被忽略提取的文案'
function Example() {
const [msg, setMsg] = useState('你好')
return (
<div>
<p title="标题">{msg + '呵呵'}</p>
<button onClick={() => setMsg(msg + '啊')}>点击</button>
</div>
)
}
export default Example
转换后
import { t } from 'i18n'
import { useState } from 'react'
/*i18n-ignore*/
const b = '被忽略提取的文案'
function Example() {
const [msg, setMsg] = useState(t('你好'))
return (
<div>
<p title={t('标题')}>{msg + t('呵呵')}</p>
<button onClick={() => setMsg(msg + t('啊'))}>{t('点击')}</button>
</div>
)
}
export default Example
转换前
<template>
<div :label="'标签'" :title="1 + '标题'">
<p title="测试注释">内容</p>
<button @click="handleClick('信息')">点击</button>
</div>
</template>
<script>
export default {
methods: {
handleClick() {
console.log('点了')
},
},
}
</script>
转换后
<template>
<div :label="$t('标签')" :title="1 + $t('标题')">
<p :title="$t('测试注释')">{{ $t('内容') }}</p>
<button @click="handleClick($t('信息'))">{{ $t('点击') }}</button>
</div>
</template>
<script>
export default {
methods: {
handleClick() {
console.log(this.$t('点了'))
},
},
}
</script>