@sheet-i18n/react 0.3.4 β†’ 0.4.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 (2) hide show
  1. package/README.md +239 -33
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -1,57 +1,263 @@
1
1
  # @sheet-i18n/react
2
2
 
3
- i18n client logic based on React, part of the `sheet-i18n` ecosystem.
3
+ The **client-side i18n library** subpackage of sheet-i18n.
4
4
 
5
- ## Installation
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
- To install this package, use your preferred package manager:
7
+ ## ✨ Package Introduction
8
8
 
9
- ```bash
10
- npm install @sheet-i18n/react
11
- # or
12
- yarn add @sheet-i18n/react
13
- # or
14
- pnpm add @sheet-i18n/react
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
- ## ✨ Features
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
- βœ… Combines `@sheet-i18n/react-core` and `@sheet-i18n/react-client` for a comprehensive i18n solution.
139
+ ### `createI18nContext`
20
140
 
21
- βœ… Provides robust client-side logic for handling translations in React applications.
141
+ Generates React context, including the `IntlProvider` and `useTranslation`.
22
142
 
23
- βœ… Fully compatible with the `sheet-i18n` ecosystem.
143
+ #### Parameters:
24
144
 
25
- ## πŸ“œ Scripts
145
+ - **`i18nStore`**: Instance of `I18nStore`.
26
146
 
27
- These scripts are available in the package:
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
- - **build**: Builds the library using `tsup`.
30
- - **dev**: Watches for changes and rebuilds during development.
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
- ## πŸ“¦ Exports
238
+ ## πŸ›  Error Handling
33
239
 
34
- The package provides the following exports:
240
+ Custom error messages help identify misconfigurations:
35
241
 
36
- - **CommonJS**: `./dist/index.js`
37
- - **ES Module**: `./dist/index.mjs`
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
- ## ⚠️ Peer Dependencies
246
+ ## πŸ“œ Library Versions πŸ”’
40
247
 
41
- This package works best when used with compatible React and `sheet-i18n` libraries.
248
+ This package supports the following library versions:
42
249
 
43
- ## πŸ› οΈ Development Dependencies
250
+ - **React**: `^19.0.0`
251
+ - **React Intl**: `^7.0.4`
44
252
 
45
- - **@sheet-i18n/typescript-config**: Shared TypeScript configuration presets.
46
- - **@swc/core**: A fast compiler for JavaScript and TypeScript.
253
+ ## πŸ“œ License
47
254
 
48
- ## πŸ”‘ Keywords
255
+ This project is licensed under the ISC License.
49
256
 
50
- - `sheet-i18n`
51
- - `react`
257
+ ## πŸ‘€ Author
52
258
 
53
- ## πŸ‘¨β€πŸ’» Author
259
+ **devAnderson**
260
+ [GitHub](https://github.com/chltjdrhd777)
261
+ [chltjdrhd777@gmail.com](mailto:chltjdrhd777@gmail.com)
54
262
 
55
- - **devAnderson**
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.3.4",
3
+ "version": "0.4.2",
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.2.0"
32
+ "@sheet-i18n/typescript-config": "1.3.2"
33
33
  },
34
34
  "dependencies": {
35
- "@sheet-i18n/react-core": "0.3.1",
36
- "@sheet-i18n/react-client": "0.3.2"
35
+ "@sheet-i18n/react-core": "0.4.2",
36
+ "@sheet-i18n/react-client": "0.4.2"
37
37
  },
38
38
  "scripts": {
39
39
  "build": "tsup",