ngx-localized-router 0.0.3 → 1.0.0

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.
Files changed (2) hide show
  1. package/README.md +190 -10
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,23 +1,203 @@
1
- # ngx-localized-router
1
+ # ✨ What is ngx-localized-router?
2
2
 
3
- tbd...
3
+ `ngx-localized-router` is a lightweight Angular library that helps you localize your application routes by adding language segments to the URL.
4
4
 
5
- ## Usage
6
- In your app config:
5
+ Examples of supported routes:
6
+ ```html
7
+ / → default language
8
+ /en → English
9
+ /de → German
10
+ /about
11
+ /en/about
12
+ /de/about
13
+ ```
14
+ It is built specifically for modern Angular (20+) and designed to work seamlessly with:
15
+
16
+ - ✅ Standalone APIs
17
+ - ✅ Angular Signals
18
+ - ✅ Zoneless applications
19
+ - ✅ Server-Side Rendering (SSR)
20
+
21
+ No hacks, no router monkey-patching — just clean, predictable localization.
22
+
23
+ ## 🚀 Features
24
+
25
+ - 🌐 Language prefixes in URLs (/en, /de, etc.)
26
+ - 🔁 Automatic language detection from the route
27
+ - 🧠 Signal-based APIs
28
+ - 🧩 Standalone-first (no required NgModules)
29
+ - ⚡ Zoneless compatible
30
+ - 🖥️ SSR-safe (Angular Universal / Node / Edge)
31
+ - 🌳 Tree-shakable & lightweight
32
+
33
+ ## 📦 Installation
34
+ ```bash
35
+ npm install ngx-localized-router
36
+ ```
7
37
 
38
+ ## 🛠️ Setup
39
+ `ngx-localized-router` integrates directly with Angular’s router by patching your routes and providing a small service to track and control the active language.
40
+
41
+ ### 1️⃣ Provide the localized router
42
+
43
+ Configure the library using provideNgxLocalizedRouter:
8
44
  ```typescript
45
+ import { ApplicationConfig } from '@angular/core';
46
+ import { provideRouter } from '@angular/router';
47
+ import { provideNgxLocalizedRouter, localizeRoutes } from 'ngx-localized-router';
48
+
49
+ export const appConfig: ApplicationConfig = {
50
+ providers: [
9
51
  provideNgxLocalizedRouter({
10
52
  defaultLanguage: 'de',
11
53
  languages: ['en', 'de'],
12
54
  languageResolved: (language: string) => {
13
- // Load translations, set app language etc...
55
+ // Set app language, load translations etc...
14
56
  },
15
57
  }),
16
- provideRouter(localizeRoutes(appRoutes))
58
+ provideRouter(localizeRoutes(appRoutes)),
59
+ ],
60
+ };
17
61
  ```
18
62
 
19
- Url localization:
63
+ ### 2️⃣ Wrap your routes with localizeRoutes()
20
64
 
21
- ```html
22
- <a class="nav__item" [routerLink]="'/some-route' | localizeRoute:'en'">Label</a>
23
- ```
65
+ Your original routes stay clean and language-agnostic:
66
+
67
+ ```typescript
68
+ export const appRoutes: Routes = [
69
+ {
70
+ path: '',
71
+ component: HomeComponent,
72
+ },
73
+ {
74
+ path: 'about',
75
+ component: AboutComponent,
76
+ },
77
+ ];
78
+ ```
79
+
80
+ After localization, the router will automatically support:
81
+
82
+ ```
83
+ /
84
+ /about
85
+ /en
86
+ /en/about
87
+ ```
88
+ Navigating to `/{defaultLanguage}` (e.g. `/de`) automatically redirects to `/`.
89
+
90
+ ## 🧠 Accessing Router Language (Signals)
91
+
92
+ ```typescript
93
+ import { inject } from '@angular/core';
94
+ import { NgxLocalizedRouterService } from 'ngx-localized-router';
95
+
96
+ export class HeaderComponent {
97
+ private localizedRouter = inject(NgxLocalizedRouterService);
98
+
99
+ language = this.localizedRouter.routeLanguage;
100
+ }
101
+ ```
102
+ Available signals:
103
+
104
+ - routeLanguage – currently active language
105
+ - defaultLanguage
106
+ - supportedLanguages
107
+
108
+ ## 🔔 Reacting to Language Changes
109
+
110
+ If you prefer an observable-style API:
111
+
112
+ ```typescript
113
+ this.localizedRouter.routeLanguageChanged.subscribe((language) => {
114
+ console.log('Language changed to:', language);
115
+ });
116
+ ```
117
+
118
+ This only emits when the language actually changes.
119
+
120
+ ## 🔗 Localizing URLs Programmatically
121
+
122
+ ```typescript
123
+ this.localizedRouter.localizeUrl('/about', 'en');
124
+ // → /en/about
125
+
126
+ this.localizedRouter.localizeUrl('/en/about', 'de');
127
+ // → /about
128
+ ```
129
+
130
+ Notes:
131
+ - Existing language prefixes are automatically removed
132
+ - The default language is never added to the URL
133
+
134
+ ### 🧵 localizeRoute Pipe (Templates)
135
+ For templates, ngx-localized-router provides the localizeRoute pipe, which wraps the same logic as localizeUrl.
136
+
137
+ This is the recommended way to localize links in templates.
138
+
139
+ **Example**
140
+ ```angular181html
141
+ <a [routerLink]="'/about' | localizeRoute : 'en'">
142
+ About (EN)
143
+ </a>
144
+
145
+ <a [routerLink]="'/about' | localizeRoute : 'de'">
146
+ Über uns (DE)
147
+ </a>
148
+ ```
149
+
150
+
151
+ You can also pass route segments as an array:
152
+ ```angular181html
153
+ <a [routerLink]="['about', 'team'] | localizeRoute : 'en'">
154
+ Team
155
+ </a>
156
+ ```
157
+
158
+ #### What the pipe does
159
+
160
+ Internally, the pipe delegates to NgxLocalizedRouterService.localizeUrl():
161
+
162
+ - Removes an existing language segment (if present)
163
+ - Adds the requested language prefix
164
+ - Omits the prefix for the default language
165
+
166
+ This ensures consistent URL generation across:
167
+
168
+ - Templates
169
+ - Components
170
+ - Services
171
+
172
+ ## 🌐 SSR & Hydration Friendly
173
+ `ngx-localized-router` is fully SSR-safe:
174
+
175
+ - No access to window or document
176
+ - Works with Angular Universal & hydration
177
+ - Deterministic language resolution on server & client
178
+
179
+ The optional languageResolved callback is invoked once, after the initial navigation:
180
+
181
+ ```typescript
182
+ languageResolved: (language) => {
183
+ // Ideal place to:
184
+ // - initialize translations
185
+ // - sync analytics
186
+ // - preload language-specific data
187
+ }
188
+ ```
189
+
190
+ ## ⚡ Zoneless & Signal-First
191
+
192
+ - No dependency on zone.js
193
+ - Uses Angular signals internally
194
+ - Plays nicely with zoneless change detection
195
+ - Minimal runtime overhead
196
+
197
+ ## 🎯 When to Use This Library
198
+ Use ngx-localized-router if you want:
199
+
200
+ - Clean, SEO-friendly localized URLs
201
+ - No coupling to translation libraries
202
+ - Full control over language resolution
203
+ - Modern Angular patterns only
package/package.json CHANGED
@@ -7,11 +7,11 @@
7
7
  "Router",
8
8
  "Translate router"
9
9
  ],
10
- "version": "0.0.3",
10
+ "version": "1.0.0",
11
11
  "license": "MIT",
12
12
  "peerDependencies": {
13
13
  "@angular/common": ">=20.0.0",
14
- "@angular/core": ">=21.0.0"
14
+ "@angular/core": ">=20.0.0"
15
15
  },
16
16
  "bugs": {
17
17
  "url": "https://github.com/odomanskyi/ngx-localized-router/issues"