@flusys/ng-localization 1.2.0-rc.1
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 +144 -0
- package/fesm2022/flusys-ng-localization.mjs +2556 -0
- package/fesm2022/flusys-ng-localization.mjs.map +1 -0
- package/package.json +33 -0
- package/types/flusys-ng-localization.d.ts +399 -0
package/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# @flusys/ng-localization
|
|
2
|
+
|
|
3
|
+
Database-driven i18n for FLUSYS Angular apps — translations loaded from the backend at startup, signal-reactive `TranslatePipe`, RTL/LTR auto-switching, and a built-in language selector for the topbar.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@flusys/ng-localization)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @flusys/ng-localization
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## 1. Register Providers
|
|
17
|
+
|
|
18
|
+
Call `provideLocalization()` once in `app.config.ts`. It registers ``(consumed by`TranslatePipe`from`ng-shared`), an `APP_INITIALIZER` that loads translations before the first render, and optionally the topbar language selector.
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
// app.config.ts
|
|
22
|
+
import { provideLocalization } from "@flusys/ng-localization";
|
|
23
|
+
|
|
24
|
+
export const appConfig: ApplicationConfig = {
|
|
25
|
+
providers: [
|
|
26
|
+
provideLocalization({
|
|
27
|
+
defaultLanguageCode: "en", // required — fallback when no saved preference
|
|
28
|
+
loadStrategy: "modules", // optional — 'modules' (default) or 'all'
|
|
29
|
+
initialModules: [], // optional — modules to pre-load at startup
|
|
30
|
+
enableLayoutSelector: true, // optional — renders language picker in AppTopbar
|
|
31
|
+
}),
|
|
32
|
+
],
|
|
33
|
+
};
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`loadStrategy: 'modules'` defers loading until a route resolver requests a module. Use `'all'` only for small translation sets.
|
|
37
|
+
|
|
38
|
+
## 2. Use TranslatePipe in Templates
|
|
39
|
+
|
|
40
|
+
Import `TranslatePipe` from `@flusys/ng-shared` (not from this package). The pipe is signal-reactive and re-renders automatically on language change.
|
|
41
|
+
language
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { TranslatePipe } from "@flusys/ng-shared";
|
|
45
|
+
|
|
46
|
+
@Component({
|
|
47
|
+
imports: [TranslatePipe],
|
|
48
|
+
template: `
|
|
49
|
+
<h1>{{ "dashboard.title" | translate }}</h1>
|
|
50
|
+
<button>{{ "shared.save" | translate }}</button>
|
|
51
|
+
<input [placeholder]="'form.search.placeholder' | translate" />
|
|
52
|
+
|
|
53
|
+
<!-- With interpolation -->
|
|
54
|
+
<p>{{ "pagination.showing" | translate: { from: 1, to: 10, total: 100 } }}</p>
|
|
55
|
+
`,
|
|
56
|
+
})
|
|
57
|
+
export class DashboardComponent {}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Translation values stored in the database use `{{variableName}}` syntax:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
Key: pagination.showing
|
|
64
|
+
Value: Showing {{from}}-{{to}} of {{total}} results
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## 3. Load Translations Per Route (Module Strategy)
|
|
68
|
+
|
|
69
|
+
Use `resolveTranslationModule()` in your route definition so translations for a feature load before the page renders. Pass `fallbackMessages` so the UI works even without a backend.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
// Inside a feature's routes file
|
|
73
|
+
import { resolveTranslationModule } from "@flusys/ng-shared";
|
|
74
|
+
import { MY_FEATURE_MESSAGES } from "./constants/messages";
|
|
75
|
+
|
|
76
|
+
export const MY_ROUTES: Routes = [
|
|
77
|
+
{
|
|
78
|
+
path: "",
|
|
79
|
+
resolve: {
|
|
80
|
+
translations: resolveTranslationModule({
|
|
81
|
+
modules: ["myFeature"], // module name used by the backend
|
|
82
|
+
fallbackMessages: MY_FEATURE_MESSAGES, // static fallback map
|
|
83
|
+
}),
|
|
84
|
+
},
|
|
85
|
+
loadComponent: () => import("./my-feature-container.component").then((m) => m.MyFeatureContainerComponent),
|
|
86
|
+
children: [
|
|
87
|
+
/* ... */
|
|
88
|
+
],
|
|
89
|
+
},
|
|
90
|
+
];
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Already-loaded modules are cached — navigating back to the same route does not re-fetch.
|
|
94
|
+
|
|
95
|
+
## 4. Translate Imperatively in Services / Components
|
|
96
|
+
|
|
97
|
+
Inject `TranslateService` when you need a string outside a template (e.g., toast messages, computed labels).
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { inject } from "@angular/core";
|
|
101
|
+
import { TranslateService } from "@flusys/ng-localization";
|
|
102
|
+
|
|
103
|
+
export class MyComponent {
|
|
104
|
+
private readonly translate = inject(TranslateService);
|
|
105
|
+
|
|
106
|
+
// Synchronous — returns fallback key if not found
|
|
107
|
+
getLabel(): string {
|
|
108
|
+
return this.translate.translate("shared.save");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// With params
|
|
112
|
+
getCountLabel(count: number): string {
|
|
113
|
+
return this.translate.translate("items.count", { count });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Signal-based — updates automatically on language change
|
|
117
|
+
readonly pageTitle = this.translate.translateSignal("dashboard.title");
|
|
118
|
+
|
|
119
|
+
// Read current language code
|
|
120
|
+
readonly lang = this.translate.languageCode; // Signal<string>
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## 5. Add Admin Routes (Language and Key Management)
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
// app.routes.ts
|
|
128
|
+
import { LOCALIZATION_ROUTES } from "@flusys/ng-localization";
|
|
129
|
+
|
|
130
|
+
export const routes: Routes = [
|
|
131
|
+
{
|
|
132
|
+
path: "localization",
|
|
133
|
+
loadChildren: () => LOCALIZATION_ROUTES,
|
|
134
|
+
},
|
|
135
|
+
];
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## RTL Support
|
|
139
|
+
|
|
140
|
+
When the user switches to an RTL language, `LocalizationStateService` sets `document.documentElement.dir` and `lang` automatically. Tailwind `rtl:` utility classes and component RTL support both activate from this single attribute change — no extra configuration needed.
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
MIT © FLUSYS
|