Skip to content

Commit

Permalink
feat: CLI to check for untranslated messages (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
freakzlike committed Jul 8, 2023
1 parent aa142d8 commit 3b0b315
Show file tree
Hide file tree
Showing 27 changed files with 613 additions and 207 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ Minimalistic and low dependency translation key extractor

## Execute

Extracts translations from source files and writes translation files:

```shell
i18n-extract ./i18n-extract.config.cjs
```

Extract translations from source files and checks for untranslated messages.
Can be used in CI and does not write the translation files.

```shell
i18n-check ./i18n-extract.config.cjs
```

## Config

```js
Expand Down
16 changes: 16 additions & 0 deletions bin/check.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node
const { i18nCheck } = require('../dist/i18n-extract.cjs')
const path = require('node:path')

const args = process.argv.slice(2)
if (!args.length || !args[0]) {
console.log('Error: Missing config path')
process.exit()
}

const options = require(path.resolve(process.cwd(), args[0]))
i18nCheck(options).then(result => {
if (!result) {
process.exit(1)
}
})
File renamed without changes.
8 changes: 8 additions & 0 deletions examples/new/i18n-extract.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
input: [
'examples/new/src/**/*.ts'
],
output: 'examples/new/locales/{{lng}}/{{ns}}.json',
languages: ['de', 'en-GB'],
namespaces: ['default', 'other']
}
4 changes: 4 additions & 0 deletions examples/new/locales/de/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"key_1": "Key 1 DE",
"key_2": "Key 2 DE"
}
4 changes: 4 additions & 0 deletions examples/new/locales/en-GB/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"key_1": "Key 1 EN",
"key_2": "Key 2 EN"
}
3 changes: 3 additions & 0 deletions examples/new/src/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function $t (key: string, params?: Record<string, unknown>): string {
return `translated: ${key}`
}
5 changes: 5 additions & 0 deletions examples/new/src/typescript-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { $t } from './i18n'

console.log($t('key_1'))
$t('key_2')
$t('other:key_1')
7 changes: 7 additions & 0 deletions examples/translated/i18n-extract.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
input: [
'examples/translated/src/**/*.ts'
],
output: 'examples/translated/locales/{{lng}}.json',
languages: ['de', 'en-GB']
}
6 changes: 6 additions & 0 deletions examples/translated/locales/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"key_1": "Key 1 DE",
"key_2": "Key 2 DE",
"key_3": "Key 3 DE",
"key_4": ""
}
6 changes: 6 additions & 0 deletions examples/translated/locales/en-GB.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"key_1": "Key 1 EN",
"key_2": "Key 2 EN",
"key_3": "Key 3 EN",
"key_4": ""
}
22 changes: 22 additions & 0 deletions examples/translated/src/typescript-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const $t = (key: string) => `translated: ${key}`


console.log($t('key_1'))
$t('key_2')
$t('key_1')

// Multiline
$t(
'key_3'
)

$t( "key_3" )

/**
* Not parse this
*/
const $a = $t
const a$t = $t

$a('not_key_1')
a$t('not_key_2')
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
],
"main": "./dist/i18n-extract.cjs",
"bin": {
"i18n-extract": "./bin/cli.cjs"
"i18n-extract": "./bin/extract.cjs",
"i18n-check": "./bin/check.cjs"
},
"module": "./dist/i18n-extract.js",
"exports": {
Expand All @@ -36,7 +37,8 @@
"test": "vitest --coverage",
"lint": "eslint src/ tests/ --ext .ts --ignore-path .gitignore",
"lint:fix": "pnpm run lint --fix",
"demo:cli": "node bin/cli.cjs examples/namespaces/i18n-extract.config.cjs",
"demo:extract": "node bin/extract.cjs examples/namespaces/i18n-extract.config.cjs",
"demo:check": "node bin/check.cjs examples/namespaces/i18n-extract.config.cjs",
"release": "semantic-release --no-ci"
},
"dependencies": {
Expand All @@ -54,11 +56,11 @@
"@vitest/coverage-v8": "0.33.0",
"eslint": "8.44.0",
"rimraf": "5.0.1",
"rollup": "3.26.1",
"rollup": "3.26.2",
"semantic-release": "21.0.7",
"tslib": "2.6.0",
"typescript": "5.1.6",
"vite": "4.3.9",
"vite": "4.4.2",
"vitest": "0.33.0"
}
}

0 comments on commit 3b0b315

Please sign in to comment.