@orderly.network/i18n 3.0.0-beta.0 → 3.0.0-beta.2

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.
Files changed (57) hide show
  1. package/README.md +23 -469
  2. package/dist/{constant-BeXwHrGj.d.mts → constant-DkvDyddr.d.mts} +37 -40
  3. package/dist/{constant-BeXwHrGj.d.ts → constant-DkvDyddr.d.ts} +37 -40
  4. package/dist/constant.d.mts +1 -1
  5. package/dist/constant.d.ts +1 -1
  6. package/dist/constant.js.map +1 -1
  7. package/dist/constant.mjs.map +1 -1
  8. package/dist/index.d.mts +69 -30
  9. package/dist/index.d.ts +69 -30
  10. package/dist/index.js +140 -118
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +132 -115
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/locale.csv +33 -106
  15. package/dist/locales/de.json +33 -38
  16. package/dist/locales/en.json +33 -38
  17. package/dist/locales/es.json +33 -38
  18. package/dist/locales/fr.json +33 -38
  19. package/dist/locales/id.json +33 -38
  20. package/dist/locales/it.json +33 -38
  21. package/dist/locales/ja.json +33 -38
  22. package/dist/locales/ko.json +33 -38
  23. package/dist/locales/nl.json +33 -38
  24. package/dist/locales/pl.json +33 -38
  25. package/dist/locales/pt.json +33 -38
  26. package/dist/locales/ru.json +33 -38
  27. package/dist/locales/tc.json +33 -38
  28. package/dist/locales/tr.json +33 -38
  29. package/dist/locales/uk.json +33 -38
  30. package/dist/locales/vi.json +33 -38
  31. package/dist/locales/zh.json +33 -38
  32. package/dist/utils.d.mts +1 -1
  33. package/dist/utils.d.ts +1 -1
  34. package/dist/utils.js +40 -45
  35. package/dist/utils.js.map +1 -1
  36. package/dist/utils.mjs +40 -45
  37. package/dist/utils.mjs.map +1 -1
  38. package/docs/guide/AGENTS.md +109 -0
  39. package/docs/guide/cli.md +133 -0
  40. package/docs/guide/examples.md +455 -0
  41. package/docs/guide/exports.md +14 -0
  42. package/docs/guide/integration.md +223 -0
  43. package/docs/guide/utils.md +14 -0
  44. package/package.json +8 -6
  45. package/scripts/copyLocales.js +11 -0
  46. package/scripts/csv2json.js +28 -0
  47. package/scripts/diffCsv.js +175 -0
  48. package/scripts/fillJson.js +33 -0
  49. package/scripts/filterLocaleKeys.js +127 -0
  50. package/scripts/generateCsv.js +36 -0
  51. package/scripts/generateEnJson.js +11 -0
  52. package/scripts/generateMissingKeys.js +49 -0
  53. package/scripts/json-csv-converter.js +286 -0
  54. package/scripts/json2csv.js +38 -0
  55. package/scripts/mergeJson.js +67 -0
  56. package/scripts/separateJson.js +50 -0
  57. package/scripts/utils.js +94 -0
package/README.md CHANGED
@@ -2,492 +2,46 @@
2
2
 
3
3
  Internationalization and CLI tools for Orderly SDK. Based on i18next ecosystem.
4
4
 
5
- **Quick start:** Install the package, wrap your app root with `LocaleProvider`, and you get English by default. For multiple locales or custom keys, see the sections below.
5
+ ## Quick start
6
6
 
7
- ## Table of Contents
8
-
9
- - [Integration Guide](#integration-guide)
10
- - [Wrap Your App with LocaleProvider](#1-wrap-your-app-with-localeprovider)
11
- - [Provide Locale Data](#2-provide-locale-data)
12
- - [Extending Locale Files](#3-extending-locale-files)
13
- - [Integrate External Resources](#4-integrate-external-resources)
14
- - [Package exports](#package-exports)
15
- - [Example](#example)
16
- - [Async load locale files](#async-load-locale-files)
17
- - [Sync load locale data](#sync-load-locale-data)
18
- - [Add custom languages](#add-custom-languages)
19
- - [CLI](#cli)
20
- - [Usage](#usage)
21
- - [Commands](#commands)
22
-
23
- ## Package exports
24
-
25
- | Export | Description |
26
- | ---------------------------------- | -------------------------------------------------------------------------------- |
27
- | `@orderly.network/i18n` | Main entry: `LocaleProvider`, hooks, types |
28
- | `@orderly.network/i18n/locales/`\* | Built-in locale JSON files |
29
- | `@orderly.network/i18n/constant` | Constants (e.g. `LocaleEnum`) |
30
- | `@orderly.network/i18n/utils` | Utility functions |
31
- | `@orderly.network/i18n/external` | External integration helpers: `ExternalLocaleProvider`, `preloadDefaultResource` |
32
-
33
- **Hooks:** `useTranslation` and `useLocaleCode` are exported from the main entry; use them as with react-i18next and the locale context (see type definitions or `docs/` for details).
34
-
35
- ## Integration Guide
36
-
37
- Follow these steps to integrate localization support in your app using Orderly SDK:
38
-
39
- ### 1. Wrap Your App with LocaleProvider
40
-
41
- The LocaleProvider is the core component that supplies localized resources to your application. Make sure to wrap your app’s root component with LocaleProvider.
7
+ Install the package, wrap your app root with `LocaleProvider`, and you get English by default.
42
8
 
43
9
  ```tsx
44
10
  import { LocaleProvider } from "@orderly.network/i18n";
45
11
 
46
- <LocaleProvider>
47
- <YourApp />
48
- </LocaleProvider>;
49
- ```
50
-
51
- ### 2. Provide Locale Data
52
-
53
- #### Default Language
54
-
55
- - English (`en`) is included by default.
56
-
57
- #### Supported Locales
58
-
59
- We currently support **17 locales**
60
-
61
- | Locale Code | Language |
62
- | ----------- | ------------------- |
63
- | `en` | English |
64
- | `zh` | Chinese |
65
- | `tc` | Traditional Chinese |
66
- | `ja` | Japanese |
67
- | `es` | Spanish |
68
- | `ko` | Korean |
69
- | `vi` | Vietnamese |
70
- | `de` | German |
71
- | `fr` | French |
72
- | `ru` | Russian |
73
- | `id` | Indonesian |
74
- | `tr` | Turkish |
75
- | `it` | Italian |
76
- | `pt` | Portuguese |
77
- | `uk` | Ukrainian |
78
- | `pl` | Polish |
79
- | `nl` | Dutch |
80
-
81
- #### CSV for Easy Translation
82
-
83
- - Each release generates a `dist/locale.csv` file to simplify translation workflows.
84
- - We provide a CLI tool to convert between CSV and JSON formats.
85
-
86
- ### 3. Extending Locale Files
87
-
88
- You can localize both the SDK UI and your own custom components.
89
-
90
- - When adding custom keys, prefix them with `extend.` to avoid conflicts with default keys.
91
-
92
- ```json
93
- {
94
- "extend.custom.button.label": "My Custom Button"
95
- }
96
- ```
97
-
98
- ### 4. Integrate External Resources
99
-
100
- In more advanced setups, your translations may live outside of this package (for example, in another bundle, a backend service, or a host application). For these cases, use the external integration helpers.
101
-
102
- #### ExternalLocaleProvider
103
-
104
- `ExternalLocaleProvider` lets you register translation resources that come from an external source. It supports both **async** and **sync** resource injection:
105
-
106
- - **Async mode**: pass a function `(lang, ns) => Promise<Record<string, string>>`. It will be called whenever the locale changes, and the returned messages will replace the in-memory bundle for that locale/namespace.
107
- - **Sync mode**: pass a static `Resources` map, similar to `LocaleProvider.resources`, and all bundles will be registered on mount.
108
-
109
- Async example (resources fetched from a host app or backend):
110
-
111
- ```tsx
112
- import { LocaleProvider, LocaleCode, Resources } from "@orderly.network/i18n";
113
- import {
114
- ExternalLocaleProvider,
115
- AsyncResources,
116
- } from "@orderly.network/i18n/external";
117
-
118
- const loadExternalMessages: AsyncResources = async (
119
- lang: LocaleCode,
120
- ns: string,
121
- ) => {
122
- // fetch translations from your host app, CDN, or backend
123
- const res = await fetch(`/i18n/${lang}/${ns}.json`);
124
- return (await res.json()) as Record<string, string>;
125
- };
126
-
127
- export function App() {
128
- return (
129
- <LocaleProvider>
130
- <ExternalLocaleProvider resources={loadExternalMessages}>
131
- <YourApp />
132
- </ExternalLocaleProvider>
133
- </LocaleProvider>
134
- );
135
- }
136
- ```
137
-
138
- Sync example (host app provides a static map of messages):
139
-
140
- ```tsx
141
- import { LocaleProvider, Resources } from "@orderly.network/i18n";
142
- import { ExternalLocaleProvider } from "@orderly.network/i18n/external";
143
-
144
- const externalResources: Resources = {
145
- en: {
146
- "extend.host.title": "Host app title",
147
- },
148
- zh: {
149
- "extend.host.title": "宿主应用标题",
150
- },
151
- };
152
-
153
12
  export function App() {
154
13
  return (
155
14
  <LocaleProvider>
156
- <ExternalLocaleProvider resources={externalResources}>
157
- <YourApp />
158
- </ExternalLocaleProvider>
15
+ <YourApp />
159
16
  </LocaleProvider>
160
17
  );
161
18
  }
162
19
  ```
163
20
 
164
- `ExternalLocaleProvider` renders no UI; it only manages i18n side effects and simply returns its children.
165
-
166
- #### Preload default resources
167
-
168
- `preloadDefaultResource` is a small helper that pre-registers the default language bundle before your React tree renders, which helps avoid a brief flash of raw translation keys:
169
-
170
- ```ts
171
- import { preloadDefaultResource } from "@orderly.network/i18n/external";
172
-
173
- // typically run once during app bootstrap
174
- preloadDefaultResource({
175
- "extend.app.loading": "Loading...",
176
- "extend.app.title": "Orderly App",
177
- });
178
- ```
179
-
180
- It uses a deep merge with overwrite semantics, so subsequent calls will keep the in-memory bundle in sync with your external source of truth.
181
-
182
- ## Example
183
-
184
- You can load locale data in three ways: **async** via `backend.loadPath` (e.g. from your `public/locales`), **sync** by passing preloaded `resources`, or **custom** by defining a `languages` list and per-locale resources.
185
-
186
- ### Async load locale files
187
-
188
- ```typescript
189
- import { FC, ReactNode } from "react";
190
- import { WalletConnectorProvider } from "@orderly.network/wallet-connector";
191
- import { OrderlyAppProvider } from "@orderly.network/react-app";
192
- import { LocaleProvider, LocaleEnum, LocaleCode } from "@orderly.network/i18n";
193
-
194
- const OrderlyProvider: FC<{ children: ReactNode }> = (props) => {
195
- const onLanguageChanged = async (lang: LocaleCode) => {};
196
-
197
- // please copy build-in locale files (@orderly.network/i18n/locales) to you public/locales
198
- // and copy you extend locale files to public/locales/extend
199
- const loadPath = (lang: LocaleCode) => {
200
- if (lang === LocaleEnum.en) {
201
- // because en is built-in, we need to load the en extend only
202
- return `/locales/extend/${lang}.json`;
203
- }
204
- return [`/locales/${lang}.json`, `/locales/extend/${lang}.json`];
205
- };
206
-
207
- return (
208
- <LocaleProvider
209
- onLanguageChanged={onLanguageChanged}
210
- backend={{ loadPath }}
211
- >
212
- <WalletConnectorProvider>
213
- <OrderlyAppProvider
214
- brokerId="orderly"
215
- brokerName="Orderly"
216
- networkId="testnet"
217
- >
218
- {props.children}
219
- </OrderlyAppProvider>
220
- </WalletConnectorProvider>
221
- </LocaleProvider>
222
- );
223
- };
224
- ```
225
-
226
- ### Sync load locale data
227
-
228
- Use this when you bundle locale JSON (e.g. import from `@orderly.network/i18n/locales/*`) and do not need async loading.
229
-
230
- ```typescript
231
- import { FC, ReactNode } from "react";
232
- import { WalletConnectorProvider } from "@orderly.network/wallet-connector";
233
- import { OrderlyAppProvider } from "@orderly.network/react-app";
234
- import { LocaleProvider, LocaleCode, Resources } from "@orderly.network/i18n";
235
- import zh from "@orderly.network/i18n/locales/zh.json";
236
- import ja from "@orderly.network/i18n/locales/ja.json";
237
- import ko from "@orderly.network/i18n/locales/ko.json";
238
-
239
- // extend or overrides English translations
240
- const extendEn = {
241
- "extend.trading": "Trading",
242
- };
243
-
244
- // extend or overrides chinese translations
245
- const extendZh = {
246
- "extend.trading": "交易",
247
- };
248
-
249
- // extend or overrides japanese translations
250
- const extendJa = {
251
- "extend.trading": "取引",
252
- };
253
-
254
- // extend or overrides korean translations
255
- const extendKo = {
256
- "extend.trading": "거래",
257
- };
258
-
259
- // define language resources
260
- const resources: Resources = {
261
- en: extendEn,
262
- zh: {
263
- ...zh,
264
- ...extendZh,
265
- },
266
- ja: {
267
- ...ja,
268
- ...extendJa,
269
- },
270
- ko: {
271
- ...ko,
272
- ...extendKo,
273
- },
274
- };
275
-
276
- const OrderlyProvider: FC<{ children: ReactNode }> = (props) => {
277
- const onLanguageChanged = (locale: LocaleCode) => {};
278
-
279
- return (
280
- <LocaleProvider resources={resources} onLanguageChanged={onLanguageChanged}>
281
- <WalletConnectorProvider>
282
- <OrderlyAppProvider
283
- brokerId="orderly"
284
- brokerName="Orderly"
285
- networkId="testnet"
286
- >
287
- {props.children}
288
- </OrderlyAppProvider>
289
- </WalletConnectorProvider>
290
- </LocaleProvider>
291
- );
292
- };
293
- ```
294
-
295
- ### Add custom languages
296
-
297
- You can restrict the language list and provide only the locales you need via `languages` and `resources`.
298
-
299
- ```typescript
300
- import { FC, ReactNode } from "react";
301
- import { WalletConnectorProvider } from "@orderly.network/wallet-connector";
302
- import { OrderlyAppProvider } from "@orderly.network/react-app";
303
- import {
304
- LocaleProvider,
305
- Resources,
306
- LocaleEnum,
307
- LocaleCode,
308
- Language,
309
- } from "@orderly.network/i18n";
310
-
311
- // japanese locale
312
- const ja = {
313
- "extend.ja": "日本語",
314
- };
315
-
316
- // korean locale
317
- const ko = {
318
- "extend.ko": "한국어",
319
- };
320
-
321
- // define language resources
322
- const resources: Resources = {
323
- ja,
324
- ko,
325
- };
326
-
327
- // custom languages
328
- const languages: Language[] = [
329
- { localCode: LocaleEnum.en, displayName: "English" },
330
- { localCode: LocaleEnum.ja, displayName: "日本語" },
331
- { localCode: LocaleEnum.ko, displayName: "한국어" },
332
- ];
333
-
334
- const OrderlyProvider: FC<{ children: ReactNode }> = (props) => {
335
- const onLanguageChanged = (locale: LocaleCode) => {};
336
-
337
- return (
338
- <LocaleProvider
339
- resources={resources}
340
- languages={languages}
341
- onLanguageChanged={onLanguageChanged}
342
- >
343
- <WalletConnectorProvider>
344
- <OrderlyAppProvider
345
- brokerId="orderly"
346
- brokerName="Orderly"
347
- networkId="testnet"
348
- >
349
- {props.children}
350
- </OrderlyAppProvider>
351
- </WalletConnectorProvider>
352
- </LocaleProvider>
353
- );
354
- };
355
- ```
356
-
357
- ## CLI
358
-
359
- ### Usage
360
-
361
- ```bash
362
- npx @orderly.network/i18n <command> [options]
363
- ```
364
-
365
- The CLI manages locale files: CSV/JSON conversion, diff, merge, and key filtering. Commands are listed below.
366
-
367
- ### Commands
368
-
369
- ### csv2json
370
-
371
- Convert a locale CSV file to multiple locale JSON files.
372
-
373
- ```bash
374
- npx @orderly.network/i18n csv2json <input> <outputDir>
375
- ```
376
-
377
- Example:
378
-
379
- ```bash
380
- npx @orderly.network/i18n csv2json ./dist/locale.csv ./dist/locales
381
- ```
382
-
383
- ### json2csv
384
-
385
- Convert multiple locale JSON files from any directory into a single locale CSV file (generic JSON → CSV).
386
-
387
- ```bash
388
- npx @orderly.network/i18n json2csv <inputDir> <output>
389
- ```
390
-
391
- Example:
392
-
393
- ```bash
394
- npx @orderly.network/i18n json2csv ./locales ./dist/locale.csv
395
- ```
396
-
397
- ### diffcsv
398
-
399
- Compare two locale CSV files to find differences.
400
-
401
- ```bash
402
- npx @orderly.network/i18n diffcsv <oldFile> <newFile>
403
- ```
404
-
405
- Example:
406
-
407
- ```bash
408
- npx @orderly.network/i18n diffcsv ./dist/locale1.csv ./dist/locale2.csv
409
- ```
410
-
411
- ### generateCsv
21
+ For multiple locales, custom keys, Vite bundling, HTTP loading, or the CLI, see the **Documentation** links below.
412
22
 
413
- Generate a locale CSV file from locale JSON files. Typically used for the package’s own `locales` directory (e.g. when building or releasing).
23
+ ## Documentation
414
24
 
415
- ```bash
416
- npx @orderly.network/i18n generateCsv <inputDir> <output>
417
- ```
25
+ | Topic | Link |
26
+ | ----------------------------------------------------------------- | -------------------------------------------------------- |
27
+ | AI / Agent integration | [docs/guide/AGENTS.md](./docs/guide/AGENTS.md) |
28
+ | Package exports | [docs/guide/exports.md](./docs/guide/exports.md) |
29
+ | Integration (providers, locales, extend keys, external resources) | [docs/guide/integration.md](./docs/guide/integration.md) |
30
+ | Utils (paths and locale helpers) | [docs/guide/utils.md](./docs/guide/utils.md) |
31
+ | Examples (recommended AsyncResources + alternatives) | [docs/guide/examples.md](./docs/guide/examples.md) |
32
+ | CLI (`i18n` binary) | [docs/guide/cli.md](./docs/guide/cli.md) |
418
33
 
419
- Example:
34
+ **Suggested reading order:** Agents start with [AGENTS](./docs/guide/AGENTS.md). Otherwise: [Integration](./docs/guide/integration.md) (API and behavior) → [Examples](./docs/guide/examples.md) (copy-paste recipes as needed) → [CLI](./docs/guide/cli.md) / [Utils](./docs/guide/utils.md) when relevant.
420
35
 
421
- ```bash
422
- npx @orderly.network/i18n generateCsv ./locales ./dist/locale.csv
423
- ```
36
+ Generated API notes for `src/` live under `packages/i18n/docs/` in the monorepo only (not published in the npm package). The **user guide** under `docs/guide/` is included in the package so links above work after `npm install`.
424
37
 
425
- ### fillJson
38
+ ### Package exports (summary)
426
39
 
427
- Build a new locale JSON with the same keys as the built-in English set, filling values from the input file (missing keys become empty). Useful for filling missing keys in a locale from another language or template.
428
-
429
- ```bash
430
- npx @orderly.network/i18n fillJson <input> <output>
431
- ```
40
+ | Export | Description |
41
+ | --------------------------------- | ------------------------------------------------------------------------------------------------------------ |
42
+ | `@orderly.network/i18n` | Main entry: providers, hooks, `i18n`, resource helpers, types — see [exports guide](./docs/guide/exports.md) |
43
+ | `@orderly.network/i18n/locales/*` | Built-in locale JSON files |
44
+ | `@orderly.network/i18n/constant` | Constants (`LocaleEnum`, `defaultLanguages`, …) |
45
+ | `@orderly.network/i18n/utils` | Utilities — see [Utils](./docs/guide/utils.md) |
432
46
 
433
- Example:
434
-
435
- ```bash
436
- npx @orderly.network/i18n fillJson ./src/locale/zh.json ./dist/locale/zh.json
437
- ```
438
-
439
- ### separateJson
440
-
441
- Split each locale JSON by key prefix: keys starting with `separateKey.` go into an `extend/` subdirectory under `outputDir`, the rest stay at the root of `outputDir`.
442
-
443
- ```bash
444
- npx @orderly.network/i18n separateJson <inputDir> <outputDir> <separateKey>
445
- ```
446
-
447
- Example:
448
-
449
- ```bash
450
- npx @orderly.network/i18n separateJson ./locales ./dist/locales extend
451
- ```
452
-
453
- ### mergeJson
454
-
455
- Merge default and extend JSON files from a single input directory into one file per locale. The input directory must contain both the main locale JSON files and an `extend/` subdirectory with matching locale files; merged output is written to `outputDir`.
456
-
457
- ```bash
458
- npx @orderly.network/i18n mergeJson <inputDir> <outputDir>
459
- ```
460
-
461
- Example:
462
-
463
- ```bash
464
- npx @orderly.network/i18n mergeJson ./locales ./dist/locales
465
- ```
466
-
467
- ### filterKeys
468
-
469
- Filter locale JSON keys by prefix: keep only keys matching the prefix, or remove keys matching the prefix. Operates on `packages/i18n/locales` by default (or a custom directory via `--locales-dir`). You must specify exactly one of `--keep` or `--remove`.
470
-
471
- ```bash
472
- npx @orderly.network/i18n filterKeys (--keep | -k | --remove | -r) --prefix <prefix> [--locales-dir <dir>]
473
- ```
474
-
475
- Options:
476
-
477
- - `--keep`, `-k` — Keep only keys that start with the given prefix.
478
- - `--remove`, `-r` — Remove keys that start with the given prefix.
479
- - `--prefix <prefix>` — (Required) Key prefix to match (e.g. `trading.` or `trading`).
480
- - `--locales-dir <dir>` — Directory containing locale JSON files (default: `packages/i18n/locales`).
481
-
482
- Examples:
483
-
484
- ```bash
485
- # Keep only keys with prefix "trading."
486
- npx @orderly.network/i18n filterKeys --keep --prefix trading.
487
-
488
- # Remove keys with prefix "trading." (short form)
489
- npx @orderly.network/i18n filterKeys -r --prefix trading.
490
-
491
- # Use a custom locales directory
492
- npx @orderly.network/i18n filterKeys -k --prefix extend. --locales-dir ./my-locales
493
- ```
47
+ The default i18n namespace is **`translation`** (`defaultNS`).