-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from warjiang/feature/i18n-glossary
add docs for i18n-tool
- Loading branch information
Showing
7 changed files
with
899 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Install | ||
|
||
|
||
```shell | ||
npm install -g @karmada/i18n-tool | ||
``` | ||
|
||
|
||
# Basic Usage | ||
For a whole frontend project which need to i18n, you can do as following: | ||
```shell | ||
# init config for under your project | ||
# here you can modify the config according to the instructions | ||
i18n-tool/node_modules/.bin/i18n-tool init | ||
|
||
# scan the Chinese and replace with i18n function invoke | ||
# generate corresponding locales files | ||
i18n-tool/node_modules/.bin/i18n-tool scan -c ./i18n.config.cjs | ||
``` | ||
|
||
For a directory which need to i18n, you can specify with `-d` options | ||
```shell | ||
i18n-tool/node_modules/.bin/i18n-tool scan -d directory/to/your/code | ||
``` | ||
|
||
For a single file which need to i18n, you can specify with `-f` options | ||
```shell | ||
i18n-tool/node_modules/.bin/i18n-tool scan -f path/to/your/code/file | ||
``` | ||
|
||
# Advanced Usage | ||
For those advanced developer, maybe they need to specify glossaries install of automatic translation. For these kind of | ||
scenarios, you can create a `glossaries.csv` under the directory of locales, the format of `glossaries.csv` should like | ||
```csv | ||
i18n-key,zh-CN,en-US | ||
86385379cf9cfbc2c554944f1c054a45,概览,Overview | ||
21a4e07b08a4efbbfe2b9d88c208836a,多云资源管理,MultiCloud Resource Management | ||
85fe5099f6807dada65d274810933389,集群,Cluster | ||
``` | ||
after create the `glossaries.csv`, for the same i18n-key, `i18n-tool` will use the content specified in `glossaries.csv` | ||
instead of automatic translation. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const fs = require("node:fs") | ||
const {parse} = require('csv-parse/sync') | ||
const {omit} = require('lodash') | ||
|
||
function makeGlossaryMap(records) { | ||
const _records = records || [] | ||
return _records.reduce((p, c) => { | ||
const i18nKey = c['i18n-key'] | ||
const restData = omit(c, 'i18n-key') | ||
return { | ||
...p, | ||
[i18nKey]: restData | ||
} | ||
}, {}) | ||
} | ||
|
||
function initGlossaries(glossaryFilePath) { | ||
if (!fs.existsSync(glossaryFilePath)) return {} | ||
const content = fs.readFileSync(glossaryFilePath).toString() | ||
const records = parse(content, { | ||
columns: true, | ||
skip_empty_lines: true, | ||
}); | ||
return makeGlossaryMap(records) | ||
} | ||
|
||
module.exports = { | ||
initGlossaries | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.