@orderly.network/i18n 2.0.8-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +219 -0
- package/bin/cli.js +172 -0
- package/dist/index.d.mts +1298 -0
- package/dist/index.d.ts +1298 -0
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +13 -0
- package/dist/index.mjs.map +1 -0
- package/dist/locale.csv +603 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# @orderly.network/i18n
|
|
2
|
+
|
|
3
|
+
Internationalization and cli tools for Orderly SDK. Based on i18next ecosystem.
|
|
4
|
+
|
|
5
|
+
## Integration
|
|
6
|
+
|
|
7
|
+
### 1. Wrap LocaleProvider
|
|
8
|
+
|
|
9
|
+
The LocaleProvider is the core component that provides locale resources to your app. You must wrap you app with LocaleProvider.
|
|
10
|
+
|
|
11
|
+
### 2. Provide Resources
|
|
12
|
+
|
|
13
|
+
- By default, English (en) is included.
|
|
14
|
+
- If you want to support other languages besides English, you need to explicitly import it from the i18n package and pass it to the resources prop of LocaleProvider.
|
|
15
|
+
- Currently, we provider English (en) and Chinese (zh) locale files.
|
|
16
|
+
- You can also extend the built-in messages by merging them with your own locale files.
|
|
17
|
+
- With each release we will generate csv files (dist/locale.csv) for easy translation and we provide a cli to convert between csv and json files.
|
|
18
|
+
- It can translate not only the ui in the SDK, but also other components you write outside of the SDK.
|
|
19
|
+
|
|
20
|
+
### Example
|
|
21
|
+
|
|
22
|
+
Here’s a complete example of how to set up the i18n integration:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { FC, ReactNode } from "react";
|
|
26
|
+
import { WalletConnectorProvider } from "@orderly.network/wallet-connector";
|
|
27
|
+
import { OrderlyAppProvider } from "@orderly.network/react-app";
|
|
28
|
+
import {
|
|
29
|
+
LocaleProvider,
|
|
30
|
+
Resources,
|
|
31
|
+
LocaleCode,
|
|
32
|
+
zh,
|
|
33
|
+
} from "@orderly.network/i18n";
|
|
34
|
+
|
|
35
|
+
// extend or overrides English translations
|
|
36
|
+
const extendEn = {
|
|
37
|
+
"extend.trading": "Trading",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// extend or overrides chinese translations
|
|
41
|
+
const extendZh = {
|
|
42
|
+
"extend.trading": "交易",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
type ExtendLocaleMessages = typeof extendEn;
|
|
46
|
+
|
|
47
|
+
// define language resources
|
|
48
|
+
const resources: Resources<ExtendLocaleMessages> = {
|
|
49
|
+
en: extendEn,
|
|
50
|
+
zh: {
|
|
51
|
+
...zh,
|
|
52
|
+
...extendZh,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const OrderlyProvider: FC<{ children: ReactNode }> = (props) => {
|
|
57
|
+
const onLocaleChange = (locale: LocaleCode) => {};
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<LocaleProvider resources={resources} onLocaleChange={onLocaleChange}>
|
|
61
|
+
<WalletConnectorProvider>
|
|
62
|
+
<OrderlyAppProvider
|
|
63
|
+
brokerId="orderly"
|
|
64
|
+
brokerName="Orderly"
|
|
65
|
+
networkId="testnet"
|
|
66
|
+
>
|
|
67
|
+
{props.children}
|
|
68
|
+
</OrderlyAppProvider>
|
|
69
|
+
</WalletConnectorProvider>
|
|
70
|
+
</LocaleProvider>
|
|
71
|
+
);
|
|
72
|
+
};
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Add more languages
|
|
76
|
+
|
|
77
|
+
We also support adding more custom languages
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { FC, ReactNode } from "react";
|
|
81
|
+
import { WalletConnectorProvider } from "@orderly.network/wallet-connector";
|
|
82
|
+
import { OrderlyAppProvider } from "@orderly.network/react-app";
|
|
83
|
+
import {
|
|
84
|
+
LocaleProvider,
|
|
85
|
+
Resources,
|
|
86
|
+
zh,
|
|
87
|
+
LocaleEnum,
|
|
88
|
+
LocaleCode,
|
|
89
|
+
Language,
|
|
90
|
+
} from "@orderly.network/i18n";
|
|
91
|
+
|
|
92
|
+
// japanese locale
|
|
93
|
+
const ja = {
|
|
94
|
+
"extend.ja": "日本語",
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// korean locale
|
|
98
|
+
const ko = {
|
|
99
|
+
"extend.ko": "한국어",
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// define language resources
|
|
103
|
+
const resources: Resources = {
|
|
104
|
+
en,
|
|
105
|
+
ja,
|
|
106
|
+
ko,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// custom languages
|
|
110
|
+
const languages: Language[] = [
|
|
111
|
+
{ localCode: LocaleEnum.en, displayName: "English" },
|
|
112
|
+
{ localCode: LocaleEnum.ja, displayName: "日本語" },
|
|
113
|
+
{ localCode: LocaleEnum.ko, displayName: "한국어" },
|
|
114
|
+
];
|
|
115
|
+
|
|
116
|
+
const OrderlyProvider: FC<{ children: ReactNode }> = (props) => {
|
|
117
|
+
const onLocaleChange = (locale: LocaleCode) => {};
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<LocaleProvider
|
|
121
|
+
resources={resources}
|
|
122
|
+
languages={languages}
|
|
123
|
+
onLocaleChange={onLocaleChange}
|
|
124
|
+
>
|
|
125
|
+
<WalletConnectorProvider>
|
|
126
|
+
<OrderlyAppProvider
|
|
127
|
+
brokerId="orderly"
|
|
128
|
+
brokerName="Orderly"
|
|
129
|
+
networkId="testnet"
|
|
130
|
+
>
|
|
131
|
+
{props.children}
|
|
132
|
+
</OrderlyAppProvider>
|
|
133
|
+
</WalletConnectorProvider>
|
|
134
|
+
</LocaleProvider>
|
|
135
|
+
);
|
|
136
|
+
};
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## CLI
|
|
140
|
+
|
|
141
|
+
## Usage
|
|
142
|
+
|
|
143
|
+
The package provides a CLI tool for managing Internationalization files.
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
npx @orderly.network/i18n <command> [options]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Commands
|
|
150
|
+
|
|
151
|
+
### csv2json
|
|
152
|
+
|
|
153
|
+
Convert a locale CSV file to multiple locale JSON files.
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
npx @orderly.network/i18n csv2json <input> <outputDir>
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Example:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
npx @orderly.network/i18n csv2json ./dist/locale.csv ./dist/locale
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### json2csv
|
|
166
|
+
|
|
167
|
+
Convert multiple locale JSON files to a single locale CSV file.
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
npx @orderly.network/i18n json2csv <input> <output>
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Example:
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
npx @orderly.network/i18n json2csv ./dist/locale/en.json,./dist/locale/zh.json ./dist/locale.csv
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### diffcsv
|
|
180
|
+
|
|
181
|
+
Compare two locale CSV files to find differences.
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
npx @orderly.network/i18n diffcsv <oldFile> <newFile>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Example:
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
npx @orderly.network/i18n diffcsv ./dist/locale1.csv ./dist/locale2.csv
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### generateCsv
|
|
194
|
+
|
|
195
|
+
Generate a locale CSV file from your source files.
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
npx @orderly.network/i18n generateCsv <output>
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Example:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
npx @orderly.network/i18n generateCsv ./dist/locale.csv
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### fillJson
|
|
208
|
+
|
|
209
|
+
Fill values from an input locale JSON file and generate a new locale JSON file.
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
npx @orderly.network/i18n fillJson <input> <output>
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Example:
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
npx @orderly.network/i18n fillJson ./src/locale/zh.json ./dist/locale/zh.json
|
|
219
|
+
```
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const yargs = require("yargs/yargs");
|
|
4
|
+
const { hideBin } = require("yargs/helpers");
|
|
5
|
+
const { generateCsv } = require("../script/generateCsv");
|
|
6
|
+
const { json2csv } = require("../script/json2csv");
|
|
7
|
+
const { csv2json } = require("../script/csv2json");
|
|
8
|
+
const { diffCsv } = require("../script/diffCsv");
|
|
9
|
+
const { fillJson } = require("../script/fillJson");
|
|
10
|
+
main();
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
const argv = getArgv();
|
|
14
|
+
|
|
15
|
+
const { _, input, output, oldFile, newFile, outputDir } = argv;
|
|
16
|
+
|
|
17
|
+
const command = _[0];
|
|
18
|
+
// console.log("argv", argv);
|
|
19
|
+
|
|
20
|
+
switch (command) {
|
|
21
|
+
case "json2csv":
|
|
22
|
+
await json2csv(input, output);
|
|
23
|
+
break;
|
|
24
|
+
case "csv2json":
|
|
25
|
+
await csv2json(input, outputDir);
|
|
26
|
+
break;
|
|
27
|
+
case "diffcsv":
|
|
28
|
+
await diffCsv(oldFile, newFile);
|
|
29
|
+
break;
|
|
30
|
+
case "generateCsv":
|
|
31
|
+
await generateCsv(output);
|
|
32
|
+
break;
|
|
33
|
+
case "fillJson":
|
|
34
|
+
await fillJson(input, output);
|
|
35
|
+
break;
|
|
36
|
+
default:
|
|
37
|
+
console.log("Invalid command");
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function getArgv() {
|
|
43
|
+
const argv = yargs(hideBin(process.argv))
|
|
44
|
+
.scriptName("")
|
|
45
|
+
.usage("i18n locale tools")
|
|
46
|
+
.usage("$0 <command> [options]")
|
|
47
|
+
.strict()
|
|
48
|
+
// Error if no subcommand is provided
|
|
49
|
+
.demandCommand(1, "Please provide a valid subcommand")
|
|
50
|
+
.help()
|
|
51
|
+
// Add `-h` as an alias for `--help`
|
|
52
|
+
.alias("h", "help")
|
|
53
|
+
|
|
54
|
+
// csv2json command
|
|
55
|
+
.command(
|
|
56
|
+
"csv2json <input> <outputDir>",
|
|
57
|
+
"Convert locale CSV to multiple locale JSON files",
|
|
58
|
+
(yargs) => {
|
|
59
|
+
return yargs
|
|
60
|
+
.positional("input", {
|
|
61
|
+
describe: "Input path for the locale CSV file",
|
|
62
|
+
type: "string",
|
|
63
|
+
demandOption: true,
|
|
64
|
+
})
|
|
65
|
+
.positional("outputDir", {
|
|
66
|
+
describe: "Output directory for locale JSON files",
|
|
67
|
+
type: "string",
|
|
68
|
+
demandOption: true,
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
(argv) => {
|
|
72
|
+
console.log(
|
|
73
|
+
`Converting ${argv.input} to locale JSON files, output directory: ${argv.outputDir}`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
// json2csv command
|
|
79
|
+
.command(
|
|
80
|
+
"json2csv <input> <output>",
|
|
81
|
+
"Convert locale JSON files to a locale CSV file",
|
|
82
|
+
(yargs) => {
|
|
83
|
+
return yargs
|
|
84
|
+
.positional("input", {
|
|
85
|
+
describe:
|
|
86
|
+
"Path to locale JSON file(s) (supports multiple files, separated by commas)",
|
|
87
|
+
type: "string",
|
|
88
|
+
demandOption: true,
|
|
89
|
+
})
|
|
90
|
+
.positional("output", {
|
|
91
|
+
describe: "Output path for the locale CSV file",
|
|
92
|
+
type: "string",
|
|
93
|
+
demandOption: true,
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
(argv) => {
|
|
97
|
+
const inputFiles = argv.input.split(","); // Parse multiple input files
|
|
98
|
+
console.log(
|
|
99
|
+
`Converting locale JSON files: ${inputFiles.join(
|
|
100
|
+
", "
|
|
101
|
+
)} to locale CSV: ${argv.output}`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
// diffcsv command
|
|
107
|
+
.command(
|
|
108
|
+
"diffcsv <oldFile> <newFile>",
|
|
109
|
+
"Compare two locale CSV files",
|
|
110
|
+
(yargs) => {
|
|
111
|
+
return yargs
|
|
112
|
+
.positional("oldFile", {
|
|
113
|
+
describe: "Path to the first locale CSV file",
|
|
114
|
+
type: "string",
|
|
115
|
+
demandOption: true,
|
|
116
|
+
})
|
|
117
|
+
.positional("newFile", {
|
|
118
|
+
describe: "Path to the second CSV file",
|
|
119
|
+
type: "string",
|
|
120
|
+
demandOption: true,
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
(argv) => {
|
|
124
|
+
console.log(
|
|
125
|
+
`Comparing locale CSV files: ${argv.oldFile} and ${argv.newFile}`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
// generateCsv command
|
|
131
|
+
.command(
|
|
132
|
+
"generateCsv <output>",
|
|
133
|
+
"Generate a locale CSV file",
|
|
134
|
+
(yargs) => {
|
|
135
|
+
return yargs.positional("output", {
|
|
136
|
+
describe: "Output path for the locale CSV file",
|
|
137
|
+
type: "string",
|
|
138
|
+
// Required
|
|
139
|
+
demandOption: true,
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
(argv) => {
|
|
143
|
+
console.log(`Generating locale CSV file at: ${argv.output}`);
|
|
144
|
+
}
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
// fillJson command
|
|
148
|
+
.command(
|
|
149
|
+
"fillJson <input> <output>",
|
|
150
|
+
"Fill values from the input locale JSON file and generate a new locale JSON file",
|
|
151
|
+
(yargs) => {
|
|
152
|
+
return yargs
|
|
153
|
+
.positional("input", {
|
|
154
|
+
describe: "Input path for the locale JSON file",
|
|
155
|
+
type: "string",
|
|
156
|
+
demandOption: true,
|
|
157
|
+
})
|
|
158
|
+
.positional("output", {
|
|
159
|
+
describe: "Output path for the locale JSON file",
|
|
160
|
+
type: "string",
|
|
161
|
+
demandOption: true,
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
(argv) => {
|
|
165
|
+
console.log(
|
|
166
|
+
`Filling values from the input locale JSON file: ${argv.input} and generating a new locale JSON file: ${argv.output}`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
).argv;
|
|
170
|
+
|
|
171
|
+
return argv;
|
|
172
|
+
}
|