@djangocfg/i18n 2.1.162 → 2.1.164

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
@@ -1,14 +1,14 @@
1
1
  # @djangocfg/i18n
2
2
 
3
- Lightweight, type-safe i18n library for @djangocfg component packages. Provides built-in translations for English, Russian, and Korean with easy extensibility.
3
+ Lightweight, type-safe i18n library with LLM-powered translation CLI.
4
4
 
5
5
  ## Features
6
6
 
7
- - **Type-safe** - Full TypeScript support with autocomplete for translation keys
8
- - **Lightweight** - No heavy dependencies, pure React
9
- - **Extensible** - Easy to add custom translations or override defaults
10
- - **Works standalone** - Components work without provider using English defaults
11
- - **3 languages built-in** - English, Russian, Korean
7
+ - **17 languages** - en, ru, ko, ja, de, fr, zh, it, es, nl, ar, tr, pt-BR, pl, sv, no, da
8
+ - **LLM Translation** - Translate with GPT-4o-mini via CLI
9
+ - **Type-safe** - Full TypeScript support with autocomplete
10
+ - **CLI included** - Manage and translate locales
11
+ - **Works standalone** - Components work without provider
12
12
 
13
13
  ## Installation
14
14
 
@@ -21,29 +21,111 @@ pnpm add @djangocfg/i18n
21
21
  ```tsx
22
22
  import { I18nProvider, useT, ru } from '@djangocfg/i18n'
23
23
 
24
- // Wrap your app (optional - works without provider too)
24
+ // Wrap your app
25
25
  <I18nProvider locale="ru" translations={ru}>
26
26
  <App />
27
27
  </I18nProvider>
28
28
 
29
- // Use in components - simplest way
29
+ // Use in components
30
30
  function MyComponent() {
31
31
  const t = useT()
32
32
  return <span>{t('ui.form.save')}</span>
33
33
  }
34
34
  ```
35
35
 
36
+ ## CLI
37
+
38
+ Built-in CLI with LLM translation support.
39
+
40
+ ### Translate Text
41
+
42
+ ```bash
43
+ # Translate to multiple languages
44
+ pnpm i18n translate "Hello World" --to ru,ko,ja
45
+ # => ru: Привет, мир
46
+ # => ko: 안녕하세요, 세계
47
+ # => ja: こんにちは、世界
48
+
49
+ # Output as JSON
50
+ pnpm i18n translate "Save" --to ru,ko --json
51
+ # => {"en":"Save","ru":"Сохранить","ko":"저장"}
52
+ ```
53
+
54
+ ### Translate Locale File
55
+
56
+ ```bash
57
+ # Translate entire en.ts to Russian
58
+ pnpm i18n translate ru
59
+
60
+ # Or explicit
61
+ pnpm i18n translate --file --to ru --from en
62
+ ```
63
+
64
+ ### Sync Missing Keys
65
+
66
+ ```bash
67
+ # Show missing keys
68
+ pnpm i18n sync --dry
69
+
70
+ # Add with [TODO] placeholders
71
+ pnpm i18n sync
72
+
73
+ # Sync with LLM translation
74
+ pnpm i18n sync --translate
75
+
76
+ # Sync specific locales
77
+ pnpm i18n sync --to ru,ko --translate
78
+ ```
79
+
80
+ ### Other Commands
81
+
82
+ ```bash
83
+ # List/search keys
84
+ pnpm i18n list # All keys
85
+ pnpm i18n list tour # Search pattern
86
+ pnpm i18n list -v # With values
87
+
88
+ # Check missing keys
89
+ pnpm i18n check
90
+
91
+ # Add key to all locales
92
+ pnpm i18n add "tools.new" '{"en":"New","ru":"Новый"}'
93
+
94
+ # Show translation cache stats
95
+ pnpm i18n translate --stats
96
+ ```
97
+
98
+ ### Working with App Locales
99
+
100
+ CLI supports any locales directory (.ts or .json files):
101
+
102
+ ```bash
103
+ # Hub app
104
+ pnpm i18n translate "Dashboard" --to ru,ko -d ../../apps/hub/i18n/locales
105
+
106
+ # Sync with LLM translation
107
+ pnpm i18n sync -d ../../apps/hub/i18n/locales --translate
108
+ ```
109
+
110
+ ### Environment Variables
111
+
112
+ ```bash
113
+ # Optional - uses built-in test key by default
114
+ SDKROUTER_API_KEY=your-key # SDKRouter (recommended)
115
+ OPENAI_API_KEY=your-key # OpenAI
116
+ ANTHROPIC_API_KEY=your-key # Anthropic
117
+ ```
118
+
36
119
  ## Hooks
37
120
 
38
121
  | Hook | Description |
39
122
  |------|-------------|
40
- | `useT()` | Returns translation function, works with/without provider (recommended) |
123
+ | `useT()` | Returns translation function (recommended) |
41
124
  | `useI18n()` | Full context: `{ t, locale, setLocale, translations }` |
42
- | `useTranslation()` | Alias for `useT()` |
43
125
  | `useLocale()` | Returns current locale string |
44
- | `useTypedT<T>()` | Type-safe translation function with compile-time key validation |
126
+ | `useTypedT<T>()` | Type-safe with compile-time key validation |
45
127
 
46
- ### useT() - Recommended
128
+ ### useT()
47
129
 
48
130
  ```tsx
49
131
  function MyComponent() {
@@ -52,7 +134,7 @@ function MyComponent() {
52
134
  }
53
135
  ```
54
136
 
55
- ### useI18n() - Full Context
137
+ ### useI18n()
56
138
 
57
139
  ```tsx
58
140
  function LocaleSwitcher() {
@@ -62,13 +144,12 @@ function LocaleSwitcher() {
62
144
  <select value={locale} onChange={(e) => setLocale(e.target.value)}>
63
145
  <option value="en">English</option>
64
146
  <option value="ru">Russian</option>
65
- <option value="ko">Korean</option>
66
147
  </select>
67
148
  )
68
149
  }
69
150
  ```
70
151
 
71
- ### useTypedT<T>() - Type-Safe Keys
152
+ ### useTypedT<T>()
72
153
 
73
154
  ```tsx
74
155
  import { useTypedT } from '@djangocfg/i18n'
@@ -81,69 +162,25 @@ function MyComponent() {
81
162
  }
82
163
  ```
83
164
 
84
- ### getT() - Non-Hook Context
85
-
86
- For class components or outside React:
87
-
88
- ```tsx
89
- import { getT } from '@djangocfg/i18n'
90
-
91
- const label = getT('ui.form.save')
92
- const message = getT('ui.pagination.showing', { from: 1, to: 10, total: 100 })
93
- ```
94
-
95
165
  ## Extending Translations
96
166
 
97
167
  ### mergeTranslations()
98
168
 
99
- Deep merge base translations with custom overrides. Supports generic type parameter for app-specific namespaces:
100
-
101
169
  ```tsx
102
170
  import { mergeTranslations, ru } from '@djangocfg/i18n'
103
171
 
104
- // Override specific keys
105
172
  const customRu = mergeTranslations(ru, {
106
- ui: {
107
- select: { placeholder: 'Выберите марку авто...' }
108
- }
173
+ ui: { select: { placeholder: 'Выберите...' } },
174
+ app: { title: 'Мое приложение' }
109
175
  })
110
-
111
- // Add app-specific namespace with type safety
112
- type AppNamespace = { app: { title: string; welcome: string } }
113
-
114
- const appRu = mergeTranslations<AppNamespace>(ru, {
115
- app: {
116
- title: 'Мое приложение',
117
- welcome: 'Добро пожаловать!'
118
- }
119
- })
120
- // Result type: I18nTranslations & AppNamespace
121
- ```
122
-
123
- ### createTranslations()
124
-
125
- Create translations from multiple partial sources:
126
-
127
- ```tsx
128
- import { createTranslations, en } from '@djangocfg/i18n'
129
-
130
- const translations = createTranslations(
131
- en, // base
132
- uiOverrides, // UI customizations
133
- appStrings // App-specific strings
134
- )
135
176
  ```
136
177
 
137
178
  ## Built-in Locales
138
179
 
139
180
  ```tsx
140
- import { en, ru, ko } from '@djangocfg/i18n'
181
+ import { en, ru, ko, ja, de, fr, zh, it, es, nl, ar, tr, ptBR, pl, sv, no, da } from '@djangocfg/i18n'
141
182
  ```
142
183
 
143
- - `en` - English (default)
144
- - `ru` - Russian
145
- - `ko` - Korean
146
-
147
184
  ## Interpolation
148
185
 
149
186
  ```tsx
@@ -154,81 +191,19 @@ t('ui.select.moreItems', { count: 5 })
154
191
  // => "+5 more"
155
192
  ```
156
193
 
157
- ## Type Utilities
158
-
159
- ```tsx
160
- import type { PathKeys, I18nKeys, I18nTranslations } from '@djangocfg/i18n'
161
-
162
- // Get all valid keys from any translations type
163
- type MyKeys = PathKeys<MyTranslations>
164
- // = "form.title" | "form.submit" | "messages.error" | ...
165
-
166
- // Built-in keys for base translations
167
- const key: I18nKeys = 'ui.form.save' // OK
168
- const bad: I18nKeys = 'ui.form.typo' // Error
169
- ```
170
-
171
194
  ## Translation Key Paths
172
195
 
173
- All translation keys use dot notation:
174
-
175
- ### UI Components
176
-
177
- ```
178
- ui.select.placeholder - "Select option..."
179
- ui.select.search - "Search..."
180
- ui.select.noResults - "No results found."
181
- ui.select.selectAll - "Select all"
182
- ui.select.clearAll - "Clear all"
183
- ui.select.moreItems - "+{count} more"
184
-
185
- ui.pagination.previous - "Previous"
186
- ui.pagination.next - "Next"
187
- ui.pagination.showing - "{from}-{to} of {total}"
188
-
189
- ui.form.submit - "Submit"
190
- ui.form.save - "Save"
191
- ui.form.cancel - "Cancel"
192
- ui.form.delete - "Delete"
193
- ui.form.loading - "Loading..."
194
- ui.form.required - "This field is required"
195
-
196
- ui.dialog.close - "Close"
197
- ui.dialog.confirm - "Confirm"
198
-
199
- ui.errors.generic - "Something went wrong"
200
- ui.errors.network - "Network error..."
201
196
  ```
202
-
203
- ### Layouts
204
-
205
- ```
206
- layouts.sidebar.toggle - "Toggle sidebar"
207
- layouts.profile.settings - "Settings"
208
- layouts.profile.logout - "Log out"
209
- layouts.theme.light - "Light"
210
- layouts.theme.dark - "Dark"
211
- ```
212
-
213
- ### API
214
-
215
- ```
216
- api.errors.networkError - "Network error"
217
- api.status.connecting - "Connecting..."
218
- api.status.connected - "Connected"
197
+ ui.* - UI components (select, form, dialog, table, pagination)
198
+ layouts.* - Layout components (sidebar, auth, profile, theme)
199
+ api.* - API/network messages
200
+ centrifugo.* - WebSocket monitoring
201
+ tools.* - Heavy tools (tour, upload, code, image)
219
202
  ```
220
203
 
221
204
  ## Works Without Provider
222
205
 
223
- Components using `useT()` or `useI18n()` work without provider - they fall back to English defaults. This allows gradual adoption in existing projects.
224
-
225
- ## Integration with @djangocfg packages
226
-
227
- This package is designed to work with:
228
- - `@djangocfg/ui-core` - Base React components
229
- - `@djangocfg/ui-nextjs` - Next.js specific components
230
- - `@djangocfg/layouts` - Layout components
231
- - `@djangocfg/nextjs` - Next.js App Router integration with next-intl
206
+ Components using `useT()` work without provider - they fall back to English defaults.
232
207
 
233
208
  ## License
234
209
 
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node