Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit 2f87c1e

Browse files
committed
⭐ new(command): i18n reporting command (experimental)
1 parent b8c94f8 commit 2f87c1e

File tree

14 files changed

+662
-525
lines changed

14 files changed

+662
-525
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
__snapshots__
12
node_modules
3+
tests/e2e/projects/vue-i18n-js/
4+
tests/e2e/projects/vue-i18n-ts/
5+
tests/output.json
26
.DS_Store
37
*.log
48
*.swp

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Vue CLI 3 plugin to add vue-i18n to your Vue Project
1313
## :star: Features
1414
- [vue-i18n](https://github.com/kazupon/vue-i18n) basic scaffolding
1515
- Locale messages in Single File components with [vue-i18n-loader](https://github.com/kazupon/vue-i18n-loader)
16+
- Locale messages missing & unused reporting (experimental)
1617
- Env Variables
1718

1819
## :rocket: Getting Started
@@ -29,6 +30,14 @@ cd my-vue-app
2930
vue add i18n
3031
```
3132

33+
## :hammer: Injected Commands
34+
- **`vue-cli-service i18n:report`** (experimental)
35+
36+
Report the missing locale message keys and unused keys.
37+
38+
> NOTE: limitation
39+
> `vue-cli-service i18n:report` cannot detect missing and unused keys from local messages of i18n custom blocks.
40+
3241
## :clipboard: Env variables
3342
When vue-i18n code files had been scaffolded into your project, the following env variables generate into `.env`:
3443

generator/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ module.exports = (api, options, rootOptions) => {
2525
*/
2626

2727
const pkg = {
28+
scripts: {
29+
'i18n:report': `vue-cli-service i18n:report --src './src/**/*.?(js|vue)' --locales '${`./src/${localeDir}/**/*.json`}'`
30+
},
2831
dependencies: {
2932
'vue-i18n': '^8.0.0'
3033
},

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ module.exports = (api, options) => {
1818
.end()
1919
}
2020
})
21+
22+
const report = require('./report')
23+
api.registerCommand('i18n:report', report.options, args => report.service(args, api))
2124
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"rimraf": "^2.6.3",
1818
"vue": "^2.5.16",
1919
"vue-i18n": "^8.0.0",
20-
"vue-i18n-extract": "^0.4.11"
20+
"vue-i18n-extract": "^0.4.13"
2121
},
2222
"devDependencies": {
2323
"@vue/cli": "^3.4.1",

report.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { chalk } = require(require.resolve('@vue/cli-shared-utils'))
2+
const { resolve } = require('path')
3+
const i18nExtract = require('vue-i18n-extract').default
4+
5+
const EXTRACT_MISSING = 1
6+
const EXTRACT_UNUSED = 2
7+
const EXTRACT_ALL = 3
8+
9+
const options = {
10+
description: 'vue-i18n report',
11+
usage: 'vue-cli-service i18n:report [options]',
12+
options: {
13+
locales: 'target locale files path, required',
14+
src: 'target source codes path, required',
15+
type: 'reporting type, default `missing` and `unused` all, optional',
16+
output: 'create a json file out of report, optional'
17+
}
18+
}
19+
20+
function resolveReportType (args) {
21+
let type = EXTRACT_ALL
22+
if (args.type === 'missing') {
23+
type = EXTRACT_MISSING
24+
} else if (args.type === 'unused') {
25+
type = EXTRACT_UNUSED
26+
}
27+
return type
28+
}
29+
30+
async function service (args = {}, api) {
31+
if (!args.src) {
32+
console.log(chalk.red(`not specified 'src' argument.`))
33+
return
34+
}
35+
36+
if (!args.locales) {
37+
console.log(chalk.red(`not specified 'locales' argument.`))
38+
return
39+
}
40+
41+
const currentDir = process.cwd()
42+
const srcFiles = resolve(currentDir, args.src)
43+
const localeFiles = resolve(currentDir, args.locales)
44+
const extractType = resolveReportType(args)
45+
46+
const i18nReport = i18nExtract.createI18NReport(srcFiles, localeFiles, extractType)
47+
i18nExtract.logI18NReport(i18nReport)
48+
49+
if (args.output) {
50+
await i18nExtract.writeReportToFile(i18nReport, args.output)
51+
}
52+
53+
return i18nReport
54+
}
55+
56+
module.exports = {
57+
service,
58+
options
59+
}

tests/fixture/App.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div id="app">main app</div>
3+
</template>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<p>{{ $t('messages.foo') }}</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'component1',
8+
created () {
9+
console.log(this.$t('messages.buz'))
10+
}
11+
}
12+
</script>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<template>
2+
<p>{{ $t('messages.bar') }}</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'component2',
8+
created () {
9+
console.log(this.$t('messages.hoge'))
10+
console.log(this.$t('messages.fuga.foo'))
11+
}
12+
}
13+
</script>
14+

tests/fixture/locales/cn.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
messages: {
3+
foo: 'foo'
4+
}
5+
}

0 commit comments

Comments
 (0)