@nice2dev/i18n 1.0.3 → 1.0.4
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 +199 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# @nice2dev/i18n
|
|
2
|
+
|
|
3
|
+
Shared internationalization infrastructure for Nice2Dev UI libraries.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **NiceI18nProvider** — React context for translations
|
|
8
|
+
- **20 built-in languages** — en, pl, de, fr, es, it, pt, nl, sv, no, da, fi, cs, sk, hu, ro, bg, uk, ja, ko, zh, ar
|
|
9
|
+
- **ICU MessageFormat** — pluralization, gender, ordinals, select
|
|
10
|
+
- **RTL support** — Arabic, Hebrew with automatic dir attribute
|
|
11
|
+
- **Browser detection** — `detectBrowserLanguage()` auto-detects user preference
|
|
12
|
+
- **Custom translations** — plug in your own `t()` function (react-i18next compatible)
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @nice2dev/i18n
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### Option 1: Built-in Dictionaries (Simplest)
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { NiceI18nProvider } from '@nice2dev/i18n';
|
|
26
|
+
// or re-exported from @nice2dev/ui
|
|
27
|
+
|
|
28
|
+
function App() {
|
|
29
|
+
const [lang, setLang] = useState('en');
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<NiceI18nProvider lang={lang}>
|
|
33
|
+
<LanguageSelector value={lang} onChange={setLang} />
|
|
34
|
+
<MyApp />
|
|
35
|
+
</NiceI18nProvider>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
All Nice2Dev components automatically pick up translations from the provider.
|
|
41
|
+
|
|
42
|
+
### Option 2: Custom Translation Function
|
|
43
|
+
|
|
44
|
+
If you use react-i18next, i18next, or any other i18n library:
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
import { useTranslation } from 'react-i18next';
|
|
48
|
+
import { NiceI18nProvider } from '@nice2dev/i18n';
|
|
49
|
+
|
|
50
|
+
function App() {
|
|
51
|
+
const { t } = useTranslation();
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<NiceI18nProvider t={t}>
|
|
55
|
+
<MyApp />
|
|
56
|
+
</NiceI18nProvider>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Option 3: No Provider (English Defaults)
|
|
62
|
+
|
|
63
|
+
All components work without a provider — they show English by default.
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
<NiceButton>Click me</NiceButton> {/* Works fine */}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## API Reference
|
|
70
|
+
|
|
71
|
+
### NiceI18nProvider
|
|
72
|
+
|
|
73
|
+
| Prop | Type | Description |
|
|
74
|
+
| ----------- | -------------------------- | -------------------------------------------------------- |
|
|
75
|
+
| `lang` | `NiceSupportedLang` | Built-in language code (default: 'en') |
|
|
76
|
+
| `t` | `(key, default) => string` | Custom translation function (takes priority over `lang`) |
|
|
77
|
+
| `overrides` | `Record<string, string>` | Extra translations merged on top of built-in dictionary |
|
|
78
|
+
| `children` | `ReactNode` | App content |
|
|
79
|
+
|
|
80
|
+
### useNiceTranslation
|
|
81
|
+
|
|
82
|
+
Hook for translating text in components:
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
import { useNiceTranslation } from '@nice2dev/i18n';
|
|
86
|
+
|
|
87
|
+
function MyComponent() {
|
|
88
|
+
const { t } = useNiceTranslation();
|
|
89
|
+
return <span>{t('save', 'Save')}</span>; // Returns localized "Save"
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### createTranslator
|
|
94
|
+
|
|
95
|
+
Create a translator function without React context:
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
import { createTranslator } from '@nice2dev/i18n';
|
|
99
|
+
|
|
100
|
+
const t = createTranslator('de');
|
|
101
|
+
console.log(t('save', 'Save')); // "Speichern"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### ICU MessageFormat
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
import { formatICU, plural, ordinal, select } from '@nice2dev/i18n';
|
|
108
|
+
|
|
109
|
+
// Pluralization
|
|
110
|
+
formatICU('{count, plural, one{# item} other{# items}}', { count: 5 });
|
|
111
|
+
// → "5 items"
|
|
112
|
+
|
|
113
|
+
// Ordinals
|
|
114
|
+
formatICU('{n, selectordinal, one{#st} two{#nd} few{#rd} other{#th}}', { n: 3 });
|
|
115
|
+
// → "3rd"
|
|
116
|
+
|
|
117
|
+
// Gender select
|
|
118
|
+
formatICU('{gender, select, male{He} female{She} other{They}} liked it', { gender: 'female' });
|
|
119
|
+
// → "She liked it"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### RTL Support
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
import { RTLProvider, useIsRTL, isRTLLanguage } from '@nice2dev/i18n';
|
|
126
|
+
|
|
127
|
+
// Wrap your app with RTL-aware container
|
|
128
|
+
<RTLProvider lang="ar">
|
|
129
|
+
<App /> {/* dir="rtl" automatically applied */}
|
|
130
|
+
</RTLProvider>;
|
|
131
|
+
|
|
132
|
+
// Check if current language is RTL
|
|
133
|
+
function MyComponent() {
|
|
134
|
+
const isRTL = useIsRTL();
|
|
135
|
+
return <div style={{ textAlign: isRTL ? 'right' : 'left' }}>...</div>;
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### detectBrowserLanguage
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
import { detectBrowserLanguage } from '@nice2dev/i18n';
|
|
143
|
+
|
|
144
|
+
const userLang = detectBrowserLanguage(); // e.g. 'pl' if browser is Polish
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Supported Languages
|
|
148
|
+
|
|
149
|
+
| Code | Language | Native Name |
|
|
150
|
+
| ---- | ------------ | ----------- |
|
|
151
|
+
| `en` | English | English |
|
|
152
|
+
| `pl` | Polish | Polski |
|
|
153
|
+
| `de` | German | Deutsch |
|
|
154
|
+
| `fr` | French | Français |
|
|
155
|
+
| `es` | Spanish | Español |
|
|
156
|
+
| `it` | Italian | Italiano |
|
|
157
|
+
| `pt` | Portuguese | Português |
|
|
158
|
+
| `nl` | Dutch | Nederlands |
|
|
159
|
+
| `sv` | Swedish | Svenska |
|
|
160
|
+
| `no` | Norwegian | Norsk |
|
|
161
|
+
| `da` | Danish | Dansk |
|
|
162
|
+
| `fi` | Finnish | Suomi |
|
|
163
|
+
| `cs` | Czech | Čeština |
|
|
164
|
+
| `sk` | Slovak | Slovenčina |
|
|
165
|
+
| `hu` | Hungarian | Magyar |
|
|
166
|
+
| `ro` | Romanian | Română |
|
|
167
|
+
| `bg` | Bulgarian | Български |
|
|
168
|
+
| `uk` | Ukrainian | Українська |
|
|
169
|
+
| `ja` | Japanese | 日本語 |
|
|
170
|
+
| `ko` | Korean | 한국어 |
|
|
171
|
+
| `zh` | Chinese | 中文 |
|
|
172
|
+
| `ar` | Arabic (RTL) | العربية |
|
|
173
|
+
|
|
174
|
+
## Adding Custom Translations
|
|
175
|
+
|
|
176
|
+
Extend built-in dictionaries with the `overrides` prop:
|
|
177
|
+
|
|
178
|
+
```tsx
|
|
179
|
+
<NiceI18nProvider lang="en" overrides={{ 'myapp.greeting': 'Hello!' }}>
|
|
180
|
+
...
|
|
181
|
+
</NiceI18nProvider>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Or provide your own complete dictionary via `t` prop.
|
|
185
|
+
|
|
186
|
+
## Translation Keys
|
|
187
|
+
|
|
188
|
+
All Nice2Dev components use namespaced keys like:
|
|
189
|
+
|
|
190
|
+
- `ok`, `cancel`, `save`, `close` — common actions
|
|
191
|
+
- `nav.home`, `nav.menu` — navigation
|
|
192
|
+
- `dataGrid.sort`, `dataGrid.filter` — grid-specific
|
|
193
|
+
- `form.required`, `form.invalid` — validation messages
|
|
194
|
+
|
|
195
|
+
See `src/i18nDictionaries.ts` for the full list.
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
199
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nice2dev/i18n",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Shared internationalization infrastructure for Nice2Dev UI libraries",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
"translation"
|
|
54
54
|
],
|
|
55
55
|
"author": "Nice2Dev",
|
|
56
|
-
"license": "
|
|
56
|
+
"license": "SEE LICENSE IN LICENSE"
|
|
57
57
|
}
|