sheet-i18n 1.3.10 β 1.3.11
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 +66 -38
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -30,9 +30,9 @@ The `sheet-i18n` ecosystem is divided into three main packages:
|
|
|
30
30
|
- `sheet-i18n/react`
|
|
31
31
|
- `@sheet-i18n/cli`
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
<br/>
|
|
34
34
|
|
|
35
|
-
<details
|
|
35
|
+
<details>
|
|
36
36
|
<summary>π Server Export Function - sheet-i18n/exporter</summary>
|
|
37
37
|
|
|
38
38
|
#### `sheet-i18n/exporter`
|
|
@@ -152,9 +152,9 @@ The exported translations will be saved in a format that is easy to use for loca
|
|
|
152
152
|
<details>
|
|
153
153
|
<summary>βοΈ Frontend Translation Provider - sheet-i18n/react</summary>
|
|
154
154
|
|
|
155
|
-
|
|
155
|
+
### `@sheet-i18n/react`
|
|
156
156
|
|
|
157
|
-
The **client-side i18n library** subpackage.
|
|
157
|
+
The **client-side i18n library** subpackage of sheet-i18n.
|
|
158
158
|
|
|
159
159
|
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.
|
|
160
160
|
|
|
@@ -163,7 +163,8 @@ This package provides tools to handle translations in React applications using c
|
|
|
163
163
|
- **`I18nStore`**: _Store Creation Class_ for managing translation data.
|
|
164
164
|
- **`createI18nContext`**: _React Context_ to generate providers and hooks for translation.
|
|
165
165
|
- **`IntlProvider`**: _React Translation Provider_ for managing current locale.
|
|
166
|
-
- **`useTranslation`**:
|
|
166
|
+
- **`useTranslation`**: _Client Side Translation Hook_ for easy access to translation messages on the client side.
|
|
167
|
+
- **`getTranslation`**: _Static Translation Function_ for easy access to translation messages on Static module files.
|
|
167
168
|
|
|
168
169
|
## π Getting Started
|
|
169
170
|
|
|
@@ -380,33 +381,12 @@ const translatedMessage = t('login');
|
|
|
380
381
|
|
|
381
382
|
### `getTranslation`
|
|
382
383
|
|
|
383
|
-
A
|
|
384
|
+
A function to access translations in the environment where cannot use react context and hooks.
|
|
384
385
|
|
|
385
386
|
#### Parameters:
|
|
386
387
|
|
|
387
388
|
- **`sheetTitle`**: The sheet title of the translation group.
|
|
388
389
|
|
|
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
|
-
|
|
410
390
|
### `t Function`
|
|
411
391
|
|
|
412
392
|
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.
|
|
@@ -443,7 +423,7 @@ A robust and flexible translation library designed for efficient handling of tra
|
|
|
443
423
|
|
|
444
424
|
- **Client-Side Translation (Explicit Strings)**: Translate explicit string keys directly in your components.
|
|
445
425
|
- **Client-Side Translation (Dynamic Strings)**: Dynamically match translations from JSON for unknown variables.
|
|
446
|
-
- **
|
|
426
|
+
- **Module Translation**: Use translations in the place where the react context or hooks cannot be used.
|
|
447
427
|
|
|
448
428
|
### Usage
|
|
449
429
|
|
|
@@ -485,30 +465,78 @@ const deviceName = data?.device?.name;
|
|
|
485
465
|
return <div>{t.dynamic(deviceName)}</div>;
|
|
486
466
|
```
|
|
487
467
|
|
|
488
|
-
#### 3. **
|
|
468
|
+
#### 3. **Module Translation**
|
|
469
|
+
|
|
470
|
+
The `getTranslation` function allows you to use translations outside of React components, such as in static configuration files, constants, or utility modules.
|
|
471
|
+
|
|
472
|
+
It provide two possible supports
|
|
473
|
+
|
|
474
|
+
1. **`t` (Synchronous Translation)** β Use when translation values are available **at runtime**.
|
|
475
|
+
2. **`t.promise` (Asynchronous Translation)** β Use when the moduleβs evaluation order is uncertain.
|
|
476
|
+
|
|
477
|
+
#### **β
Usage Scenarios**
|
|
489
478
|
|
|
490
|
-
|
|
479
|
+
#### **Scenario 1: Context where the translation value is evaluated at runtime**
|
|
491
480
|
|
|
492
|
-
|
|
481
|
+
- For the common example, **t** call expression in a **function module**
|
|
482
|
+
- This behaves the same as `useTranslation`'s `t` function.
|
|
493
483
|
|
|
494
|
-
|
|
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.
|
|
484
|
+
##### **Example**
|
|
496
485
|
|
|
497
486
|
```tsx
|
|
498
|
-
//
|
|
487
|
+
// module.ts
|
|
499
488
|
import { getTranslation } from './i18nContext';
|
|
489
|
+
|
|
490
|
+
const { t } = getTranslation('header');
|
|
491
|
+
|
|
492
|
+
export function getStatusCategory {
|
|
493
|
+
return [t('Total Energy Usage'), t('Total Waste Usage')];
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
// App.tsx
|
|
497
|
+
// the "t" call expression is evaluated at function call timing
|
|
498
|
+
import { getStatusCategory } from './module';
|
|
499
|
+
|
|
500
|
+
export default function App() {
|
|
501
|
+
return getStatusCategory().map((item) => <div>{item}</div>);
|
|
502
|
+
}
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
**Result:** The translation is resolved **immediately** during runtime.
|
|
506
|
+
|
|
507
|
+
#### **β οΈ Scenario 2: Context where the translation value is evaluated immediately**
|
|
508
|
+
|
|
509
|
+
- If the module is **imported dynamically in a client-side component**, the evaluation order of `getTranslation` and `t` call expression may not be guaranteed.
|
|
510
|
+
- Since JavaScript **evaluates modules at import time**, it may attempt to access translation values before `IntlProvider` has fully initialized the locale.
|
|
511
|
+
- In this case, use **`t.promise`** to ensure the translation resolves asynchronously.
|
|
512
|
+
|
|
513
|
+
#### **Example**
|
|
514
|
+
|
|
515
|
+
```tsx
|
|
516
|
+
// module.ts
|
|
517
|
+
// The "t" result in the array will be resolved asynchronously
|
|
518
|
+
import { getTranslation } from './i18nContext';
|
|
519
|
+
|
|
500
520
|
const { t } = getTranslation('header');
|
|
501
521
|
|
|
502
522
|
export const STATUS_CATEGORY = [
|
|
503
|
-
t('Total Energy Usage'),
|
|
504
|
-
t('Total Waste Usage'),
|
|
523
|
+
t.promise('Total Energy Usage'),
|
|
524
|
+
t.promise('Total Waste Usage'),
|
|
505
525
|
];
|
|
506
526
|
|
|
507
527
|
// App.tsx
|
|
508
|
-
|
|
528
|
+
// So, t.promise ensure the current client-side locale data
|
|
529
|
+
// is fully initialized before the translations are resolved
|
|
530
|
+
import { STATUS_CATEGORY } from './module.ts';
|
|
509
531
|
|
|
510
532
|
export default function App() {
|
|
511
|
-
return
|
|
533
|
+
return (
|
|
534
|
+
<div>
|
|
535
|
+
{STATUS_CATEGORY.map((item, index) => {
|
|
536
|
+
return <div key={index}>{item}</div>;
|
|
537
|
+
})}
|
|
538
|
+
</div>
|
|
539
|
+
);
|
|
512
540
|
}
|
|
513
541
|
```
|
|
514
542
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sheet-i18n",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.11",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/chltjdrhd777/sheet-i18n"
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
},
|
|
37
37
|
"license": "ISC",
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@sheet-i18n/exporter": "1.3.
|
|
40
|
-
"@sheet-i18n/react": "1.0.
|
|
39
|
+
"@sheet-i18n/exporter": "1.3.7",
|
|
40
|
+
"@sheet-i18n/react": "1.0.6"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"tsup": "^6.0.0",
|