kotori 3.1.1 → 4.0.0

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 CHANGED
@@ -58,7 +58,7 @@ npm i kotori
58
58
 
59
59
  ## Quick Start
60
60
 
61
- **utils.ts**
61
+ **locales.ts**
62
62
 
63
63
  ```ts
64
64
  import { kotori } from 'kotori'
@@ -67,12 +67,6 @@ export const { useT, dict, setLanguage, t } = kotori({
67
67
  primaryLanguageTag: 'en',
68
68
  secondaryLanguageTags: ['zh', 'ja', 'ms'],
69
69
  })
70
- ```
71
-
72
- **page1.tsx**
73
-
74
- ```tsx
75
- import { useT, dict, setLanguage, t } from './utils'
76
70
 
77
71
  const intro = dict({
78
72
  en: 'my name is {{name}}, I am {{age}} years old.',
@@ -89,32 +83,6 @@ const time = dict({
89
83
  // optional: type your arguments, by default it's `Record<'time', string | number>` in this example
90
84
  })<{ time: `${number}:${number}:${number}` }>
91
85
 
92
- export const Page1 = () => {
93
- const { language } = useT()
94
- return (
95
- <>
96
- <select
97
- name="language"
98
- value={language}
99
- onChange={(e) => setLanguage(e.target.value as 'en')}
100
- >
101
- <option value="en">English</option>
102
- <option value="zh">Chinese</option>
103
- <option value="ja">Japanese</option>
104
- <option value="ms">Malay</option>
105
- </select>
106
- <p>{t('intro', { name: 'John', age: 30 })}</p>
107
- <p>{t('time', { time: '12:00:00' })}</p>
108
- </>
109
- )
110
- }
111
- ```
112
-
113
- **page2.tsx**
114
-
115
- ```tsx
116
- import { useT, dict } from './utils'
117
-
118
86
  const weather = dict({
119
87
  en: 'The weather in {{city}} has {{humidity}}% humidity.',
120
88
  zh: '{{city}}的天气湿度为{{humidity}}%。',
@@ -135,9 +103,17 @@ const lastLogin = dict({
135
103
  ja: '最終ログイン:{{date}} {{time}}',
136
104
  ms: 'Log masuk terakhir: {{date}} pada {{time}}',
137
105
  })<{ date: `${number}-${number}-${number}`; time: `${number}:${number}` }>
106
+ ```
107
+
108
+ **page1.tsx**
109
+
110
+ ```tsx
111
+ import { useT, dict, setLanguage, t, intro, time } from './locales'
112
+
113
+ export const Page1 = () => {
114
+
115
+ const { language } = useT()
138
116
 
139
- export const Page2 = () => {
140
- const { t, language, setLanguage } = useT()
141
117
  return (
142
118
  <>
143
119
  <select
@@ -150,6 +126,24 @@ export const Page2 = () => {
150
126
  <option value="ja">Japanese</option>
151
127
  <option value="ms">Malay</option>
152
128
  </select>
129
+ <p>{t(intro, { name: 'John', age: 30 })}</p>
130
+ <p>{t(time, { time: '12:00:00' })}</p>
131
+ </>
132
+ )
133
+ }
134
+ ```
135
+
136
+ **page2.tsx**
137
+
138
+ ```tsx
139
+ import { useT, dict, setLanguage, t, weather, score, lastLogin } from './locales'
140
+
141
+ export const Page2 = () => {
142
+
143
+ useT()
144
+
145
+ return (
146
+ <>
153
147
  <p>{t(weather, { city: 'Kuala Lumpur', humidity: 80 })}</p>
154
148
  <p>{t(score, { score: 87, total: 100 })}</p>
155
149
  <p>{t(lastLogin, { date: '2024-04-24', time: '09:30' })}</p>
@@ -205,7 +199,7 @@ Updates the current language and rerenders all active `useT` consumers across al
205
199
 
206
200
  ### `t(dict, args?)`
207
201
 
208
- Returns the translated string for the current language. `args` is required if the string has variables, omitted if it doesn't. Available directly on the `kotori` instance for non-React usage, but typically accessed via the `useT` hook.
202
+ Returns the translated string for the current language. `args` is required if the string has variables, omitted if it doesn't. Available directly on the `kotori` instance for non-React usage.
209
203
 
210
204
  ### `useT()`
211
205
 
@@ -213,9 +207,7 @@ React hook. Returns `{ t, language, setLanguage }`.
213
207
 
214
208
  | return | type | description |
215
209
  | --- | --- | --- |
216
- | `t(dict, args?)` | `string` | Returns the translated string for the current language. `args` is required if the string has variables, omitted if it doesn't. |
217
210
  | `language` | `primaryLanguageTag` \| `secondaryLanguageTags` | The current language tag as a reactive value. Updates when `setLanguage` is called. |
218
- | `setLanguage(primaryLanguageTag \| secondaryLanguageTags)` | `void` | Updates the language and rerenders all active `useT` consumers. |
219
211
 
220
212
  ## Language Tags
221
213
 
package/dist/index.cjs CHANGED
@@ -25,11 +25,7 @@ const kotori = (props) => {
25
25
  for (const objKey in args[0]) locale = locale.replace(new RegExp(`\\{\\{\\s*${objKey}\\s*\\}\\}`, "g"), () => String(args[0]?.[objKey]));
26
26
  return locale;
27
27
  };
28
- let snapshot = {
29
- language,
30
- setLanguage,
31
- t
32
- };
28
+ let snapshot = { language };
33
29
  return {
34
30
  setLanguage,
35
31
  t,
package/dist/index.d.cts CHANGED
@@ -23,11 +23,6 @@ declare const kotori: <const PrimaryTag extends AllTags, const SecondaryTags ext
23
23
  }>;
24
24
  useT: () => {
25
25
  language: PrimaryTag | SecondaryTags;
26
- setLanguage: (tag: PrimaryTag | SecondaryTags) => void;
27
- t: <DictCallback extends () => Readonly<{
28
- translation: Record<PrimaryTag | SecondaryTags, string>;
29
- [_args]?: Record<string, string | number>;
30
- }>>(dict: DictCallback, ...args: keyof NonNullable<ReturnType<DictCallback>[typeof _args]> extends never ? [] : [NonNullable<ReturnType<DictCallback>[typeof _args]>]) => string;
31
26
  };
32
27
  };
33
28
  //#endregion
package/dist/index.d.mts CHANGED
@@ -23,11 +23,6 @@ declare const kotori: <const PrimaryTag extends AllTags, const SecondaryTags ext
23
23
  }>;
24
24
  useT: () => {
25
25
  language: PrimaryTag | SecondaryTags;
26
- setLanguage: (tag: PrimaryTag | SecondaryTags) => void;
27
- t: <DictCallback extends () => Readonly<{
28
- translation: Record<PrimaryTag | SecondaryTags, string>;
29
- [_args]?: Record<string, string | number>;
30
- }>>(dict: DictCallback, ...args: keyof NonNullable<ReturnType<DictCallback>[typeof _args]> extends never ? [] : [NonNullable<ReturnType<DictCallback>[typeof _args]>]) => string;
31
26
  };
32
27
  };
33
28
  //#endregion
package/dist/index.mjs CHANGED
@@ -24,11 +24,7 @@ const kotori = (props) => {
24
24
  for (const objKey in args[0]) locale = locale.replace(new RegExp(`\\{\\{\\s*${objKey}\\s*\\}\\}`, "g"), () => String(args[0]?.[objKey]));
25
25
  return locale;
26
26
  };
27
- let snapshot = {
28
- language,
29
- setLanguage,
30
- t
31
- };
27
+ let snapshot = { language };
32
28
  return {
33
29
  setLanguage,
34
30
  t,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "kotori",
3
3
  "description": "Strongly-typed and composable internationalization library for React",
4
- "version": "3.1.1",
4
+ "version": "4.0.0",
5
5
  "scripts": {
6
6
  "setup": "rm -rf node_modules && npm i && git init && husky",
7
7
  "prepublishOnly": "npm i && npx tsc && npm run build",