@orderly.network/i18n 2.1.0 → 2.1.1

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 CHANGED
@@ -2,35 +2,115 @@
2
2
 
3
3
  Internationalization and cli tools for Orderly SDK. Based on i18next ecosystem.
4
4
 
5
- ## Integration
5
+ ## Integration Guide
6
6
 
7
- ### 1. Wrap LocaleProvider
7
+ Follow these steps to integrate localization support in your app using Orderly SDK:
8
8
 
9
- The LocaleProvider is the core component that provides locale resources to your app. You must wrap you app with LocaleProvider.
9
+ ### 1. Wrap Your App with LocaleProvider
10
10
 
11
- ### 2. Provide Resources
11
+ The LocaleProvider is the core component that supplies localized resources to your application. Make sure to wrap your app’s root component with LocaleProvider.
12
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.
13
+ ```tsx
14
+ import { LocaleProvider } from "@orderly.network/i18n";
19
15
 
20
- ### Example
16
+ <LocaleProvider>
17
+ <YourApp />
18
+ </LocaleProvider>;
19
+ ```
20
+
21
+ ### 2. Provide Locale Data
22
+
23
+ #### Default Language
24
+
25
+ - English (`en`) is included by default.
26
+
27
+ #### Supported Locales
28
+
29
+ We currently support **8 locales**, located in the `dist/locales` directory:
30
+
31
+ | Locale Code | Language |
32
+ | ----------- | ---------- |
33
+ | `en.json` | English |
34
+ | `zh.json` | Chinese |
35
+ | `ja.json` | Japanese |
36
+ | `es.json` | Spanish |
37
+ | `ko.json` | Korean |
38
+ | `vi.json` | Vietnamese |
39
+ | `de.json` | German |
40
+ | `fr.json` | French |
41
+
42
+ > We plan to add more languages in future updates.
43
+
44
+ #### CSV for Easy Translation
45
+
46
+ - Each release generates a `dist/locale.csv` file to simplify translation workflows.
47
+ - We provide a CLI tool to convert between CSV and JSON formats.
48
+
49
+ ### 3. Extending Locale Files
50
+
51
+ You can localize both the SDK UI and your own custom components.
52
+
53
+ - When adding custom keys, prefix them with `extend.` to avoid conflicts with default keys.
54
+
55
+ ```json
56
+ {
57
+ "extend.custom.button.label": "My Custom Button"
58
+ }
59
+ ```
60
+
61
+ ## Example
62
+
63
+ Here's a complete example of how to set up the i18n integration:
21
64
 
22
- Here’s a complete example of how to set up the i18n integration:
65
+ ### Async load locale files
23
66
 
24
67
  ```typescript
25
68
  import { FC, ReactNode } from "react";
26
69
  import { WalletConnectorProvider } from "@orderly.network/wallet-connector";
27
70
  import { OrderlyAppProvider } from "@orderly.network/react-app";
28
- import {
29
- LocaleProvider,
30
- Resources,
31
- LocaleCode,
32
- zh,
33
- } from "@orderly.network/i18n";
71
+ import { LocaleProvider, LocaleEnum, LocaleCode } from "@orderly.network/i18n";
72
+
73
+ const OrderlyProvider: FC<{ children: ReactNode }> = (props) => {
74
+ const onLanguageChanged = async (lang: LocaleCode) => {};
75
+
76
+ // please copy build-in locale files to you public/locales
77
+ // and copy you extend locale files to public/locales/extend
78
+ const loadPath = (lang: LocaleCode) => {
79
+ const _lang = parseI18nLang(lang);
80
+ if (_lang === LocaleEnum.en) {
81
+ // because en is built-in, we need to load the en extend only
82
+ return `/locales/extend/${_lang}.json`;
83
+ }
84
+ return [`/locales/${_lang}.json`, `/locales/extend/${_lang}.json`];
85
+ };
86
+
87
+ return (
88
+ <LocaleProvider
89
+ onLanguageChanged={onLanguageChanged}
90
+ backend={{ loadPath }}
91
+ >
92
+ <WalletConnectorProvider>
93
+ <OrderlyAppProvider
94
+ brokerId="orderly"
95
+ brokerName="Orderly"
96
+ networkId="testnet"
97
+ >
98
+ {props.children}
99
+ </OrderlyAppProvider>
100
+ </WalletConnectorProvider>
101
+ </LocaleProvider>
102
+ );
103
+ };
104
+ ```
105
+
106
+ ### Sync Load locale data
107
+
108
+ ```typescript
109
+ import { FC, ReactNode } from "react";
110
+ import { WalletConnectorProvider } from "@orderly.network/wallet-connector";
111
+ import { OrderlyAppProvider } from "@orderly.network/react-app";
112
+ import { LocaleProvider, LocaleCode, Resources } from "@orderly.network/i18n";
113
+ import zh from "@orderly.network/i18n/locales/zh.json";
34
114
 
35
115
  // extend or overrides English translations
36
116
  const extendEn = {
@@ -42,10 +122,8 @@ const extendZh = {
42
122
  "extend.trading": "交易",
43
123
  };
44
124
 
45
- type ExtendLocaleMessages = typeof extendEn;
46
-
47
125
  // define language resources
48
- const resources: Resources<ExtendLocaleMessages> = {
126
+ const resources: Resources = {
49
127
  en: extendEn,
50
128
  zh: {
51
129
  ...zh,
@@ -54,10 +132,10 @@ const resources: Resources<ExtendLocaleMessages> = {
54
132
  };
55
133
 
56
134
  const OrderlyProvider: FC<{ children: ReactNode }> = (props) => {
57
- const onLocaleChange = (locale: LocaleCode) => {};
135
+ const onLanguageChanged = (locale: LocaleCode) => {};
58
136
 
59
137
  return (
60
- <LocaleProvider resources={resources} onLocaleChange={onLocaleChange}>
138
+ <LocaleProvider resources={resources} onLanguageChanged={onLocaleChange}>
61
139
  <WalletConnectorProvider>
62
140
  <OrderlyAppProvider
63
141
  brokerId="orderly"
@@ -72,7 +150,7 @@ const OrderlyProvider: FC<{ children: ReactNode }> = (props) => {
72
150
  };
73
151
  ```
74
152
 
75
- ### Add more languages
153
+ ### Add custom languages
76
154
 
77
155
  We also support adding more custom languages
78
156
 
@@ -83,7 +161,6 @@ import { OrderlyAppProvider } from "@orderly.network/react-app";
83
161
  import {
84
162
  LocaleProvider,
85
163
  Resources,
86
- zh,
87
164
  LocaleEnum,
88
165
  LocaleCode,
89
166
  Language,
@@ -101,7 +178,6 @@ const ko = {
101
178
 
102
179
  // define language resources
103
180
  const resources: Resources = {
104
- en,
105
181
  ja,
106
182
  ko,
107
183
  };
@@ -114,13 +190,13 @@ const languages: Language[] = [
114
190
  ];
115
191
 
116
192
  const OrderlyProvider: FC<{ children: ReactNode }> = (props) => {
117
- const onLocaleChange = (locale: LocaleCode) => {};
193
+ const onLanguageChanged = (locale: LocaleCode) => {};
118
194
 
119
195
  return (
120
196
  <LocaleProvider
121
197
  resources={resources}
122
198
  languages={languages}
123
- onLocaleChange={onLocaleChange}
199
+ onLanguageChanged={onLanguageChanged}
124
200
  >
125
201
  <WalletConnectorProvider>
126
202
  <OrderlyAppProvider
@@ -159,7 +235,7 @@ npx @orderly.network/i18n csv2json <input> <outputDir>
159
235
  Example:
160
236
 
161
237
  ```bash
162
- npx @orderly.network/i18n csv2json ./dist/locale.csv ./dist/locale
238
+ npx @orderly.network/i18n csv2json ./dist/locale.csv ./dist/locales
163
239
  ```
164
240
 
165
241
  ### json2csv
@@ -167,13 +243,13 @@ npx @orderly.network/i18n csv2json ./dist/locale.csv ./dist/locale
167
243
  Convert multiple locale JSON files to a single locale CSV file.
168
244
 
169
245
  ```bash
170
- npx @orderly.network/i18n json2csv <input> <output>
246
+ npx @orderly.network/i18n json2csv <inputDir> <output>
171
247
  ```
172
248
 
173
249
  Example:
174
250
 
175
251
  ```bash
176
- npx @orderly.network/i18n json2csv ./dist/locale/en.json,./dist/locale/zh.json ./dist/locale.csv
252
+ npx @orderly.network/i18n json2csv ./locales ./dist/locale.csv
177
253
  ```
178
254
 
179
255
  ### diffcsv
@@ -190,30 +266,44 @@ Example:
190
266
  npx @orderly.network/i18n diffcsv ./dist/locale1.csv ./dist/locale2.csv
191
267
  ```
192
268
 
193
- ### generateCsv
269
+ ### fillJson
194
270
 
195
- Generate a locale CSV file from your source files.
271
+ Fill values from an input locale JSON file and generate a new locale JSON file.
196
272
 
197
273
  ```bash
198
- npx @orderly.network/i18n generateCsv <output>
274
+ npx @orderly.network/i18n fillJson <input> <output>
199
275
  ```
200
276
 
201
277
  Example:
202
278
 
203
279
  ```bash
204
- npx @orderly.network/i18n generateCsv ./dist/locale.csv
280
+ npx @orderly.network/i18n fillJson ./src/locale/zh.json ./dist/locale/zh.json
205
281
  ```
206
282
 
207
- ### fillJson
283
+ ### separateJson
208
284
 
209
- Fill values from an input locale JSON file and generate a new locale JSON file.
285
+ Separate JSON files into default and extend key values based on a specified key.
210
286
 
211
287
  ```bash
212
- npx @orderly.network/i18n fillJson <input> <output>
288
+ npx @orderly.network/i18n separateJson <inputDir> <outputDir> <separateKey>
213
289
  ```
214
290
 
215
291
  Example:
216
292
 
217
293
  ```bash
218
- npx @orderly.network/i18n fillJson ./src/locale/zh.json ./dist/locale/zh.json
294
+ npx @orderly.network/i18n separateJson ./locales ./dist/locales extend
295
+ ```
296
+
297
+ ### mergeJson
298
+
299
+ Merge default and extend JSON files back into one file.
300
+
301
+ ```bash
302
+ npx @orderly.network/i18n mergeJson <inputDir> <outputDir>
303
+ ```
304
+
305
+ Example:
306
+
307
+ ```bash
308
+ npx @orderly.network/i18n mergeJson ./dist/locales1 ./dist/locales2
219
309
  ```
package/bin/cli.js CHANGED
@@ -2,24 +2,35 @@
2
2
 
3
3
  const yargs = require("yargs/yargs");
4
4
  const { hideBin } = require("yargs/helpers");
5
- const { generateCsv } = require("../script/generateCsv");
5
+ // const { generateCsv } = require("../script/generateCsv");
6
6
  const { json2csv } = require("../script/json2csv");
7
7
  const { csv2json } = require("../script/csv2json");
8
8
  const { diffCsv } = require("../script/diffCsv");
9
9
  const { fillJson } = require("../script/fillJson");
10
+ const { separateJson } = require("../script/separateJson");
11
+ const { mergeJson } = require("../script/mergeJson");
10
12
  main();
11
13
 
12
14
  async function main() {
13
15
  const argv = getArgv();
14
16
 
15
- const { _, input, output, oldFile, newFile, outputDir } = argv;
17
+ const {
18
+ _,
19
+ input,
20
+ output,
21
+ oldFile,
22
+ newFile,
23
+ inputDir,
24
+ outputDir,
25
+ separateKey,
26
+ } = argv;
16
27
 
17
28
  const command = _[0];
18
29
  // console.log("argv", argv);
19
30
 
20
31
  switch (command) {
21
32
  case "json2csv":
22
- await json2csv(input, output);
33
+ await json2csv(inputDir, output);
23
34
  break;
24
35
  case "csv2json":
25
36
  await csv2json(input, outputDir);
@@ -27,12 +38,18 @@ async function main() {
27
38
  case "diffcsv":
28
39
  await diffCsv(oldFile, newFile);
29
40
  break;
30
- case "generateCsv":
31
- await generateCsv(output);
32
- break;
41
+ // case "generateCsv":
42
+ // await generateCsv(output);
43
+ // break;
33
44
  case "fillJson":
34
45
  await fillJson(input, output);
35
46
  break;
47
+ case "separateJson":
48
+ await separateJson(inputDir, outputDir, separateKey);
49
+ break;
50
+ case "mergeJson":
51
+ await mergeJson(inputDir, outputDir);
52
+ break;
36
53
  default:
37
54
  console.log("Invalid command");
38
55
  break;
@@ -77,13 +94,13 @@ function getArgv() {
77
94
 
78
95
  // json2csv command
79
96
  .command(
80
- "json2csv <input> <output>",
97
+ "json2csv <inputDir> <output>",
81
98
  "Convert locale JSON files to a locale CSV file",
82
99
  (yargs) => {
83
100
  return yargs
84
- .positional("input", {
101
+ .positional("inputDir", {
85
102
  describe:
86
- "Path to locale JSON file(s) (supports multiple files, separated by commas)",
103
+ "Input directory for locale JSON files (all JSON files in the directory will be converted)",
87
104
  type: "string",
88
105
  demandOption: true,
89
106
  })
@@ -94,11 +111,8 @@ function getArgv() {
94
111
  });
95
112
  },
96
113
  (argv) => {
97
- const inputFiles = argv.input.split(","); // Parse multiple input files
98
114
  console.log(
99
- `Converting locale JSON files: ${inputFiles.join(
100
- ", "
101
- )} to locale CSV: ${argv.output}`
115
+ `Converting locale JSON files: ${argv.inputDir} to locale CSV: ${argv.output}`
102
116
  );
103
117
  }
104
118
  )
@@ -128,21 +142,21 @@ function getArgv() {
128
142
  )
129
143
 
130
144
  // 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
- )
145
+ // .command(
146
+ // "generateCsv <output>",
147
+ // "Generate a locale CSV file",
148
+ // (yargs) => {
149
+ // return yargs.positional("output", {
150
+ // describe: "Output path for the locale CSV file",
151
+ // type: "string",
152
+ // // Required
153
+ // demandOption: true,
154
+ // });
155
+ // },
156
+ // (argv) => {
157
+ // console.log(`Generating locale CSV file at: ${argv.output}`);
158
+ // }
159
+ // )
146
160
 
147
161
  // fillJson command
148
162
  .command(
@@ -166,7 +180,61 @@ function getArgv() {
166
180
  `Filling values from the input locale JSON file: ${argv.input} and generating a new locale JSON file: ${argv.output}`
167
181
  );
168
182
  }
169
- ).argv;
183
+ )
184
+
185
+ // separateJson command
186
+ .command(
187
+ "separateJson <inputDir> <outputDir> <separateKey>",
188
+ "Separate json file default and extend key values based on the key",
189
+ (yargs) => {
190
+ return yargs
191
+ .positional("inputDir", {
192
+ describe: "Input directory for locale JSON files",
193
+ type: "string",
194
+ demandOption: true,
195
+ })
196
+ .positional("outputDir", {
197
+ describe: "Output directory for locale JSON files",
198
+ type: "string",
199
+ demandOption: true,
200
+ })
201
+ .positional("separateKey", {
202
+ describe: "Key to separate the json files",
203
+ type: "string",
204
+ demandOption: true,
205
+ });
206
+ },
207
+ (argv) => {
208
+ console.log(
209
+ `Separating json files into multiple files: ${argv.inputDir} to ${argv.outputDir} with key: ${argv.key}`
210
+ );
211
+ }
212
+ )
213
+
214
+ // mergeJson command
215
+ .command(
216
+ "mergeJson <inputDir> <outputDir>",
217
+ "Merge default and extend JSON files back into one file",
218
+ (yargs) => {
219
+ return yargs
220
+ .positional("inputDir", {
221
+ describe:
222
+ "Input directory containing both default and extend JSON files",
223
+ type: "string",
224
+ demandOption: true,
225
+ })
226
+ .positional("outputDir", {
227
+ describe: "Output directory for merged JSON files",
228
+ type: "string",
229
+ demandOption: true,
230
+ });
231
+ },
232
+ (argv) => {
233
+ console.log(
234
+ `Merging JSON files from ${argv.inputDir} to ${argv.outputDir}`
235
+ );
236
+ }
237
+ );
170
238
 
171
- return argv;
239
+ return argv.argv;
172
240
  }