sheet-i18n 0.2.5-canary.5 β 0.2.5-canary.6
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 +260 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -22,8 +22,8 @@ npm install sheet-i18n
|
|
|
22
22
|
|
|
23
23
|
This is the core package for sheet-based translations. You can use it to work with Google Sheets and export translations.
|
|
24
24
|
|
|
25
|
-
<details
|
|
26
|
-
<summary>π Server export function -
|
|
25
|
+
<details>
|
|
26
|
+
<summary>π Server export function - sheet-i18n/exporter</summary>
|
|
27
27
|
|
|
28
28
|
#### `sheet-i18n/exporter`
|
|
29
29
|
|
|
@@ -136,3 +136,261 @@ This package provides a streamlined way to export data using sheets API. It is d
|
|
|
136
136
|
The exported translations will be saved in a format that is easy to use for localization purposes. Each translation is stored in its respective locale folder, with a structured format.
|
|
137
137
|
|
|
138
138
|
</details>
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
<details>
|
|
143
|
+
<summary>π Client export function - sheet-i18n/react</summary>
|
|
144
|
+
|
|
145
|
+
#### `sheet-i18n/react`
|
|
146
|
+
|
|
147
|
+
The **client-side i18n library** subpackage.
|
|
148
|
+
|
|
149
|
+
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.
|
|
150
|
+
|
|
151
|
+
## β¨ Package Introduction
|
|
152
|
+
|
|
153
|
+
- **createI18nStore**: `Store Creation` for managing translation data.
|
|
154
|
+
- **createI18nContext**: `React Context` to generate providers and hooks for translations.
|
|
155
|
+
- **IntlProvider**: `Translation Provider` for managing current locale.
|
|
156
|
+
- **useTranslation**: `Translation Hook` for easy access to translation messages.
|
|
157
|
+
|
|
158
|
+
## π Getting Started
|
|
159
|
+
|
|
160
|
+
### Package list
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
import { createI18nStore, createI18nContext, useTranslation } from '@sheet-i18n/react';
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Basic Usage
|
|
167
|
+
|
|
168
|
+
#### 1. Define Translation Data
|
|
169
|
+
|
|
170
|
+
Prepare locale JSON files:
|
|
171
|
+
|
|
172
|
+
```json
|
|
173
|
+
// en.json
|
|
174
|
+
{
|
|
175
|
+
"header": {
|
|
176
|
+
"login": "Login",
|
|
177
|
+
"logout": "Logout"
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ko.json
|
|
182
|
+
{
|
|
183
|
+
"header": {
|
|
184
|
+
"login": "λ‘κ·ΈμΈ",
|
|
185
|
+
"logout": "λ‘κ·Έμμ"
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
#### 2. Create i18n Store
|
|
191
|
+
|
|
192
|
+
this store will be used as a core translations module.
|
|
193
|
+
|
|
194
|
+
```tsx
|
|
195
|
+
import en from './en.json';
|
|
196
|
+
import ko from './ko.json';
|
|
197
|
+
|
|
198
|
+
import { createI18nStore } from '@sheet-i18n/react';
|
|
199
|
+
|
|
200
|
+
export const i18nStore = createI18nStore({
|
|
201
|
+
supportedLocales: ['ko', 'en'],
|
|
202
|
+
defaultLocale: 'ko',
|
|
203
|
+
localeSet: {
|
|
204
|
+
ko,
|
|
205
|
+
en,
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
#### 3. Create i18n Context
|
|
211
|
+
|
|
212
|
+
```tsx
|
|
213
|
+
import { i18nStore } from './file-path-of-i18nStore-initiated';
|
|
214
|
+
import { createI18nContext } from '@sheet-i18n/react';
|
|
215
|
+
|
|
216
|
+
export const { IntlProvider, useTranslation } = createI18nContext(i18nStore);
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
#### 4. Mount Intl Context Provider in your App
|
|
220
|
+
|
|
221
|
+
```tsx
|
|
222
|
+
import React from 'react';
|
|
223
|
+
import { IntlProvider } from './i18nContext';
|
|
224
|
+
|
|
225
|
+
const App = (currentLocale: string) => {
|
|
226
|
+
return (
|
|
227
|
+
<IntlProvider currentLocale={currentLocale}>
|
|
228
|
+
<YourComponent />
|
|
229
|
+
</IntlProvider>
|
|
230
|
+
);
|
|
231
|
+
};
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
#### 5. Use Translations
|
|
235
|
+
|
|
236
|
+
```tsx
|
|
237
|
+
import React from 'react';
|
|
238
|
+
import { useTranslation } from './i18nContext';
|
|
239
|
+
|
|
240
|
+
const YourComponent = () => {
|
|
241
|
+
const { t } = useTranslation('header');
|
|
242
|
+
|
|
243
|
+
return (
|
|
244
|
+
<div>
|
|
245
|
+
<button>{t('login')}</button>
|
|
246
|
+
<button>{t('logout')}</button>
|
|
247
|
+
</div>
|
|
248
|
+
);
|
|
249
|
+
};
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
## π¦ API Reference
|
|
255
|
+
|
|
256
|
+
### `createI18nStore`
|
|
257
|
+
|
|
258
|
+
Creates a store for managing translations.
|
|
259
|
+
|
|
260
|
+
#### Parameters:
|
|
261
|
+
|
|
262
|
+
- **`supportedLocales`** (array): Array of supported locale strings in your project.
|
|
263
|
+
- **`defaultLocale`** (string): Default locale to use.
|
|
264
|
+
- **`localeSet`** (object): Object containing translations for each locale.
|
|
265
|
+
|
|
266
|
+
#### Example:
|
|
267
|
+
|
|
268
|
+
```tsx
|
|
269
|
+
const i18nStore = createI18nStore({
|
|
270
|
+
supportedLocales: ['ko', 'en'],
|
|
271
|
+
defaultLocale: 'ko',
|
|
272
|
+
localeSet: { ko, en },
|
|
273
|
+
});
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
---
|
|
277
|
+
|
|
278
|
+
### `createI18nContext`
|
|
279
|
+
|
|
280
|
+
Generates React context, including the `IntlProvider` and `useTranslation`.
|
|
281
|
+
|
|
282
|
+
#### Parameters:
|
|
283
|
+
|
|
284
|
+
- **`i18nStore`**: Output of `createI18nStore`.
|
|
285
|
+
|
|
286
|
+
#### Example:
|
|
287
|
+
|
|
288
|
+
```tsx
|
|
289
|
+
const { IntlProvider, useTranslation } = createI18nContext(i18nStore);
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
---
|
|
293
|
+
|
|
294
|
+
### `IntlProvider`
|
|
295
|
+
|
|
296
|
+
A React component to provide translations to child components.
|
|
297
|
+
|
|
298
|
+
#### Props:
|
|
299
|
+
|
|
300
|
+
- **`currentLocale`**: Key locale. This will determine which locale to use for translations data.
|
|
301
|
+
- **`children`**: React children.
|
|
302
|
+
|
|
303
|
+
#### Example:
|
|
304
|
+
|
|
305
|
+
```tsx
|
|
306
|
+
<IntlProvider currentLocale="ko">{children}</IntlProvider>
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
---
|
|
310
|
+
|
|
311
|
+
### `useTranslation`
|
|
312
|
+
|
|
313
|
+
A hook to access translations in your components.
|
|
314
|
+
|
|
315
|
+
#### Parameters:
|
|
316
|
+
|
|
317
|
+
- **`sheetTitle`**: The sheet title of the translation group.
|
|
318
|
+
|
|
319
|
+
#### Example:
|
|
320
|
+
|
|
321
|
+
```tsx
|
|
322
|
+
const { t } = useTranslation('header');
|
|
323
|
+
const translatedMessage = t('login');
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### `t Function`
|
|
327
|
+
|
|
328
|
+
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.
|
|
329
|
+
|
|
330
|
+
#### Parameters:
|
|
331
|
+
|
|
332
|
+
- key (string): The translation key to retrieve the localized string.
|
|
333
|
+
|
|
334
|
+
```tsx
|
|
335
|
+
const translatedMessage = t('login'); // login is key
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
- values (object, optional): An object containing key-value pairs to interpolate into the translation string.
|
|
339
|
+
|
|
340
|
+
```tsx
|
|
341
|
+
// John Doe shown
|
|
342
|
+
const translatedMessage = t('{username} shown', { username: 'John Doe' });
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
> π‘ Note: The values object can contain any type of data, including React components.
|
|
346
|
+
>
|
|
347
|
+
> ```tsx
|
|
348
|
+
> // <Username /> shown
|
|
349
|
+
> const translatedMessage = t('{username} shown', { username: <Username /> });
|
|
350
|
+
> ```
|
|
351
|
+
|
|
352
|
+
## π Error Handling
|
|
353
|
+
|
|
354
|
+
Custom error messages help identify misconfigurations:
|
|
355
|
+
|
|
356
|
+
1. **Invalid Params**: Ensure `supportedLocales`, `defaultLocale`, and `localeSet` are correctly provided.
|
|
357
|
+
2. **Missing Default Locale**: The `defaultLocale` must exist in `supportedLocales`.
|
|
358
|
+
3. **Invalid LocaleSet**: Ensure the `localeSet` keys match `supportedLocales`.
|
|
359
|
+
|
|
360
|
+
## π Example Usage in Applications
|
|
361
|
+
|
|
362
|
+
```tsx
|
|
363
|
+
import { useTranslation } from './i18nContext';
|
|
364
|
+
|
|
365
|
+
const TableComponent = () => {
|
|
366
|
+
const { t } = useTranslation('header');
|
|
367
|
+
|
|
368
|
+
const columns = [
|
|
369
|
+
{
|
|
370
|
+
title: t('logout'),
|
|
371
|
+
dataIndex: 'action',
|
|
372
|
+
},
|
|
373
|
+
];
|
|
374
|
+
|
|
375
|
+
return <Table columns={columns} />;
|
|
376
|
+
};
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
## π Library Versions π’
|
|
380
|
+
|
|
381
|
+
This package supports the following library versions:
|
|
382
|
+
|
|
383
|
+
- **React**: `^19.0.0`
|
|
384
|
+
- **React Intl**: `^7.0.4`
|
|
385
|
+
|
|
386
|
+
## π License
|
|
387
|
+
|
|
388
|
+
This project is licensed under the ISC License.
|
|
389
|
+
|
|
390
|
+
## π€ Author
|
|
391
|
+
|
|
392
|
+
**devAnderson**
|
|
393
|
+
[GitHub](https://github.com/chltjdrhd777)
|
|
394
|
+
[chltjdrhd777@gmail.com](mailto:chltjdrhd777@gmail.com)
|
|
395
|
+
|
|
396
|
+
</details>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sheet-i18n",
|
|
3
|
-
"version": "0.2.5-canary.
|
|
3
|
+
"version": "0.2.5-canary.6",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"module": "./dist/index.mjs",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -32,13 +32,13 @@
|
|
|
32
32
|
},
|
|
33
33
|
"license": "ISC",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@sheet-i18n/exporter": "0.2.
|
|
36
|
-
"@sheet-i18n/react": "0.1.1-canary.
|
|
35
|
+
"@sheet-i18n/exporter": "0.2.2-canary.0",
|
|
36
|
+
"@sheet-i18n/react": "0.1.1-canary.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"tsup": "^6.0.0",
|
|
40
40
|
"typescript": "^5.0.0",
|
|
41
|
-
"@sheet-i18n/typescript-config": "0.2.
|
|
41
|
+
"@sheet-i18n/typescript-config": "0.2.2-canary.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "tsup",
|