@sheet-i18n/react 1.2.0-canary.0 → 1.2.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 +97 -44
- 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
|
|
|
@@ -49,14 +56,24 @@ import { I18nStore } from '@sheet-i18n/react';
|
|
|
49
56
|
export const i18nStore = new I18nStore({
|
|
50
57
|
supportedLocales: ['ko', 'en'],
|
|
51
58
|
defaultLocale: 'ko',
|
|
59
|
+
|
|
60
|
+
/** if you want to load translation data statically */
|
|
52
61
|
localeSet: {
|
|
53
62
|
ko,
|
|
54
63
|
en,
|
|
55
64
|
},
|
|
65
|
+
|
|
66
|
+
/** if you want to load translation data dynamically */
|
|
67
|
+
// dynamicLoaders: {
|
|
68
|
+
// ko: () => import('./ko.json'),
|
|
69
|
+
// en: () => import('./en.json'),
|
|
70
|
+
// jp: () => import('./jp.json'),
|
|
71
|
+
// cn: () => import('./cn.json'),
|
|
72
|
+
// },
|
|
56
73
|
});
|
|
57
74
|
```
|
|
58
75
|
|
|
59
|
-
|
|
76
|
+
### step 3. Create i18n Context
|
|
60
77
|
|
|
61
78
|
```tsx
|
|
62
79
|
import { i18nStore } from './file-path-of-i18nStore-initiated';
|
|
@@ -66,22 +83,24 @@ export const { IntlProvider, useTranslation, getTranslation } =
|
|
|
66
83
|
createI18nContext(i18nStore);
|
|
67
84
|
```
|
|
68
85
|
|
|
69
|
-
|
|
86
|
+
### step 4. Mount Intl Context Provider in your App
|
|
70
87
|
|
|
71
88
|
```tsx
|
|
72
89
|
import React from 'react';
|
|
73
90
|
import { IntlProvider } from './i18nContext';
|
|
74
91
|
|
|
75
92
|
const App = () => {
|
|
93
|
+
const [locale, setLocale] = useState('en');
|
|
94
|
+
|
|
76
95
|
return (
|
|
77
|
-
<IntlProvider>
|
|
96
|
+
<IntlProvider currentLocale={locale}>
|
|
78
97
|
<YourComponent />
|
|
79
98
|
</IntlProvider>
|
|
80
99
|
);
|
|
81
100
|
};
|
|
82
101
|
```
|
|
83
102
|
|
|
84
|
-
|
|
103
|
+
### step 5. Use Translations
|
|
85
104
|
|
|
86
105
|
```tsx
|
|
87
106
|
import React from 'react';
|
|
@@ -105,18 +124,29 @@ const YourComponent = () => {
|
|
|
105
124
|
|
|
106
125
|
### `I18nStore(core)`
|
|
107
126
|
|
|
108
|
-
The `I18nStore` manages
|
|
127
|
+
The `I18nStore` manages translation states, ensuring consistency across locales.
|
|
109
128
|
|
|
110
129
|
#### Parameters:
|
|
111
130
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
+
| |
|
|
116
141
|
|
|
117
142
|
> 💡 **typeSafe?** <br/>
|
|
118
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.
|
|
119
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
|
+
|
|
120
150
|
> ```tsx
|
|
121
151
|
> // typeSafe: true
|
|
122
152
|
> const YourComponent = () => {
|
|
@@ -131,12 +161,47 @@ The `I18nStore` manages type-safe translation states, ensuring consistency acros
|
|
|
131
161
|
> );
|
|
132
162
|
> };
|
|
133
163
|
> ```
|
|
134
|
-
|
|
164
|
+
>
|
|
135
165
|
> ⚠️ Caveats:
|
|
136
166
|
>
|
|
137
167
|
> 1. `supportedLocales` must be an array of locale strings.
|
|
138
168
|
> 2. `defaultLocale` must exist in `supportedLocales`.
|
|
139
|
-
|
|
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.
|
|
140
205
|
|
|
141
206
|
#### Example:
|
|
142
207
|
|
|
@@ -144,15 +209,20 @@ The `I18nStore` manages type-safe translation states, ensuring consistency acros
|
|
|
144
209
|
export const i18nStore = new I18nStore({
|
|
145
210
|
supportedLocales: ['ko', 'en'],
|
|
146
211
|
defaultLocale: 'ko',
|
|
212
|
+
/** if you want to load translation data statically */
|
|
147
213
|
localeSet: {
|
|
148
214
|
ko,
|
|
149
215
|
en,
|
|
150
216
|
},
|
|
151
|
-
});
|
|
152
217
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
218
|
+
/** if you want to load translation data dynamically */
|
|
219
|
+
// dynamicLoaders: {
|
|
220
|
+
// ko: () => import('./ko.json'),
|
|
221
|
+
// en: () => import('./en.json'),
|
|
222
|
+
// jp: () => import('./jp.json'),
|
|
223
|
+
// cn: () => import('./cn.json'),
|
|
224
|
+
// },
|
|
225
|
+
});
|
|
156
226
|
```
|
|
157
227
|
|
|
158
228
|
### `createI18nContext`
|
|
@@ -184,7 +254,7 @@ A Context provider to provide translations to child components.
|
|
|
184
254
|
|
|
185
255
|
- A key representing the current locale to use for translations.
|
|
186
256
|
- If not provided, the user's preferred language is automatically detected based on the browser setting.
|
|
187
|
-
- 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`.
|
|
188
258
|
|
|
189
259
|
- **`children`**: React children.
|
|
190
260
|
|
|
@@ -320,7 +390,7 @@ It provide two possible supports
|
|
|
320
390
|
|
|
321
391
|
#### **✅ Usage Scenarios**
|
|
322
392
|
|
|
323
|
-
#### **Scenario 1: Context where the
|
|
393
|
+
#### **[Scenario 1]: Context where the call expression of `t` function is evaluated at runtime**
|
|
324
394
|
|
|
325
395
|
- For the common example, **t** call expression in a **function module**
|
|
326
396
|
- This behaves the same as `useTranslation`'s `t` function.
|
|
@@ -348,7 +418,7 @@ export default function App() {
|
|
|
348
418
|
|
|
349
419
|
**Result:** The translation is resolved **immediately** during runtime.
|
|
350
420
|
|
|
351
|
-
####
|
|
421
|
+
#### **[Scenario 2]: Context where the translation value is evaluated immediately**
|
|
352
422
|
|
|
353
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.
|
|
354
424
|
- Since JavaScript **evaluates modules at import time**, it may attempt to access translation values before `IntlProvider` has fully initialized the locale.
|
|
@@ -384,27 +454,10 @@ export default function App() {
|
|
|
384
454
|
}
|
|
385
455
|
```
|
|
386
456
|
|
|
387
|
-
##
|
|
388
|
-
|
|
389
|
-
Custom error messages help identify misconfigurations:
|
|
390
|
-
|
|
391
|
-
1. **Invalid Params**: Ensure `supportedLocales`, `defaultLocale`, and `localeSet` are correctly provided.
|
|
392
|
-
2. **Missing Default Locale**: The `defaultLocale` must exist in `supportedLocales`.
|
|
393
|
-
3. **Invalid LocaleSet**: Ensure the `localeSet` keys match `supportedLocales`.
|
|
394
|
-
|
|
395
|
-
## 📜 Library Versions 🔢
|
|
396
|
-
|
|
397
|
-
This package supports the following library versions:
|
|
398
|
-
|
|
399
|
-
- **React**: `^19.0.0`
|
|
400
|
-
- **React Intl**: `^7.0.4`
|
|
401
|
-
|
|
402
|
-
## 📜 License
|
|
457
|
+
## License 📜
|
|
403
458
|
|
|
404
|
-
This project is licensed under the ISC License.
|
|
459
|
+
This project is licensed under the ISC License. See the LICENSE file for details.
|
|
405
460
|
|
|
406
|
-
##
|
|
461
|
+
## Author ✍️
|
|
407
462
|
|
|
408
|
-
|
|
409
|
-
[GitHub](https://github.com/chltjdrhd777)
|
|
410
|
-
[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.2.
|
|
3
|
+
"version": "1.2.1",
|
|
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.5.
|
|
32
|
+
"@sheet-i18n/typescript-config": "1.5.1"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@sheet-i18n/react-core": "1.2.
|
|
36
|
-
"@sheet-i18n/react-client": "1.2.
|
|
35
|
+
"@sheet-i18n/react-core": "1.2.1",
|
|
36
|
+
"@sheet-i18n/react-client": "1.2.1"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsup",
|