@sheet-i18n/react 1.2.1 → 1.3.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 +49 -82
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,27 +1,20 @@
|
|
|
1
|
-
# @sheet-i18n/react ✨
|
|
2
|
-
|
|
3
|
-
[](https://npmjs.com/package/@sheet-i18n/react)
|
|
4
|
-
|
|
5
1
|
The **client-side i18n library** subpackage of sheet-i18n.
|
|
6
2
|
|
|
7
3
|
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.
|
|
8
4
|
|
|
9
5
|
## ✨ Package Introduction
|
|
10
6
|
|
|
11
|
-
- **`I18nStore`**:
|
|
7
|
+
- **`I18nStore`**: _Store Creation Class_ for managing translation data.
|
|
12
8
|
- **`createI18nContext`**: _React Context_ to generate providers and hooks for translation.
|
|
13
9
|
- **`IntlProvider`**: _React Translation Provider_ for managing current locale.
|
|
14
10
|
- **`useTranslation`**: _Client Side Translation Hook_ for easy access to translation messages on the client side.
|
|
15
11
|
- **`getTranslation`**: _Static Translation Function_ for easy access to translation messages on Static module files.
|
|
16
12
|
|
|
17
|
-
## 🚀 Getting Started
|
|
13
|
+
## 🚀 Getting Started
|
|
18
14
|
|
|
19
|
-
|
|
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.
|
|
15
|
+
### Basic Usage
|
|
23
16
|
|
|
24
|
-
|
|
17
|
+
#### 1. Define Translation Data
|
|
25
18
|
|
|
26
19
|
Prepare locale JSON files:
|
|
27
20
|
|
|
@@ -43,7 +36,7 @@ Prepare locale JSON files:
|
|
|
43
36
|
}
|
|
44
37
|
```
|
|
45
38
|
|
|
46
|
-
|
|
39
|
+
#### 2. Initialize i18nStore
|
|
47
40
|
|
|
48
41
|
this store will be used as a core translations module.
|
|
49
42
|
|
|
@@ -67,13 +60,11 @@ export const i18nStore = new I18nStore({
|
|
|
67
60
|
// dynamicLoaders: {
|
|
68
61
|
// ko: () => import('./ko.json'),
|
|
69
62
|
// en: () => import('./en.json'),
|
|
70
|
-
// jp: () => import('./jp.json'),
|
|
71
|
-
// cn: () => import('./cn.json'),
|
|
72
63
|
// },
|
|
73
64
|
});
|
|
74
65
|
```
|
|
75
66
|
|
|
76
|
-
|
|
67
|
+
#### 3. Create i18n Context
|
|
77
68
|
|
|
78
69
|
```tsx
|
|
79
70
|
import { i18nStore } from './file-path-of-i18nStore-initiated';
|
|
@@ -83,24 +74,22 @@ export const { IntlProvider, useTranslation, getTranslation } =
|
|
|
83
74
|
createI18nContext(i18nStore);
|
|
84
75
|
```
|
|
85
76
|
|
|
86
|
-
|
|
77
|
+
#### 4. Mount Intl Context Provider in your App
|
|
87
78
|
|
|
88
79
|
```tsx
|
|
89
80
|
import React from 'react';
|
|
90
81
|
import { IntlProvider } from './i18nContext';
|
|
91
82
|
|
|
92
83
|
const App = () => {
|
|
93
|
-
const [locale, setLocale] = useState('en');
|
|
94
|
-
|
|
95
84
|
return (
|
|
96
|
-
<IntlProvider
|
|
85
|
+
<IntlProvider>
|
|
97
86
|
<YourComponent />
|
|
98
87
|
</IntlProvider>
|
|
99
88
|
);
|
|
100
89
|
};
|
|
101
90
|
```
|
|
102
91
|
|
|
103
|
-
|
|
92
|
+
#### 5. Use Translations
|
|
104
93
|
|
|
105
94
|
```tsx
|
|
106
95
|
import React from 'react';
|
|
@@ -124,29 +113,27 @@ const YourComponent = () => {
|
|
|
124
113
|
|
|
125
114
|
### `I18nStore(core)`
|
|
126
115
|
|
|
127
|
-
The `I18nStore` manages translation states, ensuring consistency across locales.
|
|
116
|
+
The `I18nStore` manages type-safe translation states, ensuring consistency across locales.
|
|
128
117
|
|
|
129
118
|
#### Parameters:
|
|
130
119
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
120
|
+
- **`supportedLocales`**: Array of supported locale strings.
|
|
121
|
+
<br/>
|
|
122
|
+
<br/>
|
|
123
|
+
- **`defaultLocale`**: The default locale, included in `supportedLocales`.
|
|
124
|
+
<br/>
|
|
125
|
+
<br/>
|
|
126
|
+
- **`localeSet(optional. choice 1)`**: An object where keys match `supportedLocales`, and values are translation sets. If you want to use this option, you need to provide all static locale data according to the `supportedLocales`.
|
|
127
|
+
<br/>
|
|
128
|
+
<br/>
|
|
129
|
+
- **`dynamicLoaders(optional. choice 2)`**: An object where keys match `supportedLocales`, and values are dynamic translation loader functions.
|
|
130
|
+
<br/>
|
|
131
|
+
<br/>
|
|
132
|
+
- **`typeSafe(optional, default = false)`**: An optional boolean indicating whether to use type-safe translations.
|
|
141
133
|
|
|
142
134
|
> 💡 **typeSafe?** <br/>
|
|
143
135
|
> 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.
|
|
144
136
|
>
|
|
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
|
-
|
|
150
137
|
> ```tsx
|
|
151
138
|
> // typeSafe: true
|
|
152
139
|
> const YourComponent = () => {
|
|
@@ -161,47 +148,12 @@ supportedLocales in i18nStore`
|
|
|
161
148
|
> );
|
|
162
149
|
> };
|
|
163
150
|
> ```
|
|
164
|
-
|
|
151
|
+
|
|
165
152
|
> ⚠️ Caveats:
|
|
166
153
|
>
|
|
167
154
|
> 1. `supportedLocales` must be an array of locale strings.
|
|
168
155
|
> 2. `defaultLocale` must exist in `supportedLocales`.
|
|
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.
|
|
156
|
+
> 3. `localeSet` must be an object with keys matching `supportedLocales`.
|
|
205
157
|
|
|
206
158
|
#### Example:
|
|
207
159
|
|
|
@@ -219,8 +171,6 @@ export const i18nStore = new I18nStore({
|
|
|
219
171
|
// dynamicLoaders: {
|
|
220
172
|
// ko: () => import('./ko.json'),
|
|
221
173
|
// en: () => import('./en.json'),
|
|
222
|
-
// jp: () => import('./jp.json'),
|
|
223
|
-
// cn: () => import('./cn.json'),
|
|
224
174
|
// },
|
|
225
175
|
});
|
|
226
176
|
```
|
|
@@ -254,7 +204,7 @@ A Context provider to provide translations to child components.
|
|
|
254
204
|
|
|
255
205
|
- A key representing the current locale to use for translations.
|
|
256
206
|
- If not provided, the user's preferred language is automatically detected based on the browser setting.
|
|
257
|
-
- The fallback is the default locale in **i18nStore** if the detected
|
|
207
|
+
- The fallback is the default locale in **i18nStore** if the detected language is unsupported.
|
|
258
208
|
|
|
259
209
|
- **`children`**: React children.
|
|
260
210
|
|
|
@@ -390,7 +340,7 @@ It provide two possible supports
|
|
|
390
340
|
|
|
391
341
|
#### **✅ Usage Scenarios**
|
|
392
342
|
|
|
393
|
-
#### **
|
|
343
|
+
#### **Scenario 1: Context where the translation value is evaluated at runtime**
|
|
394
344
|
|
|
395
345
|
- For the common example, **t** call expression in a **function module**
|
|
396
346
|
- This behaves the same as `useTranslation`'s `t` function.
|
|
@@ -418,7 +368,7 @@ export default function App() {
|
|
|
418
368
|
|
|
419
369
|
**Result:** The translation is resolved **immediately** during runtime.
|
|
420
370
|
|
|
421
|
-
####
|
|
371
|
+
#### **⚠️ Scenario 2: Context where the translation value is evaluated immediately**
|
|
422
372
|
|
|
423
373
|
- If the module is **imported dynamically in a client-side component**, the evaluation order of `getTranslation` and `t` call expression may not be guaranteed.
|
|
424
374
|
- Since JavaScript **evaluates modules at import time**, it may attempt to access translation values before `IntlProvider` has fully initialized the locale.
|
|
@@ -454,10 +404,27 @@ export default function App() {
|
|
|
454
404
|
}
|
|
455
405
|
```
|
|
456
406
|
|
|
457
|
-
##
|
|
407
|
+
## 🛠 Error Handling
|
|
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
|
|
458
423
|
|
|
459
|
-
This project is licensed under the ISC License.
|
|
424
|
+
This project is licensed under the ISC License.
|
|
460
425
|
|
|
461
|
-
## Author
|
|
426
|
+
## 👤 Author
|
|
462
427
|
|
|
463
|
-
|
|
428
|
+
**devAnderson**
|
|
429
|
+
[GitHub](https://github.com/chltjdrhd777)
|
|
430
|
+
[chltjdrhd777@gmail.com](mailto:chltjdrhd777@gmail.com)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sheet-i18n/react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.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.6.0"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@sheet-i18n/react-core": "1.
|
|
36
|
-
"@sheet-i18n/react-client": "1.
|
|
35
|
+
"@sheet-i18n/react-core": "1.3.0",
|
|
36
|
+
"@sheet-i18n/react-client": "1.3.0"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsup",
|