sheet-i18n 1.3.8 → 1.3.9
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 +138 -15
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -191,7 +191,7 @@ Prepare locale JSON files:
|
|
|
191
191
|
}
|
|
192
192
|
```
|
|
193
193
|
|
|
194
|
-
#### 2. Initialize
|
|
194
|
+
#### 2. Initialize i18nStore
|
|
195
195
|
|
|
196
196
|
this store will be used as a core translations module.
|
|
197
197
|
|
|
@@ -217,7 +217,8 @@ export const i18nStore = new I18nStore({
|
|
|
217
217
|
import { i18nStore } from './file-path-of-i18nStore-initiated';
|
|
218
218
|
import { createI18nContext } from '@sheet-i18n/react';
|
|
219
219
|
|
|
220
|
-
export const { IntlProvider, useTranslation } =
|
|
220
|
+
export const { IntlProvider, useTranslation, getTranslation } =
|
|
221
|
+
createI18nContext(i18nStore);
|
|
221
222
|
```
|
|
222
223
|
|
|
223
224
|
#### 4. Mount Intl Context Provider in your App
|
|
@@ -253,11 +254,11 @@ const YourComponent = () => {
|
|
|
253
254
|
};
|
|
254
255
|
```
|
|
255
256
|
|
|
256
|
-
|
|
257
|
+
<br/>
|
|
257
258
|
|
|
258
|
-
## 📦 API Reference
|
|
259
|
+
## 📦 Base API Reference
|
|
259
260
|
|
|
260
|
-
### `I18nStore`
|
|
261
|
+
### `I18nStore(core)`
|
|
261
262
|
|
|
262
263
|
The `I18nStore` manages type-safe translation states, ensuring consistency across locales.
|
|
263
264
|
|
|
@@ -266,6 +267,25 @@ The `I18nStore` manages type-safe translation states, ensuring consistency acros
|
|
|
266
267
|
- **`supportedLocales`**: Array of supported locale strings.
|
|
267
268
|
- **`defaultLocale`**: The default locale, included in `supportedLocales`.
|
|
268
269
|
- **`localeSet`**: An object where keys match `supportedLocales`, and values are translation sets.
|
|
270
|
+
- **`typeSafe(optional, default = true)`**: An optional boolean indicating whether to use type-safe translations.
|
|
271
|
+
|
|
272
|
+
> 💡 **typeSafe?** <br/>
|
|
273
|
+
> I18nStore is type-safe by default. So, basically, you can't add the translation data that is not defined in the locale Json files. However, for fast development, you can off the typeSafe option manually.
|
|
274
|
+
>
|
|
275
|
+
> ```tsx
|
|
276
|
+
> // typeSafe: false
|
|
277
|
+
> const YourComponent = () => {
|
|
278
|
+
> // useTranslation(sheetTitle: string)
|
|
279
|
+
> const { t } = useTranslation('header');
|
|
280
|
+
>
|
|
281
|
+
> return (
|
|
282
|
+
> <div>
|
|
283
|
+
> {/* t(key: string): any */}
|
|
284
|
+
> <button>{t('login')}</button>
|
|
285
|
+
> </div>
|
|
286
|
+
> );
|
|
287
|
+
> };
|
|
288
|
+
> ```
|
|
269
289
|
|
|
270
290
|
> ⚠️ Caveats:
|
|
271
291
|
>
|
|
@@ -311,15 +331,15 @@ const { IntlProvider, useTranslation } = createI18nContext(i18nStore);
|
|
|
311
331
|
|
|
312
332
|
### `IntlProvider`
|
|
313
333
|
|
|
314
|
-
A
|
|
334
|
+
A Context provider to provide translations to child components.
|
|
315
335
|
|
|
316
336
|
#### Properties:
|
|
317
337
|
|
|
318
338
|
- **`currentLocale`** (optional):
|
|
319
339
|
|
|
320
340
|
- A key representing the current locale to use for translations.
|
|
321
|
-
- If not provided, the user's preferred language is automatically detected based on
|
|
322
|
-
-
|
|
341
|
+
- If not provided, the user's preferred language is automatically detected based on the browser setting.
|
|
342
|
+
- The fallback is the default locale in **i18nStore** if the detected language is unsupported.
|
|
323
343
|
|
|
324
344
|
- **`children`**: React children.
|
|
325
345
|
|
|
@@ -328,9 +348,7 @@ A React component to provide translations to child components.
|
|
|
328
348
|
```tsx
|
|
329
349
|
// Add currentLocale if you want to manually handle the locale
|
|
330
350
|
|
|
331
|
-
//
|
|
332
|
-
import { i18nStore } from './file-path-of-i18nStore-initiated';
|
|
333
|
-
|
|
351
|
+
// This example is Next.js app routing
|
|
334
352
|
interface LayoutProps {
|
|
335
353
|
params: {
|
|
336
354
|
locale: Locale;
|
|
@@ -341,10 +359,7 @@ export default function Layout({
|
|
|
341
359
|
children,
|
|
342
360
|
params,
|
|
343
361
|
}: PropsWithChildren<PageProps>) {
|
|
344
|
-
|
|
345
|
-
const currentLocale = params?.locale ?? defaultLocale;
|
|
346
|
-
|
|
347
|
-
return <IntlProvider currentLocale={currentLocale}>{children}</IntlProvider>;
|
|
362
|
+
return <IntlProvider currentLocale={params.locale}>{children}</IntlProvider>;
|
|
348
363
|
}
|
|
349
364
|
```
|
|
350
365
|
|
|
@@ -363,6 +378,35 @@ const { t } = useTranslation('header');
|
|
|
363
378
|
const translatedMessage = t('login');
|
|
364
379
|
```
|
|
365
380
|
|
|
381
|
+
### `getTranslation`
|
|
382
|
+
|
|
383
|
+
A hook to access translations in static modules.
|
|
384
|
+
|
|
385
|
+
#### Parameters:
|
|
386
|
+
|
|
387
|
+
- **`sheetTitle`**: The sheet title of the translation group.
|
|
388
|
+
|
|
389
|
+
#### Example:
|
|
390
|
+
|
|
391
|
+
```tsx
|
|
392
|
+
// data.ts
|
|
393
|
+
import { getTranslation } from './i18nContext';
|
|
394
|
+
|
|
395
|
+
const { t } = getTranslation('header');
|
|
396
|
+
|
|
397
|
+
export const STATUS_CATEGORY = [
|
|
398
|
+
t('Total Energy Usage'),
|
|
399
|
+
t('Total Waste Usage'),
|
|
400
|
+
];
|
|
401
|
+
|
|
402
|
+
// App.tsx
|
|
403
|
+
import { STATUS_CATEGORY } from './data';
|
|
404
|
+
|
|
405
|
+
export default function App() {
|
|
406
|
+
return STATUS_CATEGORY.map((item) => <div>{item}</div>);
|
|
407
|
+
}
|
|
408
|
+
```
|
|
409
|
+
|
|
366
410
|
### `t Function`
|
|
367
411
|
|
|
368
412
|
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.
|
|
@@ -389,6 +433,85 @@ const translatedMessage = t('{username} shown', { username: 'John Doe' });
|
|
|
389
433
|
const translatedMessage = t('{username} shown', { username: <Username /> });
|
|
390
434
|
```
|
|
391
435
|
|
|
436
|
+
<br/>
|
|
437
|
+
|
|
438
|
+
## 📄 Best Practices of Translation utilities
|
|
439
|
+
|
|
440
|
+
A robust and flexible translation library designed for efficient handling of translations in React applications.
|
|
441
|
+
|
|
442
|
+
### Features
|
|
443
|
+
|
|
444
|
+
- **Client-Side Translation (Explicit Strings)**: Translate explicit string keys directly in your components.
|
|
445
|
+
- **Client-Side Translation (Dynamic Strings)**: Dynamically match translations from JSON for unknown variables.
|
|
446
|
+
- **Static Module Translation**: Use translations in static modules for configuration constants.
|
|
447
|
+
|
|
448
|
+
### Usage
|
|
449
|
+
|
|
450
|
+
#### 1. **Client-Side Translation (Explicit Strings)**
|
|
451
|
+
|
|
452
|
+
Retrieve translations for explicit keys in your React components using `useTranslation`.
|
|
453
|
+
|
|
454
|
+
```tsx
|
|
455
|
+
import React from 'react';
|
|
456
|
+
import { useTranslation } from './i18nContext';
|
|
457
|
+
|
|
458
|
+
const YourComponent = () => {
|
|
459
|
+
const { t } = useTranslation('header');
|
|
460
|
+
|
|
461
|
+
return (
|
|
462
|
+
<div>
|
|
463
|
+
<button>{t('login')}</button>
|
|
464
|
+
<button>{t('logout')}</button>
|
|
465
|
+
</div>
|
|
466
|
+
);
|
|
467
|
+
};
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
#### 2. **Client-Side Translation (Dynamic Strings)**
|
|
471
|
+
|
|
472
|
+
For scenarios where the string key is not explicitly known (e.g., coming from a server), use `t.dynamic`. Ensure that your translation JSON and sheet are updated to include the variable values.
|
|
473
|
+
</br></br>
|
|
474
|
+
The **t.dynamic** function will automatically match the values from the JSON with the provided arguments and display the result.
|
|
475
|
+
|
|
476
|
+
```tsx
|
|
477
|
+
// App.tsx
|
|
478
|
+
import { useTranslation } from './i18nContext';
|
|
479
|
+
|
|
480
|
+
const { t } = useTranslation('header');
|
|
481
|
+
const { data } = useQuery(...queryOptions);
|
|
482
|
+
|
|
483
|
+
const deviceName = data?.device?.name;
|
|
484
|
+
|
|
485
|
+
return <div>{t.dynamic(deviceName)}</div>;
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
#### 3. **Static Module Translation**
|
|
489
|
+
|
|
490
|
+
Use `getTranslation` to initialize translations for static configurations, constants, or data that needs to be translated.
|
|
491
|
+
|
|
492
|
+
The **"t"** function from `getTranslation` returns a **Promise** that resolves to the translated string.
|
|
493
|
+
|
|
494
|
+
> 💡 Note <br/>
|
|
495
|
+
> The **`t`** function returned by **`getTranslation`** resolves its Promise in the background. For display purposes, you don't need to wait for the Promise to resolve, allowing you to use it without async/await.<br/> However, if you need to work with the fulfilled translated value during runtime, you have use async/await to retrieve it.
|
|
496
|
+
|
|
497
|
+
```tsx
|
|
498
|
+
// data.ts
|
|
499
|
+
import { getTranslation } from './i18nContext';
|
|
500
|
+
const { t } = getTranslation('header');
|
|
501
|
+
|
|
502
|
+
export const STATUS_CATEGORY = [
|
|
503
|
+
t('Total Energy Usage'),
|
|
504
|
+
t('Total Waste Usage'),
|
|
505
|
+
];
|
|
506
|
+
|
|
507
|
+
// App.tsx
|
|
508
|
+
import { STATUS_CATEGORY } from './data';
|
|
509
|
+
|
|
510
|
+
export default function App() {
|
|
511
|
+
return STATUS_CATEGORY.map((item) => <div>{item}</div>);
|
|
512
|
+
}
|
|
513
|
+
```
|
|
514
|
+
|
|
392
515
|
## 🛠 Error Handling
|
|
393
516
|
|
|
394
517
|
Custom error messages help identify misconfigurations:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sheet-i18n",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.9",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/chltjdrhd777/sheet-i18n"
|
|
@@ -36,13 +36,13 @@
|
|
|
36
36
|
},
|
|
37
37
|
"license": "ISC",
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@sheet-i18n/
|
|
40
|
-
"@sheet-i18n/
|
|
39
|
+
"@sheet-i18n/exporter": "1.3.5",
|
|
40
|
+
"@sheet-i18n/react": "1.0.4"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"tsup": "^6.0.0",
|
|
44
44
|
"typescript": "^5.0.0",
|
|
45
|
-
"@sheet-i18n/typescript-config": "1.3.
|
|
45
|
+
"@sheet-i18n/typescript-config": "1.3.5"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "tsup",
|