@cocoar/localization 0.1.0-beta.114 → 0.1.0-beta.115
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 +49 -69
- package/fesm2022/cocoar-localization.mjs +1272 -1325
- package/fesm2022/cocoar-localization.mjs.map +1 -1
- package/package.json +2 -2
- package/types/cocoar-localization.d.ts +113 -187
package/README.md
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
# @cocoar/localization
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Complete localization system for the Cocoar Design System.
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
This library provides centralized language management for Cocoar applications. It serves as the single source of truth for the current language and
|
|
7
|
+
This library provides centralized language management, formatting (L10n), and translations (i18n) for Cocoar applications. It serves as the single source of truth for the current language and automatically synchronizes all localization systems.
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
|
-
- **
|
|
12
|
-
- **
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
11
|
+
- **Language Management** - Centralized language state with Signal-based API
|
|
12
|
+
- **L10n (Localization)** - Date, number, currency, percent formatting with browser Intl API + optional HTTP overrides
|
|
13
|
+
- **i18n (Internationalization)** - Translation system with automatic loading and parameter interpolation
|
|
14
|
+
- **Automatic Synchronization** - Language changes automatically trigger L10n and i18n updates
|
|
15
|
+
- **Reactive API** - Angular Signals for reactive language and translation updates
|
|
15
16
|
- **Lightweight** - Zero dependencies beyond Angular core and RxJS
|
|
16
17
|
|
|
17
18
|
## Installation
|
|
@@ -20,11 +21,9 @@ This library provides centralized language management for Cocoar applications. I
|
|
|
20
21
|
pnpm add @cocoar/localization
|
|
21
22
|
```
|
|
22
23
|
|
|
23
|
-
##
|
|
24
|
+
## Quick Start
|
|
24
25
|
|
|
25
|
-
###
|
|
26
|
-
|
|
27
|
-
Configure available languages and default language using `provideCoarLocalization()`:
|
|
26
|
+
### Minimal Setup (L10n only)
|
|
28
27
|
|
|
29
28
|
```typescript
|
|
30
29
|
import { ApplicationConfig } from '@angular/core';
|
|
@@ -32,15 +31,47 @@ import { provideCoarLocalization } from '@cocoar/localization';
|
|
|
32
31
|
|
|
33
32
|
export const appConfig: ApplicationConfig = {
|
|
34
33
|
providers: [
|
|
34
|
+
// Core system: language management + Intl formatting
|
|
35
35
|
provideCoarLocalization({
|
|
36
|
-
availableLanguages: ['en', 'de', 'fr'],
|
|
37
36
|
defaultLanguage: 'en',
|
|
38
37
|
}),
|
|
39
38
|
],
|
|
40
39
|
};
|
|
41
40
|
```
|
|
42
41
|
|
|
43
|
-
|
|
42
|
+
This gives you:
|
|
43
|
+
- Language management (`CoarLocalizationService`)
|
|
44
|
+
- Browser Intl API for formatting (date, number, currency, percent)
|
|
45
|
+
- i18n system (but no translation loader)
|
|
46
|
+
|
|
47
|
+
### With HTTP Sources (L10n + i18n)
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { ApplicationConfig } from '@angular/core';
|
|
51
|
+
import { provideHttpClient } from '@angular/common/http';
|
|
52
|
+
import {
|
|
53
|
+
provideCoarLocalization,
|
|
54
|
+
provideCoarL10nHttpSource,
|
|
55
|
+
provideCoarI18nHttpSource,
|
|
56
|
+
} from '@cocoar/localization';
|
|
57
|
+
|
|
58
|
+
export const appConfig: ApplicationConfig = {
|
|
59
|
+
providers: [
|
|
60
|
+
provideHttpClient(),
|
|
61
|
+
|
|
62
|
+
// Core system
|
|
63
|
+
provideCoarLocalization({
|
|
64
|
+
defaultLanguage: 'en',
|
|
65
|
+
}),
|
|
66
|
+
|
|
67
|
+
// Optional: L10n HTTP overrides for business rules
|
|
68
|
+
provideCoarL10nHttpSource(), // Defaults to /locales/{lang}.json
|
|
69
|
+
|
|
70
|
+
// Optional: i18n HTTP translations
|
|
71
|
+
provideCoarI18nHttpSource(), // Defaults to /i18n/{lang}.json
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
```
|
|
44
75
|
|
|
45
76
|
```typescript
|
|
46
77
|
import { Component, inject, effect } from '@angular/core';
|
|
@@ -141,14 +172,14 @@ export class MyService {
|
|
|
141
172
|
|
|
142
173
|
### provideCoarLocalization()
|
|
143
174
|
|
|
144
|
-
Configures the locale system with
|
|
175
|
+
Configures the locale system with the default language.
|
|
176
|
+
|
|
177
|
+
**Note:** Browser Intl API is automatically included as the first localization source.
|
|
145
178
|
|
|
146
179
|
```typescript
|
|
147
180
|
function provideCoarLocalization(config: CoarLocalizationConfig): EnvironmentProviders;
|
|
148
181
|
|
|
149
182
|
interface CoarLocalizationConfig {
|
|
150
|
-
/** All languages available in the application */
|
|
151
|
-
availableLanguages: string[];
|
|
152
183
|
/** The default/fallback language */
|
|
153
184
|
defaultLanguage: string;
|
|
154
185
|
}
|
|
@@ -158,7 +189,6 @@ interface CoarLocalizationConfig {
|
|
|
158
189
|
|
|
159
190
|
```typescript
|
|
160
191
|
provideCoarLocalization({
|
|
161
|
-
availableLanguages: ['en', 'de', 'fr', 'es'],
|
|
162
192
|
defaultLanguage: 'en',
|
|
163
193
|
});
|
|
164
194
|
```
|
|
@@ -173,65 +203,15 @@ provideCoarLocalization({
|
|
|
173
203
|
#### Methods
|
|
174
204
|
|
|
175
205
|
- **`getCurrentLanguage(): string`** - Returns the current language code
|
|
176
|
-
- **`setLanguage(language: string): void
|
|
177
|
-
- **`getAvailableLanguages(): string[]`** - Returns all available languages (from config)
|
|
206
|
+
- **`setLanguage(language: string): Promise<void>`** - Sets the current language (async to load data)
|
|
178
207
|
- **`getDefaultLanguage(): string`** - Returns the default language (from config)
|
|
179
208
|
|
|
180
209
|
## Default Configuration
|
|
181
210
|
|
|
182
211
|
If `provideCoarLocalization()` is not called, the service uses these defaults:
|
|
183
212
|
- **Default language**: `'en'`
|
|
184
|
-
- **
|
|
185
|
-
|
|
186
|
-
},
|
|
187
|
-
],
|
|
188
|
-
};
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
## Integration with i18n
|
|
193
|
-
|
|
194
|
-
The `@cocoar/localization` library includes a complete i18n system with built-in HTTP loader.
|
|
195
|
-
|
|
196
|
-
**Example:**
|
|
197
|
-
|
|
198
|
-
```typescript
|
|
199
|
-
import { ApplicationConfig } from '@angular/core';
|
|
200
|
-
import { provideHttpClient } from '@angular/common/http';
|
|
201
|
-
import { provideCoarLocalization, provideCoarI18n } from '@cocoar/localization';
|
|
202
|
-
|
|
203
|
-
export const appConfig: ApplicationConfig = {
|
|
204
|
-
providers: [
|
|
205
|
-
provideHttpClient(),
|
|
206
|
-
|
|
207
|
-
// 1. Configure locale (languages and default)
|
|
208
|
-
provideCoarLocalization({
|
|
209
|
-
availableLanguages: ['en', 'de', 'fr'],
|
|
210
|
-
defaultLanguage: 'en',
|
|
211
|
-
}),
|
|
212
|
-
|
|
213
|
-
// 2. Set up i18n (loads from /i18n/en.json, /i18n/de.json, etc.)
|
|
214
|
-
provideCoarI18n(),
|
|
215
|
-
],
|
|
216
|
-
};
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
For custom loaders (SignalR, static imports, etc.), implement `CoarTranslationLoader`.
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
This service is designed to be the foundation for both i18n (translations) and localization (formatting). Other systems can subscribe to `languageChanged$` to react to language changes:
|
|
223
|
-
|
|
224
|
-
```typescript
|
|
225
|
-
// Example: i18n integration (future)
|
|
226
|
-
locale.languageChanged$.subscribe((lang) => {
|
|
227
|
-
translationService.loadLanguage(lang);
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
// Example: localization integration (future)
|
|
231
|
-
locale.languageChanged$.subscribe((lang) => {
|
|
232
|
-
formattingService.setLocale(lang);
|
|
233
|
-
});
|
|
234
|
-
```
|
|
213
|
+
- **L10n source**: Browser Intl API (always included automatically)
|
|
214
|
+
- **i18n service**: Available but no translation loader (use `provideCoarI18nHttpSource()` to add one)
|
|
235
215
|
|
|
236
216
|
## License
|
|
237
217
|
|