@oxog/i18n-react 0.1.1 → 0.1.2
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 +347 -0
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
# @oxog/i18n-react
|
|
2
|
+
|
|
3
|
+
React bindings for @oxog/i18n - seamless internationalization for React applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🎯 **React Hooks**: Modern hooks API for translations
|
|
8
|
+
- 🔄 **Context Provider**: Share i18n instance across your app
|
|
9
|
+
- 🎨 **Components**: Ready-to-use translation components
|
|
10
|
+
- 📦 **TypeScript Support**: Full type safety with TypeScript
|
|
11
|
+
- ⚡ **Performance**: Optimized re-renders with memoization
|
|
12
|
+
- 🔌 **SSR Compatible**: Works with Next.js and server-side rendering
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @oxog/i18n @oxog/i18n-react
|
|
18
|
+
# or
|
|
19
|
+
pnpm add @oxog/i18n @oxog/i18n-react
|
|
20
|
+
# or
|
|
21
|
+
yarn add @oxog/i18n @oxog/i18n-react
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import React from 'react';
|
|
28
|
+
import { createI18n } from '@oxog/i18n';
|
|
29
|
+
import { I18nProvider, useTranslation, Trans } from '@oxog/i18n-react';
|
|
30
|
+
|
|
31
|
+
// Create i18n instance
|
|
32
|
+
const i18n = createI18n({
|
|
33
|
+
locale: 'en',
|
|
34
|
+
messages: {
|
|
35
|
+
en: {
|
|
36
|
+
greeting: 'Hello, {name}!',
|
|
37
|
+
description: 'Welcome to <bold>React</bold> i18n'
|
|
38
|
+
},
|
|
39
|
+
es: {
|
|
40
|
+
greeting: '¡Hola, {name}!',
|
|
41
|
+
description: 'Bienvenido a <bold>React</bold> i18n'
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Wrap your app with I18nProvider
|
|
47
|
+
function App() {
|
|
48
|
+
return (
|
|
49
|
+
<I18nProvider i18n={i18n}>
|
|
50
|
+
<MyComponent />
|
|
51
|
+
</I18nProvider>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Use translations in your components
|
|
56
|
+
function MyComponent() {
|
|
57
|
+
const { t, locale, setLocale } = useTranslation();
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<div>
|
|
61
|
+
<h1>{t('greeting', { name: 'World' })}</h1>
|
|
62
|
+
<Trans i18nKey="description" components={{ bold: <strong /> }} />
|
|
63
|
+
<button onClick={() => setLocale(locale === 'en' ? 'es' : 'en')}>
|
|
64
|
+
Switch Language
|
|
65
|
+
</button>
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## API Reference
|
|
72
|
+
|
|
73
|
+
### `<I18nProvider>`
|
|
74
|
+
|
|
75
|
+
Provides i18n context to your React app.
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
import { I18nProvider } from '@oxog/i18n-react';
|
|
79
|
+
|
|
80
|
+
<I18nProvider i18n={i18nInstance}>
|
|
81
|
+
<App />
|
|
82
|
+
</I18nProvider>
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Props:
|
|
86
|
+
- `i18n`: The i18n instance created with `createI18n()`
|
|
87
|
+
- `children`: Your React components
|
|
88
|
+
|
|
89
|
+
### `useTranslation()`
|
|
90
|
+
|
|
91
|
+
Hook to access translation functions and locale management.
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
const { t, locale, setLocale, i18n } = useTranslation();
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
- `t`: Translation function
|
|
99
|
+
- `locale`: Current locale
|
|
100
|
+
- `setLocale`: Function to change locale
|
|
101
|
+
- `i18n`: The i18n instance
|
|
102
|
+
|
|
103
|
+
### `useI18n()`
|
|
104
|
+
|
|
105
|
+
Hook to access the raw i18n instance.
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
const i18n = useI18n();
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### `<Trans>`
|
|
112
|
+
|
|
113
|
+
Component for translations with embedded components.
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
<Trans
|
|
117
|
+
i18nKey="message"
|
|
118
|
+
values={{ count: 5 }}
|
|
119
|
+
components={{
|
|
120
|
+
bold: <strong />,
|
|
121
|
+
link: <a href="/docs" />
|
|
122
|
+
}}
|
|
123
|
+
/>
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Props:
|
|
127
|
+
- `i18nKey`: Translation key
|
|
128
|
+
- `values`: Interpolation values
|
|
129
|
+
- `components`: Components to embed in translation
|
|
130
|
+
- `defaultValue`: Fallback value
|
|
131
|
+
|
|
132
|
+
## Advanced Usage
|
|
133
|
+
|
|
134
|
+
### TypeScript Support
|
|
135
|
+
|
|
136
|
+
```tsx
|
|
137
|
+
// Define your message schema
|
|
138
|
+
type MessageSchema = {
|
|
139
|
+
greeting: string;
|
|
140
|
+
user: {
|
|
141
|
+
welcome: string;
|
|
142
|
+
profile: string;
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// Create typed hooks
|
|
147
|
+
const { useTranslation } = createI18nReact<MessageSchema>();
|
|
148
|
+
|
|
149
|
+
// Use with full type safety
|
|
150
|
+
function Component() {
|
|
151
|
+
const { t } = useTranslation();
|
|
152
|
+
|
|
153
|
+
t('greeting'); // ✅ Valid
|
|
154
|
+
t('user.welcome'); // ✅ Valid
|
|
155
|
+
t('invalid.key'); // ❌ TypeScript error
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Namespaces
|
|
160
|
+
|
|
161
|
+
Organize translations by feature or component:
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
function FeatureComponent() {
|
|
165
|
+
const { t } = useTranslation('feature');
|
|
166
|
+
|
|
167
|
+
return <div>{t('title')}</div>; // Translates 'feature.title'
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Suspense Support
|
|
172
|
+
|
|
173
|
+
Load translations asynchronously:
|
|
174
|
+
|
|
175
|
+
```tsx
|
|
176
|
+
import { Suspense } from 'react';
|
|
177
|
+
import { I18nProvider, useLazyTranslation } from '@oxog/i18n-react';
|
|
178
|
+
|
|
179
|
+
function AsyncComponent() {
|
|
180
|
+
const { t } = useLazyTranslation();
|
|
181
|
+
return <div>{t('async.message')}</div>;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function App() {
|
|
185
|
+
return (
|
|
186
|
+
<I18nProvider i18n={i18n}>
|
|
187
|
+
<Suspense fallback={<div>Loading translations...</div>}>
|
|
188
|
+
<AsyncComponent />
|
|
189
|
+
</Suspense>
|
|
190
|
+
</I18nProvider>
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Server-Side Rendering (SSR)
|
|
196
|
+
|
|
197
|
+
```tsx
|
|
198
|
+
// Next.js example
|
|
199
|
+
import { GetServerSideProps } from 'next';
|
|
200
|
+
|
|
201
|
+
export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
|
|
202
|
+
const messages = await import(`../locales/${locale}.json`);
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
props: {
|
|
206
|
+
locale,
|
|
207
|
+
messages: messages.default
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
function MyApp({ Component, pageProps }) {
|
|
213
|
+
const i18n = createI18n({
|
|
214
|
+
locale: pageProps.locale,
|
|
215
|
+
messages: {
|
|
216
|
+
[pageProps.locale]: pageProps.messages
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<I18nProvider i18n={i18n}>
|
|
222
|
+
<Component {...pageProps} />
|
|
223
|
+
</I18nProvider>
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Performance Optimization
|
|
229
|
+
|
|
230
|
+
```tsx
|
|
231
|
+
import { memo } from 'react';
|
|
232
|
+
import { useTranslation } from '@oxog/i18n-react';
|
|
233
|
+
|
|
234
|
+
// Component only re-renders when translation changes
|
|
235
|
+
const TranslatedComponent = memo(function TranslatedComponent() {
|
|
236
|
+
const { t } = useTranslation();
|
|
237
|
+
|
|
238
|
+
return (
|
|
239
|
+
<div>
|
|
240
|
+
<h1>{t('expensive.computation')}</h1>
|
|
241
|
+
</div>
|
|
242
|
+
);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// Selective subscription
|
|
246
|
+
function OptimizedComponent() {
|
|
247
|
+
// Only subscribe to locale changes, not translation updates
|
|
248
|
+
const { locale, setLocale } = useTranslation({ subscribe: ['locale'] });
|
|
249
|
+
|
|
250
|
+
return (
|
|
251
|
+
<select value={locale} onChange={(e) => setLocale(e.target.value)}>
|
|
252
|
+
<option value="en">English</option>
|
|
253
|
+
<option value="es">Español</option>
|
|
254
|
+
</select>
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Custom Components
|
|
260
|
+
|
|
261
|
+
Create your own translation components:
|
|
262
|
+
|
|
263
|
+
```tsx
|
|
264
|
+
import { useTranslation } from '@oxog/i18n-react';
|
|
265
|
+
|
|
266
|
+
function FormattedDate({ date, format = 'short' }) {
|
|
267
|
+
const { t, locale } = useTranslation();
|
|
268
|
+
|
|
269
|
+
const formatted = new Intl.DateTimeFormat(locale, {
|
|
270
|
+
dateStyle: format
|
|
271
|
+
}).format(date);
|
|
272
|
+
|
|
273
|
+
return <time dateTime={date.toISOString()}>{formatted}</time>;
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Best Practices
|
|
278
|
+
|
|
279
|
+
1. **Initialize i18n outside components** to prevent recreation on each render
|
|
280
|
+
2. **Use `Trans` component** for translations with embedded React components
|
|
281
|
+
3. **Memoize components** that use translations to optimize performance
|
|
282
|
+
4. **Load translations asynchronously** for code splitting
|
|
283
|
+
5. **Type your translations** for better developer experience
|
|
284
|
+
|
|
285
|
+
## Examples
|
|
286
|
+
|
|
287
|
+
### Language Switcher
|
|
288
|
+
|
|
289
|
+
```tsx
|
|
290
|
+
function LanguageSwitcher() {
|
|
291
|
+
const { locale, setLocale } = useTranslation();
|
|
292
|
+
const languages = [
|
|
293
|
+
{ code: 'en', name: 'English' },
|
|
294
|
+
{ code: 'es', name: 'Español' },
|
|
295
|
+
{ code: 'fr', name: 'Français' }
|
|
296
|
+
];
|
|
297
|
+
|
|
298
|
+
return (
|
|
299
|
+
<select value={locale} onChange={(e) => setLocale(e.target.value)}>
|
|
300
|
+
{languages.map(lang => (
|
|
301
|
+
<option key={lang.code} value={lang.code}>
|
|
302
|
+
{lang.name}
|
|
303
|
+
</option>
|
|
304
|
+
))}
|
|
305
|
+
</select>
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### Pluralization
|
|
311
|
+
|
|
312
|
+
```tsx
|
|
313
|
+
function ItemCount({ count }) {
|
|
314
|
+
const { t } = useTranslation();
|
|
315
|
+
|
|
316
|
+
return (
|
|
317
|
+
<div>
|
|
318
|
+
{t('items.count', { count })}
|
|
319
|
+
</div>
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Translation file:
|
|
324
|
+
// {
|
|
325
|
+
// "items": {
|
|
326
|
+
// "count": {
|
|
327
|
+
// "zero": "No items",
|
|
328
|
+
// "one": "One item",
|
|
329
|
+
// "other": "{count} items"
|
|
330
|
+
// }
|
|
331
|
+
// }
|
|
332
|
+
// }
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
## License
|
|
336
|
+
|
|
337
|
+
MIT © Ersin Koç
|
|
338
|
+
|
|
339
|
+
## Contributing
|
|
340
|
+
|
|
341
|
+
Contributions are welcome! Please read our [Contributing Guide](https://github.com/ersinkoc/i18n/blob/main/CONTRIBUTING.md) for details.
|
|
342
|
+
|
|
343
|
+
## Links
|
|
344
|
+
|
|
345
|
+
- [GitHub Repository](https://github.com/ersinkoc/i18n)
|
|
346
|
+
- [NPM Package](https://www.npmjs.com/package/@oxog/i18n-react)
|
|
347
|
+
- [Documentation](https://github.com/ersinkoc/i18n#readme)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oxog/i18n-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "React bindings for @oxog/i18n",
|
|
5
5
|
"author": "Ersin Koç",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
],
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"react": ">=18.0.0",
|
|
28
|
-
"@oxog/i18n": "0.1.
|
|
28
|
+
"@oxog/i18n": "0.1.2"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@testing-library/jest-dom": "^6.2.0",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"tsup": "^8.0.1",
|
|
39
39
|
"typescript": "^5.3.3",
|
|
40
40
|
"vitest": "^1.2.1",
|
|
41
|
-
"@oxog/i18n": "0.1.
|
|
41
|
+
"@oxog/i18n": "0.1.2"
|
|
42
42
|
},
|
|
43
43
|
"sideEffects": false,
|
|
44
44
|
"keywords": [
|