@sheet-i18n/react 0.4.1 β 0.4.3
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 +239 -33
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,57 +1,263 @@
|
|
|
1
1
|
# @sheet-i18n/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The **client-side i18n library** subpackage of sheet-i18n.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
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.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## β¨ Package Introduction
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
- **`I18nStore`**: _Store Creation Class_ for managing translation data.
|
|
10
|
+
- **`createI18nContext`**: _React Context_ to generate providers and hooks for translation.
|
|
11
|
+
- **`IntlProvider`**: _React Translation Provider_ for managing current locale.
|
|
12
|
+
- **`useTranslation`**: _Translation Hook_ for easy access to translation messages.
|
|
13
|
+
|
|
14
|
+
## π Getting Started
|
|
15
|
+
|
|
16
|
+
### Basic Usage
|
|
17
|
+
|
|
18
|
+
#### 1. Define Translation Data
|
|
19
|
+
|
|
20
|
+
Prepare locale JSON files:
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
// en.json
|
|
24
|
+
{
|
|
25
|
+
"header": {
|
|
26
|
+
"login": "Login",
|
|
27
|
+
"logout": "Logout"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ko.json
|
|
32
|
+
{
|
|
33
|
+
"header": {
|
|
34
|
+
"login": "λ‘κ·ΈμΈ",
|
|
35
|
+
"logout": "λ‘κ·Έμμ"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
#### 2. Initialize i18n Store
|
|
41
|
+
|
|
42
|
+
this store will be used as a core translations module.
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
import en from './en.json';
|
|
46
|
+
import ko from './ko.json';
|
|
47
|
+
|
|
48
|
+
import { I18nStore } from '@sheet-i18n/react';
|
|
49
|
+
|
|
50
|
+
export const i18nStore = new I18nStore({
|
|
51
|
+
supportedLocales: ['ko', 'en'],
|
|
52
|
+
defaultLocale: 'ko',
|
|
53
|
+
localeSet: {
|
|
54
|
+
ko,
|
|
55
|
+
en,
|
|
56
|
+
},
|
|
57
|
+
});
|
|
15
58
|
```
|
|
16
59
|
|
|
17
|
-
|
|
60
|
+
#### 3. Create i18n Context
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
import { i18nStore } from './file-path-of-i18nStore-initiated';
|
|
64
|
+
import { createI18nContext } from '@sheet-i18n/react';
|
|
65
|
+
|
|
66
|
+
export const { IntlProvider, useTranslation } = createI18nContext(i18nStore);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### 4. Mount Intl Context Provider in your App
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
import React from 'react';
|
|
73
|
+
import { IntlProvider } from './i18nContext';
|
|
74
|
+
|
|
75
|
+
const App = () => {
|
|
76
|
+
return (
|
|
77
|
+
<IntlProvider>
|
|
78
|
+
<YourComponent />
|
|
79
|
+
</IntlProvider>
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### 5. Use Translations
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
import React from 'react';
|
|
88
|
+
import { useTranslation } from './i18nContext';
|
|
89
|
+
|
|
90
|
+
const YourComponent = () => {
|
|
91
|
+
const { t } = useTranslation('header');
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<div>
|
|
95
|
+
<button>{t('login')}</button>
|
|
96
|
+
<button>{t('logout')}</button>
|
|
97
|
+
</div>
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## π¦ API Reference
|
|
105
|
+
|
|
106
|
+
### `I18nStore`
|
|
107
|
+
|
|
108
|
+
The `I18nStore` manages type-safe translation states, ensuring consistency across locales.
|
|
109
|
+
|
|
110
|
+
#### Parameters:
|
|
111
|
+
|
|
112
|
+
- **`supportedLocales`**: Array of supported locale strings.
|
|
113
|
+
- **`defaultLocale`**: The default locale, included in `supportedLocales`.
|
|
114
|
+
- **`localeSet`**: An object where keys match `supportedLocales`, and values are translation sets.
|
|
115
|
+
|
|
116
|
+
> β οΈ Caveats:
|
|
117
|
+
>
|
|
118
|
+
> 1. `supportedLocales` must be an array of locale strings.
|
|
119
|
+
> 2. `defaultLocale` must exist in `supportedLocales`.
|
|
120
|
+
> 3. `localeSet` must be an object with keys matching `supportedLocales`.
|
|
121
|
+
|
|
122
|
+
#### Example:
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
export const i18nStore = new I18nStore({
|
|
126
|
+
supportedLocales: ['ko', 'en'],
|
|
127
|
+
defaultLocale: 'ko',
|
|
128
|
+
localeSet: {
|
|
129
|
+
ko,
|
|
130
|
+
en,
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// Accessing properties
|
|
135
|
+
i18nStore.supportedLocales; // ['ko', 'en']
|
|
136
|
+
i18nStore.defaultLocale; // 'ko'
|
|
137
|
+
```
|
|
18
138
|
|
|
19
|
-
|
|
139
|
+
### `createI18nContext`
|
|
20
140
|
|
|
21
|
-
|
|
141
|
+
Generates React context, including the `IntlProvider` and `useTranslation`.
|
|
22
142
|
|
|
23
|
-
|
|
143
|
+
#### Parameters:
|
|
24
144
|
|
|
25
|
-
|
|
145
|
+
- **`i18nStore`**: Instance of `I18nStore`.
|
|
26
146
|
|
|
27
|
-
|
|
147
|
+
> β οΈ Caveats:
|
|
148
|
+
>
|
|
149
|
+
> 1. `i18nStore` passed to createI18nContext must be an instance of I18nStore.
|
|
150
|
+
> 2. custom object is not allowed to be passed to createI18nContext.
|
|
28
151
|
|
|
29
|
-
|
|
30
|
-
|
|
152
|
+
#### Example:
|
|
153
|
+
|
|
154
|
+
```tsx
|
|
155
|
+
const { IntlProvider, useTranslation } = createI18nContext(i18nStore);
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### `IntlProvider`
|
|
159
|
+
|
|
160
|
+
A React component to provide translations to child components.
|
|
161
|
+
|
|
162
|
+
#### Properties:
|
|
163
|
+
|
|
164
|
+
- **`currentLocale`** (optional):
|
|
165
|
+
|
|
166
|
+
- A key representing the current locale to use for translations.
|
|
167
|
+
- If not provided, the user's preferred language is automatically detected based on their browser settings.
|
|
168
|
+
- Falls back to the default locale in **i18nStore** if the detected language is unsupported.
|
|
169
|
+
|
|
170
|
+
- **`children`**: React children.
|
|
171
|
+
|
|
172
|
+
#### Example:
|
|
173
|
+
|
|
174
|
+
```tsx
|
|
175
|
+
// Add currentLocale if you want to manually handle the locale
|
|
176
|
+
|
|
177
|
+
// The example is Next.js app routing
|
|
178
|
+
import { i18nStore } from './file-path-of-i18nStore-initiated';
|
|
179
|
+
|
|
180
|
+
interface LayoutProps {
|
|
181
|
+
params: {
|
|
182
|
+
locale: Locale;
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export default function Layout({
|
|
187
|
+
children,
|
|
188
|
+
params,
|
|
189
|
+
}: PropsWithChildren<PageProps>) {
|
|
190
|
+
const { defaultLocale } = i18nStore;
|
|
191
|
+
const currentLocale = params?.locale ?? defaultLocale;
|
|
192
|
+
|
|
193
|
+
return <IntlProvider currentLocale={currentLocale}>{children}</IntlProvider>;
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### `useTranslation`
|
|
198
|
+
|
|
199
|
+
A hook to access translations in your components.
|
|
200
|
+
|
|
201
|
+
#### Parameters:
|
|
202
|
+
|
|
203
|
+
- **`sheetTitle`**: The sheet title of the translation group.
|
|
204
|
+
|
|
205
|
+
#### Example:
|
|
206
|
+
|
|
207
|
+
```tsx
|
|
208
|
+
const { t } = useTranslation('header');
|
|
209
|
+
const translatedMessage = t('login');
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### `t Function`
|
|
213
|
+
|
|
214
|
+
The t function is used to retrieve a translation string based on a key and optionally interpolate variables. It provides flexibility to include dynamic values, such as plain strings or React components, for enhanced localization capabilities.
|
|
215
|
+
|
|
216
|
+
#### Parameters:
|
|
217
|
+
|
|
218
|
+
- key (string): The translation key to retrieve the localized string.
|
|
219
|
+
|
|
220
|
+
```tsx
|
|
221
|
+
const translatedMessage = t('login'); // login is key
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
- values (object, optional): An object containing key-value pairs to interpolate into the translation string.
|
|
225
|
+
|
|
226
|
+
```tsx
|
|
227
|
+
// John Doe shown
|
|
228
|
+
const translatedMessage = t('{username} shown', { username: 'John Doe' });
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
π‘ Note: The values object can contain any type of data, including React components.
|
|
232
|
+
|
|
233
|
+
```tsx
|
|
234
|
+
// <Username /> shown
|
|
235
|
+
const translatedMessage = t('{username} shown', { username: <Username /> });
|
|
236
|
+
```
|
|
31
237
|
|
|
32
|
-
##
|
|
238
|
+
## π Error Handling
|
|
33
239
|
|
|
34
|
-
|
|
240
|
+
Custom error messages help identify misconfigurations:
|
|
35
241
|
|
|
36
|
-
|
|
37
|
-
|
|
242
|
+
1. **Invalid Params**: Ensure `supportedLocales`, `defaultLocale`, and `localeSet` are correctly provided.
|
|
243
|
+
2. **Missing Default Locale**: The `defaultLocale` must exist in `supportedLocales`.
|
|
244
|
+
3. **Invalid LocaleSet**: Ensure the `localeSet` keys match `supportedLocales`.
|
|
38
245
|
|
|
39
|
-
##
|
|
246
|
+
## π Library Versions π’
|
|
40
247
|
|
|
41
|
-
This package
|
|
248
|
+
This package supports the following library versions:
|
|
42
249
|
|
|
43
|
-
|
|
250
|
+
- **React**: `^19.0.0`
|
|
251
|
+
- **React Intl**: `^7.0.4`
|
|
44
252
|
|
|
45
|
-
|
|
46
|
-
- **@swc/core**: A fast compiler for JavaScript and TypeScript.
|
|
253
|
+
## π License
|
|
47
254
|
|
|
48
|
-
|
|
255
|
+
This project is licensed under the ISC License.
|
|
49
256
|
|
|
50
|
-
|
|
51
|
-
- `react`
|
|
257
|
+
## π€ Author
|
|
52
258
|
|
|
53
|
-
|
|
259
|
+
**devAnderson**
|
|
260
|
+
[GitHub](https://github.com/chltjdrhd777)
|
|
261
|
+
[chltjdrhd777@gmail.com](mailto:chltjdrhd777@gmail.com)
|
|
54
262
|
|
|
55
|
-
|
|
56
|
-
- [GitHub Profile](https://github.com/chltjdrhd777)
|
|
57
|
-
- π§ [Email](mailto:chltjdrhd777@gmail.com)
|
|
263
|
+
</details>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sheet-i18n/react",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
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.3.
|
|
32
|
+
"@sheet-i18n/typescript-config": "1.3.2"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@sheet-i18n/react-
|
|
36
|
-
"@sheet-i18n/react-
|
|
35
|
+
"@sheet-i18n/react-client": "0.4.3",
|
|
36
|
+
"@sheet-i18n/react-core": "0.4.2"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsup",
|