sheet-i18n 0.2.6-canary.2 → 0.3.0-canary.3

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 +74 -57
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # sheet-i18n
2
2
 
3
- **An all-in-one package for sheet-based translations.**
3
+ **An all-in-one package for sheet-based translation**
4
4
 
5
5
  [![npm](https://img.shields.io/npm/v/sheet-i18n)](https://www.npmjs.com/package/sheet-i18n)
6
6
  <a href="https://sheet-i18n.vercel.app/en" target="_blank">
@@ -20,10 +20,16 @@ npm install sheet-i18n
20
20
 
21
21
  ### 📑 Main Package - `sheet-i18n`
22
22
 
23
- This is the core package for sheet-based translations. You can use it to work with Google Sheets and export translations.
23
+ `sheet-i18n` is a **powerful and flexible library** designed to streamline the process of managing and utilizing translations stored in **Google Sheets** _(planned to support other spreadsheets soon)_.
24
24
 
25
- <details>
26
- <summary>📄 Server export function - sheet-i18n/exporter</summary>
25
+ It serves as a bridge between your translation data and your application, offering an **end-to-end solution** for exporting, managing, and integrating translations into your projects.
26
+
27
+ The `sheet-i18n` ecosystem is divided into two main packages:
28
+
29
+ ---
30
+
31
+ <details open>
32
+ <summary><h3>🌍 Server Export Function - sheet-i18n/exporter</h3></summary>
27
33
 
28
34
  #### `sheet-i18n/exporter`
29
35
 
@@ -139,8 +145,8 @@ The exported translations will be saved in a format that is easy to use for loca
139
145
 
140
146
  ---
141
147
 
142
- <details>
143
- <summary>📄 Client export function - sheet-i18n/react</summary>
148
+ <details open>
149
+ <summary><h3>⚛️ Frontend Translation Provider - sheet-i18n/react</h3></summary>
144
150
 
145
151
  #### `sheet-i18n/react`
146
152
 
@@ -150,19 +156,13 @@ This package provides tools to handle translations in React applications using c
150
156
 
151
157
  ## ✨ Package Introduction
152
158
 
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.
159
+ - **`I18nStore`**: _Store Creation Class_ for managing translation data.
160
+ - **`createI18nContext`**: _React Context_ to generate providers and hooks for translation.
161
+ - **`IntlProvider`**: _React Translation Provider_ for managing current locale.
162
+ - **`useTranslation`**: _Translation Hook_ for easy access to translation messages.
157
163
 
158
164
  ## 🚀 Getting Started
159
165
 
160
- ### Package list
161
-
162
- ```bash
163
- import { createI18nStore, createI18nContext, useTranslation } from '@sheet-i18n/react';
164
- ```
165
-
166
166
  ### Basic Usage
167
167
 
168
168
  #### 1. Define Translation Data
@@ -187,7 +187,7 @@ Prepare locale JSON files:
187
187
  }
188
188
  ```
189
189
 
190
- #### 2. Create i18n Store
190
+ #### 2. Initialize i18n Store
191
191
 
192
192
  this store will be used as a core translations module.
193
193
 
@@ -195,9 +195,9 @@ this store will be used as a core translations module.
195
195
  import en from './en.json';
196
196
  import ko from './ko.json';
197
197
 
198
- import { createI18nStore } from '@sheet-i18n/react';
198
+ import { I18nStore } from '@sheet-i18n/react';
199
199
 
200
- export const i18nStore = createI18nStore({
200
+ export const i18nStore = new I18nStore({
201
201
  supportedLocales: ['ko', 'en'],
202
202
  defaultLocale: 'ko',
203
203
  localeSet: {
@@ -222,9 +222,9 @@ export const { IntlProvider, useTranslation } = createI18nContext(i18nStore);
222
222
  import React from 'react';
223
223
  import { IntlProvider } from './i18nContext';
224
224
 
225
- const App = (currentLocale: string) => {
225
+ const App = () => {
226
226
  return (
227
- <IntlProvider currentLocale={currentLocale}>
227
+ <IntlProvider>
228
228
  <YourComponent />
229
229
  </IntlProvider>
230
230
  );
@@ -253,27 +253,38 @@ const YourComponent = () => {
253
253
 
254
254
  ## 📦 API Reference
255
255
 
256
- ### `createI18nStore`
256
+ ### `I18nStore`
257
257
 
258
- Creates a store for managing translations.
258
+ The `I18nStore` manages type-safe translation states, ensuring consistency across locales.
259
259
 
260
260
  #### Parameters:
261
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.
262
+ - **`supportedLocales`**: Array of supported locale strings.
263
+ - **`defaultLocale`**: The default locale, included in `supportedLocales`.
264
+ - **`localeSet`**: An object where keys match `supportedLocales`, and values are translation sets.
265
+
266
+ > ⚠️ Caveats:
267
+
268
+ 1. `supportedLocales` must be an array of locale strings.
269
+ 2. `defaultLocale` must exist in `supportedLocales`.
270
+ 3. `localeSet` must be an object with keys matching `supportedLocales`.
265
271
 
266
272
  #### Example:
267
273
 
268
274
  ```tsx
269
- const i18nStore = createI18nStore({
275
+ export const i18nStore = new I18nStore({
270
276
  supportedLocales: ['ko', 'en'],
271
277
  defaultLocale: 'ko',
272
- localeSet: { ko, en },
278
+ localeSet: {
279
+ ko,
280
+ en,
281
+ },
273
282
  });
274
- ```
275
283
 
276
- ---
284
+ // Accessing properties
285
+ i18nStore.supportedLocales; // ['ko', 'en']
286
+ i18nStore.defaultLocale; // 'ko'
287
+ ```
277
288
 
278
289
  ### `createI18nContext`
279
290
 
@@ -281,7 +292,12 @@ Generates React context, including the `IntlProvider` and `useTranslation`.
281
292
 
282
293
  #### Parameters:
283
294
 
284
- - **`i18nStore`**: Output of `createI18nStore`.
295
+ - **`i18nStore`**: Instance of `I18nStore`.
296
+
297
+ > ⚠️ Caveats:
298
+
299
+ 1. `i18nStore` passed to createI18nContext must be an instance of I18nStore.
300
+ 2. custom object is not allowed to be passed to createI18nContext.
285
301
 
286
302
  #### Example:
287
303
 
@@ -289,24 +305,44 @@ Generates React context, including the `IntlProvider` and `useTranslation`.
289
305
  const { IntlProvider, useTranslation } = createI18nContext(i18nStore);
290
306
  ```
291
307
 
292
- ---
293
-
294
308
  ### `IntlProvider`
295
309
 
296
310
  A React component to provide translations to child components.
297
311
 
298
- #### Props:
312
+ #### Properties:
313
+
314
+ - **`currentLocale`** (optional):
315
+
316
+ - A key representing the current locale to use for translations.
317
+ - If not provided, the user's preferred language is automatically detected based on their browser settings.
318
+ - Falls back to the default locale in **i18nStore** if the detected language is unsupported.
299
319
 
300
- - **`currentLocale`**: Key locale. This will determine which locale to use for translations data.
301
320
  - **`children`**: React children.
302
321
 
303
322
  #### Example:
304
323
 
305
324
  ```tsx
306
- <IntlProvider currentLocale="ko">{children}</IntlProvider>
307
- ```
325
+ // Add currentLocale if you want to manually handle the locale
308
326
 
309
- ---
327
+ // The example is Next.js app routing
328
+ import { i18nStore } from './file-path-of-i18nStore-initiated';
329
+
330
+ interface LayoutProps {
331
+ params: {
332
+ locale: Locale;
333
+ };
334
+ }
335
+
336
+ export default function Layout({
337
+ children,
338
+ params,
339
+ }: PropsWithChildren<PageProps>) {
340
+ const { defaultLocale } = i18nStore;
341
+ const currentLocale = params?.locale ?? defaultLocale;
342
+
343
+ return <IntlProvider currentLocale={currentLocale}>{children}</IntlProvider>;
344
+ }
345
+ ```
310
346
 
311
347
  ### `useTranslation`
312
348
 
@@ -357,25 +393,6 @@ Custom error messages help identify misconfigurations:
357
393
  2. **Missing Default Locale**: The `defaultLocale` must exist in `supportedLocales`.
358
394
  3. **Invalid LocaleSet**: Ensure the `localeSet` keys match `supportedLocales`.
359
395
 
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
396
  ## 📜 Library Versions 🔢
380
397
 
381
398
  This package supports the following library versions:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sheet-i18n",
3
- "version": "0.2.6-canary.2",
3
+ "version": "0.3.0-canary.3",
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.3-canary.0",
36
- "@sheet-i18n/react": "0.1.2-canary.2"
35
+ "@sheet-i18n/exporter": "0.3.0-canary.1",
36
+ "@sheet-i18n/react": "0.2.0-canary.3"
37
37
  },
38
38
  "devDependencies": {
39
39
  "tsup": "^6.0.0",
40
40
  "typescript": "^5.0.0",
41
- "@sheet-i18n/typescript-config": "0.2.3-canary.0"
41
+ "@sheet-i18n/typescript-config": "0.3.0-canary.1"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",