@sheet-i18n/react 1.0.5 → 1.0.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.
Files changed (2) hide show
  1. package/README.md +60 -37
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,5 +1,3 @@
1
- # @sheet-i18n/react
2
-
3
1
  The **client-side i18n library** subpackage of sheet-i18n.
4
2
 
5
3
  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.
@@ -227,33 +225,12 @@ const translatedMessage = t('login');
227
225
 
228
226
  ### `getTranslation`
229
227
 
230
- A hook to access translations in static modules.
228
+ A function to access translations in the environment where cannot use react context and hooks.
231
229
 
232
230
  #### Parameters:
233
231
 
234
232
  - **`sheetTitle`**: The sheet title of the translation group.
235
233
 
236
- #### Example:
237
-
238
- ```tsx
239
- // data.ts
240
- import { getTranslation } from './i18nContext';
241
-
242
- const { t } = getTranslation('header');
243
-
244
- export const STATUS_CATEGORY = [
245
- t('Total Energy Usage'),
246
- t('Total Waste Usage'),
247
- ];
248
-
249
- // App.tsx
250
- import { STATUS_CATEGORY } from './data';
251
-
252
- export default function App() {
253
- return STATUS_CATEGORY.map((item) => <div>{item}</div>);
254
- }
255
- ```
256
-
257
234
  ### `t Function`
258
235
 
259
236
  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.
@@ -290,7 +267,7 @@ A robust and flexible translation library designed for efficient handling of tra
290
267
 
291
268
  - **Client-Side Translation (Explicit Strings)**: Translate explicit string keys directly in your components.
292
269
  - **Client-Side Translation (Dynamic Strings)**: Dynamically match translations from JSON for unknown variables.
293
- - **Static Module Translation**: Use translations in static modules for configuration constants.
270
+ - **Module Translation**: Use translations in the place where the react context or hooks cannot be used.
294
271
 
295
272
  ### Usage
296
273
 
@@ -332,30 +309,78 @@ const deviceName = data?.device?.name;
332
309
  return <div>{t.dynamic(deviceName)}</div>;
333
310
  ```
334
311
 
335
- #### 3. **Static Module Translation**
312
+ #### 3. **Module Translation**
313
+
314
+ The `getTranslation` function allows you to use translations outside of React components, such as in static configuration files, constants, or utility modules.
315
+
316
+ It provide two possible supports
317
+
318
+ 1. **`t` (Synchronous Translation)** – Use when translation values are available **at runtime**.
319
+ 2. **`t.promise` (Asynchronous Translation)** – Use when the module’s evaluation order is uncertain.
320
+
321
+ #### **✅ Usage Scenarios**
322
+
323
+ #### **Scenario 1: Context where the translation value is evaluated at runtime**
324
+
325
+ - For the common example, **t** call expression in a **function module**
326
+ - This behaves the same as `useTranslation`'s `t` function.
327
+
328
+ ##### **Example**
329
+
330
+ ```tsx
331
+ // module.ts
332
+ import { getTranslation } from './i18nContext';
333
+
334
+ const { t } = getTranslation('header');
335
+
336
+ export function getStatusCategory {
337
+ return [t('Total Energy Usage'), t('Total Waste Usage')];
338
+ };
339
+
340
+ // App.tsx
341
+ // the "t" call expression is evaluated at function call timing
342
+ import { getStatusCategory } from './module';
343
+
344
+ export default function App() {
345
+ return getStatusCategory().map((item) => <div>{item}</div>);
346
+ }
347
+ ```
348
+
349
+ **Result:** The translation is resolved **immediately** during runtime.
336
350
 
337
- Use `getTranslation` to initialize translations for static configurations, constants, or data that needs to be translated.
351
+ #### **⚠️ Scenario 2: Context where the translation value is evaluated immediately**
338
352
 
339
- The **"t"** function from `getTranslation` returns a **Promise** that resolves to the translated string.
353
+ - If the module is **imported dynamically in a client-side component**, the evaluation order of `getTranslation` and `t` call expression may not be guaranteed.
354
+ - Since JavaScript **evaluates modules at import time**, it may attempt to access translation values before `IntlProvider` has fully initialized the locale.
355
+ - In this case, use **`t.promise`** to ensure the translation resolves asynchronously.
340
356
 
341
- > 💡 Note <br/>
342
- > 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.
357
+ #### **Example**
343
358
 
344
359
  ```tsx
345
- // data.ts
360
+ // module.ts
361
+ // The "t" result in the array will be resolved asynchronously
346
362
  import { getTranslation } from './i18nContext';
363
+
347
364
  const { t } = getTranslation('header');
348
365
 
349
366
  export const STATUS_CATEGORY = [
350
- t('Total Energy Usage'),
351
- t('Total Waste Usage'),
367
+ t.promise('Total Energy Usage'),
368
+ t.promise('Total Waste Usage'),
352
369
  ];
353
370
 
354
371
  // App.tsx
355
- import { STATUS_CATEGORY } from './data';
372
+ // So, t.promise ensure the current client-side locale data
373
+ // is fully initialized before the translations are resolved
374
+ import { STATUS_CATEGORY } from './module.ts';
356
375
 
357
376
  export default function App() {
358
- return STATUS_CATEGORY.map((item) => <div>{item}</div>);
377
+ return (
378
+ <div>
379
+ {STATUS_CATEGORY.map((item, index) => {
380
+ return <div key={index}>{item}</div>;
381
+ })}
382
+ </div>
383
+ );
359
384
  }
360
385
  ```
361
386
 
@@ -383,5 +408,3 @@ This project is licensed under the ISC License.
383
408
  **devAnderson**
384
409
  [GitHub](https://github.com/chltjdrhd777)
385
410
  [chltjdrhd777@gmail.com](mailto:chltjdrhd777@gmail.com)
386
-
387
- </details>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sheet-i18n/react",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "i18n client logic based on react",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -32,7 +32,7 @@
32
32
  "@sheet-i18n/typescript-config": "1.3.6"
33
33
  },
34
34
  "dependencies": {
35
- "@sheet-i18n/react-client": "1.0.5",
35
+ "@sheet-i18n/react-client": "1.0.6",
36
36
  "@sheet-i18n/react-core": "1.0.4"
37
37
  },
38
38
  "scripts": {