@owlmeans/i18n 0.1.2 → 0.1.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 +29 -297
- package/package.json +3 -2
- package/tsconfig.json +6 -11
package/README.md
CHANGED
|
@@ -1,323 +1,55 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @owlmeans/i18n
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Multi-level translation resource registration for OwlMeans applications.
|
|
4
4
|
|
|
5
|
-
##
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @owlmeans/i18n
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Core Concepts
|
|
12
|
-
|
|
13
|
-
### Translation Levels
|
|
14
|
-
The i18n system supports three levels of translations with different priorities:
|
|
15
|
-
- **Library Level**: Lowest priority, used for basic library translations
|
|
16
|
-
- **App Level**: Medium priority, used for application-specific translations
|
|
17
|
-
- **Service Level**: Highest priority, used for service-specific overrides
|
|
18
|
-
|
|
19
|
-
### Namespaces
|
|
20
|
-
Translations are organized into namespaces to avoid conflicts:
|
|
21
|
-
- `translation` - Default namespace for general translations
|
|
22
|
-
- `lib` - Library-specific translations
|
|
23
|
-
- `service` - Service-specific translations
|
|
24
|
-
- Custom namespaces can be defined as needed
|
|
25
|
-
|
|
26
|
-
### Resources
|
|
27
|
-
Each translation resource represents a collection of translations for a specific language and context, with optional priority settings for ordering.
|
|
28
|
-
|
|
29
|
-
## API Reference
|
|
30
|
-
|
|
31
|
-
### Types
|
|
32
|
-
|
|
33
|
-
#### `I18nStorage`
|
|
34
|
-
Main storage interface for the i18n system.
|
|
35
|
-
```typescript
|
|
36
|
-
interface I18nStorage {
|
|
37
|
-
data: I18nNamespaces
|
|
38
|
-
}
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
#### `I18nNamespaces`
|
|
42
|
-
Collection of namespaces, each containing resources.
|
|
43
|
-
```typescript
|
|
44
|
-
interface I18nNamespaces extends Record<string, I18nResources> { }
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
#### `I18nResources`
|
|
48
|
-
Collection of resources within a namespace.
|
|
49
|
-
```typescript
|
|
50
|
-
interface I18nResources extends Record<string, I18nLanguages> { }
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
#### `I18nLanguages`
|
|
54
|
-
Language-specific data containing resources and initialization status.
|
|
55
|
-
```typescript
|
|
56
|
-
interface I18nLanguages extends Record<string, {
|
|
57
|
-
resources: I18nResource[],
|
|
58
|
-
lngInitialized: string[]
|
|
59
|
-
}> { }
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
#### `I18nResource`
|
|
63
|
-
Individual translation resource with metadata.
|
|
64
|
-
```typescript
|
|
65
|
-
interface I18nResource {
|
|
66
|
-
ns?: string // Namespace (optional)
|
|
67
|
-
lng?: string // Language code (optional)
|
|
68
|
-
level: I18nLevel // Translation level
|
|
69
|
-
resource: string // Resource identifier
|
|
70
|
-
priority?: number // Priority for ordering (optional)
|
|
71
|
-
data: Record<string, any> // Translation data
|
|
72
|
-
}
|
|
73
|
-
```
|
|
5
|
+
## Overview
|
|
74
6
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
interface I18nResourceOptions {
|
|
79
|
-
priroty?: number // Priority for resource ordering (note: typo in source)
|
|
80
|
-
ns?: string // Namespace override
|
|
81
|
-
}
|
|
82
|
-
```
|
|
7
|
+
- Priority-based i18n resource storage: libraries register at lower priority, apps at higher priority
|
|
8
|
+
- Namespace-based organization — translations are grouped by resource name and namespace
|
|
9
|
+
- Used at startup to register translation bundles; actual translation rendering is done by the platform-specific i18n package (react-i18next, etc.)
|
|
83
10
|
|
|
84
|
-
|
|
85
|
-
Configuration interface for the i18n system.
|
|
86
|
-
```typescript
|
|
87
|
-
interface I18nConfig {
|
|
88
|
-
defaultLng?: string // Default language code
|
|
89
|
-
defaultNs?: string // Default namespace
|
|
90
|
-
}
|
|
91
|
-
```
|
|
11
|
+
## Installation
|
|
92
12
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
enum I18nLevel {
|
|
97
|
-
Library = 'library',
|
|
98
|
-
App = 'app',
|
|
99
|
-
Service = 'service'
|
|
100
|
-
}
|
|
13
|
+
```bash
|
|
14
|
+
bun add @owlmeans/i18n
|
|
101
15
|
```
|
|
102
16
|
|
|
103
|
-
|
|
17
|
+
## Usage
|
|
104
18
|
|
|
105
|
-
|
|
106
|
-
Add library-level translations.
|
|
107
|
-
```typescript
|
|
108
|
-
function addI18nLib(
|
|
109
|
-
lng: string, // Language code
|
|
110
|
-
resource: string, // Resource identifier
|
|
111
|
-
data: Record<string, any>, // Translation data
|
|
112
|
-
opts?: I18nResourceOptions | string // Options or namespace
|
|
113
|
-
): void
|
|
114
|
-
```
|
|
19
|
+
Register translations for a package at startup:
|
|
115
20
|
|
|
116
|
-
**Example:**
|
|
117
|
-
```typescript
|
|
118
|
-
import { addI18nLib } from '@owlmeans/i18n'
|
|
119
|
-
|
|
120
|
-
addI18nLib('en', 'common', {
|
|
121
|
-
'button.save': 'Save',
|
|
122
|
-
'button.cancel': 'Cancel'
|
|
123
|
-
})
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
#### `addI18nApp(lng, resource, data, opts?)`
|
|
127
|
-
Add application-level translations.
|
|
128
|
-
```typescript
|
|
129
|
-
function addI18nApp(
|
|
130
|
-
lng: string, // Language code
|
|
131
|
-
resource: string, // Resource identifier
|
|
132
|
-
data: Record<string, any>, // Translation data
|
|
133
|
-
opts?: I18nResourceOptions | string // Options or namespace
|
|
134
|
-
): void
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
**Example:**
|
|
138
21
|
```typescript
|
|
139
22
|
import { addI18nApp } from '@owlmeans/i18n'
|
|
140
23
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
'
|
|
144
|
-
'
|
|
24
|
+
// Register app-level translations (highest priority)
|
|
25
|
+
addI18nApp('en', 'manager-web', {
|
|
26
|
+
'project.create.title': 'Create Project',
|
|
27
|
+
'project.create.submit': 'Create',
|
|
145
28
|
})
|
|
146
|
-
```
|
|
147
29
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
function addCommonI18n(
|
|
152
|
-
lng: string, // Language code
|
|
153
|
-
resource: string, // Resource identifier
|
|
154
|
-
data: Record<string, any>, // Translation data
|
|
155
|
-
opts?: I18nResourceOptions | string // Options or namespace
|
|
156
|
-
): void
|
|
30
|
+
// Register library-level translations (lower priority — overridable by apps)
|
|
31
|
+
import { addI18n } from '@owlmeans/i18n'
|
|
32
|
+
addI18n('en', 'client-panel', { 'form.submit': 'Submit' })
|
|
157
33
|
```
|
|
158
34
|
|
|
159
|
-
|
|
160
|
-
```typescript
|
|
161
|
-
import { addCommonI18n } from '@owlmeans/i18n'
|
|
35
|
+
## API
|
|
162
36
|
|
|
163
|
-
|
|
164
|
-
'error.unauthorized': 'Access denied',
|
|
165
|
-
'error.notfound': 'Resource not found'
|
|
166
|
-
})
|
|
167
|
-
```
|
|
37
|
+
### `addI18nApp(lng, resource, data, opts?)`
|
|
168
38
|
|
|
169
|
-
|
|
170
|
-
Initialize translation resources for a specific language and resource.
|
|
171
|
-
```typescript
|
|
172
|
-
function initI18nResource(
|
|
173
|
-
lng: string, // Language code
|
|
174
|
-
resource: string, // Resource identifier
|
|
175
|
-
ns?: string // Namespace (optional)
|
|
176
|
-
): null | I18nResource[]
|
|
177
|
-
```
|
|
39
|
+
Register translations at app priority (highest). Typically called in `src/i18n.ts`.
|
|
178
40
|
|
|
179
|
-
|
|
41
|
+
### `addI18n(level, lng, resource, data, opts?)`
|
|
180
42
|
|
|
181
|
-
|
|
182
|
-
```typescript
|
|
183
|
-
import { initI18nResource } from '@owlmeans/i18n'
|
|
43
|
+
Register translations at a specific `I18nLevel`. Lower levels are overridden by higher ones.
|
|
184
44
|
|
|
185
|
-
|
|
186
|
-
if (resources) {
|
|
187
|
-
// Process initialized resources
|
|
188
|
-
resources.forEach(resource => {
|
|
189
|
-
console.log(`Loading ${resource.level} level translations:`, resource.data)
|
|
190
|
-
})
|
|
191
|
-
}
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
### Constants
|
|
45
|
+
### `I18nLevel`
|
|
195
46
|
|
|
196
|
-
|
|
197
|
-
Default namespace for translations.
|
|
198
|
-
```typescript
|
|
199
|
-
const DEFAULT_NAMESPACE = 'translation'
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
#### `LIB_NAMESPACE`
|
|
203
|
-
Namespace for library translations.
|
|
204
|
-
```typescript
|
|
205
|
-
const LIB_NAMESPACE = 'lib'
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
#### `SRV_NAMESPACE`
|
|
209
|
-
Namespace for service translations.
|
|
210
|
-
```typescript
|
|
211
|
-
const SRV_NAMESPACE = 'service'
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
#### `DEFAULT_LNG`
|
|
215
|
-
Default language code.
|
|
216
|
-
```typescript
|
|
217
|
-
const DEFAULT_LNG = 'en'
|
|
218
|
-
```
|
|
47
|
+
Priority levels: `Lib` < `Package` < `App`. App-level translations win over library translations for the same key.
|
|
219
48
|
|
|
220
|
-
|
|
221
|
-
Maximum priority value for resource ordering.
|
|
222
|
-
```typescript
|
|
223
|
-
const MAX_PRIORITY = Number.MAX_SAFE_INTEGER
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
### Utils
|
|
227
|
-
|
|
228
|
-
The package also exports utility functions under the `/utils` subpackage:
|
|
229
|
-
|
|
230
|
-
```typescript
|
|
231
|
-
import { ensureStructure, levelCost } from '@owlmeans/i18n/utils'
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
#### `ensureStructure(lng, resource, ns?)`
|
|
235
|
-
Ensures the storage structure exists for the given language, resource, and namespace.
|
|
236
|
-
```typescript
|
|
237
|
-
function ensureStructure(
|
|
238
|
-
lng: string, // Language code
|
|
239
|
-
resource: string, // Resource identifier
|
|
240
|
-
ns?: string // Namespace (optional)
|
|
241
|
-
): I18nLanguages[string]
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
#### `levelCost`
|
|
245
|
-
Mapping of translation levels to their priority costs:
|
|
246
|
-
```typescript
|
|
247
|
-
const levelCost = {
|
|
248
|
-
[I18nLevel.Library]: 0,
|
|
249
|
-
[I18nLevel.App]: 1,
|
|
250
|
-
[I18nLevel.Service]: 2
|
|
251
|
-
}
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
#### `_OwlMeansI18nStorage`
|
|
255
|
-
Internal storage instance (use with caution):
|
|
256
|
-
```typescript
|
|
257
|
-
const _OwlMeansI18nStorage: I18nStorage
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
> **Note:** This is an internal storage variable. Direct manipulation is not recommended for normal usage.
|
|
261
|
-
|
|
262
|
-
## Usage Examples
|
|
263
|
-
|
|
264
|
-
### Basic Usage
|
|
265
|
-
```typescript
|
|
266
|
-
import { addI18nLib, addI18nApp, initI18nResource } from '@owlmeans/i18n'
|
|
267
|
-
|
|
268
|
-
// Add library translations
|
|
269
|
-
addI18nLib('en', 'common', {
|
|
270
|
-
'yes': 'Yes',
|
|
271
|
-
'no': 'No'
|
|
272
|
-
})
|
|
273
|
-
|
|
274
|
-
// Add app-specific translations
|
|
275
|
-
addI18nApp('en', 'common', {
|
|
276
|
-
'yes': 'OK', // This will override the library translation
|
|
277
|
-
'save': 'Save Changes'
|
|
278
|
-
})
|
|
279
|
-
|
|
280
|
-
// Initialize resources (sorted by level and priority)
|
|
281
|
-
const resources = initI18nResource('en', 'common')
|
|
282
|
-
// resources will contain both library and app translations, with app taking priority
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
### Working with Namespaces
|
|
286
|
-
```typescript
|
|
287
|
-
import { addI18nLib, addI18nApp } from '@owlmeans/i18n'
|
|
288
|
-
|
|
289
|
-
// Add translations to specific namespace
|
|
290
|
-
addI18nLib('en', 'buttons', {
|
|
291
|
-
'save': 'Save',
|
|
292
|
-
'cancel': 'Cancel'
|
|
293
|
-
}, 'ui')
|
|
294
|
-
|
|
295
|
-
// Add with namespace in options
|
|
296
|
-
addI18nApp('en', 'forms', {
|
|
297
|
-
'required': 'This field is required'
|
|
298
|
-
}, { ns: 'validation' })
|
|
299
|
-
```
|
|
300
|
-
|
|
301
|
-
### Priority-based Loading
|
|
302
|
-
```typescript
|
|
303
|
-
import { addI18nLib, addI18nApp, addCommonI18n } from '@owlmeans/i18n'
|
|
304
|
-
|
|
305
|
-
// Add translations with different priorities
|
|
306
|
-
addI18nLib('en', 'messages', { 'welcome': 'Welcome' })
|
|
307
|
-
addI18nApp('en', 'messages', { 'welcome': 'Welcome to App' })
|
|
308
|
-
addCommonI18n('en', 'messages', { 'welcome': 'Service Welcome' })
|
|
309
|
-
|
|
310
|
-
// When initialized, service translation will have highest priority
|
|
311
|
-
const resources = initI18nResource('en', 'messages')
|
|
312
|
-
// The 'welcome' key will resolve to 'Service Welcome'
|
|
313
|
-
```
|
|
49
|
+
### `i18nStorage`
|
|
314
50
|
|
|
315
|
-
|
|
51
|
+
The global translation store. Read by platform-specific i18n adapters (e.g. `@owlmeans/client-i18n`).
|
|
316
52
|
|
|
317
|
-
|
|
318
|
-
- **types**: TypeScript interfaces and type definitions
|
|
319
|
-
- **consts**: Static values and constants
|
|
320
|
-
- **helper**: Consumer-facing utility functions
|
|
321
|
-
- **utils**: Internal utility functions for storage management
|
|
53
|
+
## Related Packages
|
|
322
54
|
|
|
323
|
-
|
|
55
|
+
- [`@owlmeans/client-i18n`](../client-i18n) — React i18next adapter that reads from `i18nStorage`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owlmeans/i18n",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -28,8 +28,9 @@
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
+
"@owlmeans/dep-config": "workspace:*",
|
|
31
32
|
"nodemon": "^3.1.11",
|
|
32
|
-
"typescript": "^
|
|
33
|
+
"typescript": "^6.0.2"
|
|
33
34
|
},
|
|
34
35
|
"publishConfig": {
|
|
35
36
|
"access": "public"
|
package/tsconfig.json
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": [
|
|
3
|
-
"
|
|
4
|
-
"
|
|
3
|
+
"@owlmeans/dep-config/tsconfig.base.json",
|
|
4
|
+
"@owlmeans/dep-config/tsconfig.react.json"
|
|
5
5
|
],
|
|
6
6
|
"compilerOptions": {
|
|
7
|
-
"rootDir": "./src/",
|
|
8
|
-
"outDir": "./build/"
|
|
9
|
-
"moduleResolution": "Bundler"
|
|
7
|
+
"rootDir": "./src/",
|
|
8
|
+
"outDir": "./build/"
|
|
10
9
|
},
|
|
11
|
-
"exclude": [
|
|
12
|
-
|
|
13
|
-
"./build/**/*",
|
|
14
|
-
"./*.ts"
|
|
15
|
-
]
|
|
16
|
-
}
|
|
10
|
+
"exclude": ["./dist/**/*", "./build/**/*", "./*.ts"]
|
|
11
|
+
}
|