@sheet-i18n/react 1.3.0 → 1.4.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 +82 -49
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,20 +1,27 @@
|
|
|
1
|
+
# @sheet-i18n/react ✨
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.com/package/@sheet-i18n/react)
|
|
4
|
+
|
|
1
5
|
The **client-side i18n library** subpackage of sheet-i18n.
|
|
2
6
|
|
|
3
7
|
This package provides tools to handle translations in React applications using context and hooks. It simplifies internationalization workflows by offering functions and components to manage, access, and use locale-specific translation data.
|
|
4
8
|
|
|
5
9
|
## ✨ Package Introduction
|
|
6
10
|
|
|
7
|
-
- **`I18nStore`**:
|
|
11
|
+
- **`I18nStore`**: _Core Store Creation Class_ for managing translation data.
|
|
8
12
|
- **`createI18nContext`**: _React Context_ to generate providers and hooks for translation.
|
|
9
13
|
- **`IntlProvider`**: _React Translation Provider_ for managing current locale.
|
|
10
14
|
- **`useTranslation`**: _Client Side Translation Hook_ for easy access to translation messages on the client side.
|
|
11
15
|
- **`getTranslation`**: _Static Translation Function_ for easy access to translation messages on Static module files.
|
|
12
16
|
|
|
13
|
-
## 🚀 Getting Started
|
|
17
|
+
## 🚀 Getting Started(Manually)
|
|
14
18
|
|
|
15
|
-
|
|
19
|
+
> ⚡ **Strongly recommended to use the init CLI for setup**
|
|
20
|
+
> 👉 [Please follow the Init CLI section](#initial-setup-anchor)
|
|
21
|
+
>
|
|
22
|
+
> If you don't want to use the CLI, you can follow the `Manual Setup` below.
|
|
16
23
|
|
|
17
|
-
|
|
24
|
+
### step 1. Define Translation Data
|
|
18
25
|
|
|
19
26
|
Prepare locale JSON files:
|
|
20
27
|
|
|
@@ -36,7 +43,7 @@ Prepare locale JSON files:
|
|
|
36
43
|
}
|
|
37
44
|
```
|
|
38
45
|
|
|
39
|
-
|
|
46
|
+
### step 2. Initialize i18nStore
|
|
40
47
|
|
|
41
48
|
this store will be used as a core translations module.
|
|
42
49
|
|
|
@@ -60,11 +67,13 @@ export const i18nStore = new I18nStore({
|
|
|
60
67
|
// dynamicLoaders: {
|
|
61
68
|
// ko: () => import('./ko.json'),
|
|
62
69
|
// en: () => import('./en.json'),
|
|
70
|
+
// jp: () => import('./jp.json'),
|
|
71
|
+
// cn: () => import('./cn.json'),
|
|
63
72
|
// },
|
|
64
73
|
});
|
|
65
74
|
```
|
|
66
75
|
|
|
67
|
-
|
|
76
|
+
### step 3. Create i18n Context
|
|
68
77
|
|
|
69
78
|
```tsx
|
|
70
79
|
import { i18nStore } from './file-path-of-i18nStore-initiated';
|
|
@@ -74,22 +83,24 @@ export const { IntlProvider, useTranslation, getTranslation } =
|
|
|
74
83
|
createI18nContext(i18nStore);
|
|
75
84
|
```
|
|
76
85
|
|
|
77
|
-
|
|
86
|
+
### step 4. Mount Intl Context Provider in your App
|
|
78
87
|
|
|
79
88
|
```tsx
|
|
80
89
|
import React from 'react';
|
|
81
90
|
import { IntlProvider } from './i18nContext';
|
|
82
91
|
|
|
83
92
|
const App = () => {
|
|
93
|
+
const [locale, setLocale] = useState('en');
|
|
94
|
+
|
|
84
95
|
return (
|
|
85
|
-
<IntlProvider>
|
|
96
|
+
<IntlProvider currentLocale={locale}>
|
|
86
97
|
<YourComponent />
|
|
87
98
|
</IntlProvider>
|
|
88
99
|
);
|
|
89
100
|
};
|
|
90
101
|
```
|
|
91
102
|
|
|
92
|
-
|
|
103
|
+
### step 5. Use Translations
|
|
93
104
|
|
|
94
105
|
```tsx
|
|
95
106
|
import React from 'react';
|
|
@@ -113,27 +124,29 @@ const YourComponent = () => {
|
|
|
113
124
|
|
|
114
125
|
### `I18nStore(core)`
|
|
115
126
|
|
|
116
|
-
The `I18nStore` manages
|
|
127
|
+
The `I18nStore` manages translation states, ensuring consistency across locales.
|
|
117
128
|
|
|
118
129
|
#### Parameters:
|
|
119
130
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
<br/>
|
|
131
|
-
<br/>
|
|
132
|
-
- **`typeSafe(optional, default = false)`**: An optional boolean indicating whether to use type-safe translations.
|
|
131
|
+
### 🧠 I18nStore Configuration Options
|
|
132
|
+
|
|
133
|
+
| Option | Type | Required | Description |
|
|
134
|
+
| ------------------ | ---------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------- |
|
|
135
|
+
| `supportedLocales` | string[] | ✅ | List of supported locale codes. |
|
|
136
|
+
| `defaultLocale` | string | ✅ | Default locale to use when no match is found. Must be included in `supportedLocales`. |
|
|
137
|
+
| localeSet | Record<string, object> | | _(Static Loading Option)_ Preload all translation data for each locale in memory. Keys must match `supportedLocales`. |
|
|
138
|
+
| dynamicLoaders | Record<string, () => Promise<any>> | | _(Recommended for large locale sets)_ Dynamically load translation data on demand, reducing initial bundle size. |
|
|
139
|
+
| typeSafe | boolean | | Enable strict key checking and autocompletion (default: `false`). |
|
|
140
|
+
| |
|
|
133
141
|
|
|
134
142
|
> 💡 **typeSafe?** <br/>
|
|
135
143
|
> I18nStore doesn't enforce adherence to your locale JSON definitions by default. This means that you can add translation data even if it isn’t pre-defined in your locale JSON files. However, if you prefer to enforce strict type-safety, you can manually enable the typeSafe option which allows you to notice the auto-completed list in translation data.
|
|
136
144
|
>
|
|
145
|
+
> But to enable correct type checking, `the full localeSet must be defined according to the
|
|
146
|
+
supportedLocales in i18nStore`
|
|
147
|
+
>
|
|
148
|
+
> As a result, type auto-completion relies on having the complete localeSet defined at initialization. This means that using dynamicLoaders to fetch locales conditionally at runtime can be limiting when strict type-safety is enabled.
|
|
149
|
+
|
|
137
150
|
> ```tsx
|
|
138
151
|
> // typeSafe: true
|
|
139
152
|
> const YourComponent = () => {
|
|
@@ -148,12 +161,47 @@ The `I18nStore` manages type-safe translation states, ensuring consistency acros
|
|
|
148
161
|
> );
|
|
149
162
|
> };
|
|
150
163
|
> ```
|
|
151
|
-
|
|
164
|
+
>
|
|
152
165
|
> ⚠️ Caveats:
|
|
153
166
|
>
|
|
154
167
|
> 1. `supportedLocales` must be an array of locale strings.
|
|
155
168
|
> 2. `defaultLocale` must exist in `supportedLocales`.
|
|
156
|
-
|
|
169
|
+
|
|
170
|
+
### 🚀 Static vs Dynamic Loading
|
|
171
|
+
|
|
172
|
+
You can configure how translations are loaded:
|
|
173
|
+
|
|
174
|
+
- **Static (`localeSet`)**
|
|
175
|
+
Load all translations at once. Ideal for small projects or limited locale sets.
|
|
176
|
+
|
|
177
|
+
- **Dynamic (`dynamicLoaders`)** ✅ _Recommended_
|
|
178
|
+
Load only the locale needed, reducing the bundle size dramatically.
|
|
179
|
+
Useful when supporting many languages or large translation files.
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
export const i18nStore = new I18nStore({
|
|
183
|
+
supportedLocales: ['en', 'fr', 'ko'],
|
|
184
|
+
defaultLocale: 'en',
|
|
185
|
+
dynamicLoaders: {
|
|
186
|
+
en: () => import('./en.json'),
|
|
187
|
+
fr: () => import('./fr.json'),
|
|
188
|
+
ko: () => import('./ko.json'),
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
💡 When both `localeSet` and `dynamicLoaders` are defined, sheet-i18n **automatically merges** both sources — static data loads immediately, while dynamic data supplements on-demand.
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
### 🎯 Why Dynamic Loaders?
|
|
198
|
+
|
|
199
|
+
For projects with many locale datasets, it's often preferable to load translation data on demand rather than all at once. It is better using `dynamicLoaders` to enable this conditional loading strategy.
|
|
200
|
+
|
|
201
|
+
- ✅ Reduces the initial JavaScript bundle size.
|
|
202
|
+
- ✅ Keeps only required locales in memory.
|
|
203
|
+
- ✅ Works well with code-splitting and SSR/CSR.
|
|
204
|
+
- ✅ Enables **lazy-loading** for translations.
|
|
157
205
|
|
|
158
206
|
#### Example:
|
|
159
207
|
|
|
@@ -171,6 +219,8 @@ export const i18nStore = new I18nStore({
|
|
|
171
219
|
// dynamicLoaders: {
|
|
172
220
|
// ko: () => import('./ko.json'),
|
|
173
221
|
// en: () => import('./en.json'),
|
|
222
|
+
// jp: () => import('./jp.json'),
|
|
223
|
+
// cn: () => import('./cn.json'),
|
|
174
224
|
// },
|
|
175
225
|
});
|
|
176
226
|
```
|
|
@@ -204,7 +254,7 @@ A Context provider to provide translations to child components.
|
|
|
204
254
|
|
|
205
255
|
- A key representing the current locale to use for translations.
|
|
206
256
|
- If not provided, the user's preferred language is automatically detected based on the browser setting.
|
|
207
|
-
- The fallback is the default locale in **i18nStore** if the detected language is unsupported
|
|
257
|
+
- The fallback is the default locale in **i18nStore** if the detected user-preferred language is not unsupported in the `supportedLocales`.
|
|
208
258
|
|
|
209
259
|
- **`children`**: React children.
|
|
210
260
|
|
|
@@ -340,7 +390,7 @@ It provide two possible supports
|
|
|
340
390
|
|
|
341
391
|
#### **✅ Usage Scenarios**
|
|
342
392
|
|
|
343
|
-
#### **Scenario 1: Context where the
|
|
393
|
+
#### **[Scenario 1]: Context where the call expression of `t` function is evaluated at runtime**
|
|
344
394
|
|
|
345
395
|
- For the common example, **t** call expression in a **function module**
|
|
346
396
|
- This behaves the same as `useTranslation`'s `t` function.
|
|
@@ -368,7 +418,7 @@ export default function App() {
|
|
|
368
418
|
|
|
369
419
|
**Result:** The translation is resolved **immediately** during runtime.
|
|
370
420
|
|
|
371
|
-
####
|
|
421
|
+
#### **[Scenario 2]: Context where the translation value is evaluated immediately**
|
|
372
422
|
|
|
373
423
|
- If the module is **imported dynamically in a client-side component**, the evaluation order of `getTranslation` and `t` call expression may not be guaranteed.
|
|
374
424
|
- Since JavaScript **evaluates modules at import time**, it may attempt to access translation values before `IntlProvider` has fully initialized the locale.
|
|
@@ -404,27 +454,10 @@ export default function App() {
|
|
|
404
454
|
}
|
|
405
455
|
```
|
|
406
456
|
|
|
407
|
-
##
|
|
408
|
-
|
|
409
|
-
Custom error messages help identify misconfigurations:
|
|
410
|
-
|
|
411
|
-
1. **Invalid Params**: Ensure `supportedLocales`, `defaultLocale`, and `localeSet` are correctly provided.
|
|
412
|
-
2. **Missing Default Locale**: The `defaultLocale` must exist in `supportedLocales`.
|
|
413
|
-
3. **Invalid LocaleSet**: Ensure the `localeSet` keys match `supportedLocales`.
|
|
414
|
-
|
|
415
|
-
## 📜 Library Versions 🔢
|
|
416
|
-
|
|
417
|
-
This package supports the following library versions:
|
|
418
|
-
|
|
419
|
-
- **React**: `^19.0.0`
|
|
420
|
-
- **React Intl**: `^7.0.4`
|
|
421
|
-
|
|
422
|
-
## 📜 License
|
|
457
|
+
## License 📜
|
|
423
458
|
|
|
424
|
-
This project is licensed under the ISC License.
|
|
459
|
+
This project is licensed under the ISC License. See the LICENSE file for details.
|
|
425
460
|
|
|
426
|
-
##
|
|
461
|
+
## Author ✍️
|
|
427
462
|
|
|
428
|
-
|
|
429
|
-
[GitHub](https://github.com/chltjdrhd777)
|
|
430
|
-
[chltjdrhd777@gmail.com](mailto:chltjdrhd777@gmail.com)
|
|
463
|
+
Created by [devAnderson](https://github.com/chltjdrhd777).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sheet-i18n/react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "i18n client logic based on react",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -29,11 +29,11 @@
|
|
|
29
29
|
"license": "ISC",
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@swc/core": "^1.10.2",
|
|
32
|
-
"@sheet-i18n/typescript-config": "1.
|
|
32
|
+
"@sheet-i18n/typescript-config": "1.7.0"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@sheet-i18n/react-core": "1.
|
|
36
|
-
"@sheet-i18n/react-client": "1.
|
|
35
|
+
"@sheet-i18n/react-core": "1.4.0",
|
|
36
|
+
"@sheet-i18n/react-client": "1.4.0"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsup",
|