@cocoar/localization 0.1.0-beta.114
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 +238 -0
- package/fesm2022/cocoar-localization.mjs +1752 -0
- package/fesm2022/cocoar-localization.mjs.map +1 -0
- package/package.json +49 -0
- package/types/cocoar-localization.d.ts +1194 -0
package/README.md
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
# @cocoar/localization
|
|
2
|
+
|
|
3
|
+
Locale and language management for the Cocoar Design System.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This library provides centralized language management for Cocoar applications. It serves as the single source of truth for the current language and notifies other systems (i18n, localization/formatting) about language changes.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **CoarLocalizationService** - Language state management
|
|
12
|
+
- **Centralized Configuration** - Single source of truth for available languages
|
|
13
|
+
- **Signal-based API** - Reactive language updates using Angular Signals
|
|
14
|
+
- **Observable API** - RxJS-based language change notifications
|
|
15
|
+
- **Lightweight** - Zero dependencies beyond Angular core and RxJS
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add @cocoar/localization
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Configuration
|
|
26
|
+
|
|
27
|
+
Configure available languages and default language using `provideCoarLocalization()`:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { ApplicationConfig } from '@angular/core';
|
|
31
|
+
import { provideCoarLocalization } from '@cocoar/localization';
|
|
32
|
+
|
|
33
|
+
export const appConfig: ApplicationConfig = {
|
|
34
|
+
providers: [
|
|
35
|
+
provideCoarLocalization({
|
|
36
|
+
availableLanguages: ['en', 'de', 'fr'],
|
|
37
|
+
defaultLanguage: 'en',
|
|
38
|
+
}),
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Basic Usage
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { Component, inject, effect } from '@angular/core';
|
|
47
|
+
import { CoarLocalizationService } from '@cocoar/localization';
|
|
48
|
+
|
|
49
|
+
@Component({
|
|
50
|
+
selector: 'app-language-switcher',
|
|
51
|
+
template: `
|
|
52
|
+
<div>
|
|
53
|
+
<p>Current language: {{ locale.language() }}</p>
|
|
54
|
+
<button (click)="switchToGerman()">Deutsch</button>
|
|
55
|
+
<button (click)="switchToEnglish()">English</button>
|
|
56
|
+
</div>
|
|
57
|
+
`,
|
|
58
|
+
})
|
|
59
|
+
export class LanguageSwitcherComponent {
|
|
60
|
+
readonly locale = inject(CoarLocalizationService);
|
|
61
|
+
|
|
62
|
+
constructor() {
|
|
63
|
+
// React to language changes using effects
|
|
64
|
+
effect(() => {
|
|
65
|
+
console.log('Language changed to:', this.locale.language());
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
switchToGerman() {
|
|
70
|
+
this.locale.setLanguage('de');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
switchToEnglish() {
|
|
74
|
+
this.locale.setLanguage('en');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Using the Signal
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { Component, inject, computed } from '@angular/core';
|
|
83
|
+
import { CoarLocalizationService } from '@cocoar/localization';
|
|
84
|
+
|
|
85
|
+
@Component({
|
|
86
|
+
selector: 'app-example',
|
|
87
|
+
template: `<h1>{{ greeting() }}</h1>`,
|
|
88
|
+
})
|
|
89
|
+
export class ExampleComponent {
|
|
90
|
+
private readonly locale = inject(CoarLocalizationService);
|
|
91
|
+
|
|
92
|
+
readonly greeting = computed(() => {
|
|
93
|
+
const lang = this.locale.language();
|
|
94
|
+
return lang === 'de' ? 'Hallo Welt' : 'Hello World';
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Using the Observable
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { Component, inject, OnInit } from '@angular/core';
|
|
103
|
+
import { CoarLocalizationService } from '@cocoar/localization';
|
|
104
|
+
|
|
105
|
+
@Component({
|
|
106
|
+
selector: 'app-example',
|
|
107
|
+
template: `<p>Language changes: {{ changeCount }}</p>`,
|
|
108
|
+
})
|
|
109
|
+
export class ExampleComponent implements OnInit {
|
|
110
|
+
private readonly locale = inject(CoarLocalizationService);
|
|
111
|
+
changeCount = 0;
|
|
112
|
+
|
|
113
|
+
ngOnInit() {
|
|
114
|
+
// Subscribe to language changes
|
|
115
|
+
this.locale.languageChanged$.subscribe((newLang) => {
|
|
116
|
+
console.log('Language changed to:', newLang);
|
|
117
|
+
this.changeCount++;
|
|
118
|
+
// Reload data, update formatting, etc.
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Getting Current Language
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { inject } from '@angular/core';
|
|
128
|
+
import { CoarLocalizationService } from '@cocoar/localization';
|
|
129
|
+
|
|
130
|
+
export class MyService {
|
|
131
|
+
private readonly locale = inject(CoarLocalizationService);
|
|
132
|
+
|
|
133
|
+
doWork() {
|
|
134
|
+
const currentLang = this.locale.getCurrentLanguage();
|
|
135
|
+
console.log('Current language:', currentLang);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## API
|
|
141
|
+
|
|
142
|
+
### provideCoarLocalization()
|
|
143
|
+
|
|
144
|
+
Configures the locale system with available languages and default language.
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
function provideCoarLocalization(config: CoarLocalizationConfig): EnvironmentProviders;
|
|
148
|
+
|
|
149
|
+
interface CoarLocalizationConfig {
|
|
150
|
+
/** All languages available in the application */
|
|
151
|
+
availableLanguages: string[];
|
|
152
|
+
/** The default/fallback language */
|
|
153
|
+
defaultLanguage: string;
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**Example:**
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
provideCoarLocalization({
|
|
161
|
+
availableLanguages: ['en', 'de', 'fr', 'es'],
|
|
162
|
+
defaultLanguage: 'en',
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### CoarLocalizationService
|
|
167
|
+
|
|
168
|
+
#### Properties
|
|
169
|
+
|
|
170
|
+
- **`language: Signal<string>`** - Signal containing the current language code
|
|
171
|
+
- **`languageChanged$: Observable<string>`** - Observable that emits when language changes
|
|
172
|
+
|
|
173
|
+
#### Methods
|
|
174
|
+
|
|
175
|
+
- **`getCurrentLanguage(): string`** - Returns the current language code
|
|
176
|
+
- **`setLanguage(language: string): void`** - Sets the current language
|
|
177
|
+
- **`getAvailableLanguages(): string[]`** - Returns all available languages (from config)
|
|
178
|
+
- **`getDefaultLanguage(): string`** - Returns the default language (from config)
|
|
179
|
+
|
|
180
|
+
## Default Configuration
|
|
181
|
+
|
|
182
|
+
If `provideCoarLocalization()` is not called, the service uses these defaults:
|
|
183
|
+
- **Default language**: `'en'`
|
|
184
|
+
- **Available languages**: `['en']`
|
|
185
|
+
deps: [CoarLocalizationService],
|
|
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
|
+
```
|
|
235
|
+
|
|
236
|
+
## License
|
|
237
|
+
|
|
238
|
+
Apache-2.0
|