Skip to content

Commit

Permalink
Merge pull request #127 from ruizmarc/master
Browse files Browse the repository at this point in the history
Update to angular 14 and fix angular breaking changes
  • Loading branch information
gilsdav authored Jul 25, 2022
2 parents 1f1f4eb + 0b8db6d commit adaf3fa
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 81 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Based on and extension of [ngx-translate](https://github.com/ngx-translate/core)
| 8 | 2.2.3 | 1.1.0 | legacy |
| 8 - 12 | 3.1.9 | 1.1.2 | active |
| 13 | 4.0.1 | 2.0.0 | active |

| 14 | 5.0.0 | 2.0.0 | active |

Demo project can be found under sub folder `src`.

Expand Down Expand Up @@ -521,20 +521,21 @@ Usage example:
export const appInitializerFactory = (injector: Injector) => {
return () => {
const localize = injector.get(LocalizeRouterService);
return localize.hooks.initialized
.pipe(
tap(() => {
const router = injector.get(Router);
router.events.pipe(
filter(url => url instanceof NavigationEnd),
first()
).subscribe((route: NavigationEnd) => {
console.log(router.url, route.url);
router.navigate(['/fr/accueil']);
});
})
return firstValueFrom(
localize.hooks.initialized
.pipe(
tap(() => {
const router = injector.get(Router);
router.events.pipe(
filter(url => url instanceof NavigationEnd),
first()
).subscribe((route: NavigationEnd) => {
console.log(router.url, route.url);
router.navigate(['/fr/accueil']);
});
})
)
)
.toPromise();
}
};
```
Expand Down
3 changes: 1 addition & 2 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,5 @@
}
}
}
},
"defaultProject": "ngx-translate-router-demo"
}
}
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
},
"private": true,
"dependencies": {
"@angular/animations": "^13.1.0",
"@angular/common": "^13.1.0",
"@angular/compiler": "^13.1.0",
"@angular/core": "^13.1.0",
"@angular/platform-browser": "^13.1.0",
"@angular/platform-browser-dynamic": "^13.1.0",
"@angular/router": "^13.1.0",
"@angular/animations": "^14.0.5",
"@angular/common": "^14.0.5",
"@angular/compiler": "^14.0.5",
"@angular/core": "^14.0.5",
"@angular/platform-browser": "^14.0.5",
"@angular/platform-browser-dynamic": "^14.0.5",
"@angular/router": "^14.0.5",
"@ngx-translate/core": "^14.0.0",
"@ngx-translate/http-loader": "^7.0.0",
"@scullyio/scully": "0.0.102",
Expand All @@ -29,11 +29,11 @@
"zone.js": "~0.11.4"
},
"devDependencies": {
"@angular/compiler-cli": "^13.1.0",
"@angular-devkit/build-angular": "^13.1.0",
"@angular/cli": "^13.1.0",
"@angular/language-service": "^13.1.0",
"ng-packagr": "^13.1.0",
"@angular/compiler-cli": "^14.0.5",
"@angular-devkit/build-angular": "^14.0.5",
"@angular/cli": "^14.0.5",
"@angular/language-service": "^14.0.5",
"ng-packagr": "^14.0.3",
"@types/node": "^16.11.6",
"@types/jasmine": "~3.10.2",
"@types/jasminewd2": "~2.0.8",
Expand All @@ -47,6 +47,6 @@
"karma-jasmine-html-reporter": "^1.7.0",
"protractor": "~7.0.0",
"ts-node": "~10.4.0",
"typescript": "~4.4.4"
"typescript": "~4.7.4"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"target": "es2015",
"target": "es2020",
"module": "es2015",
"moduleResolution": "node",
"declaration": true,
Expand Down
8 changes: 4 additions & 4 deletions projects/ngx-translate-router/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gilsdav/ngx-translate-router",
"version": "4.0.1",
"version": "5.0.0",
"homepage": "https://github.com/gilsdav/ngx-translate-router#readme",
"license" : "MIT",
"author": {
Expand All @@ -9,9 +9,9 @@
"url": "https://github.com/gilsdav"
},
"peerDependencies": {
"@angular/common": ">=13.0.0",
"@angular/core": ">=13.0.0",
"@angular/router": ">=13.0.0",
"@angular/common": ">=14.0.0",
"@angular/core": ">=14.0.0",
"@angular/router": ">=14.0.0",
"@ngx-translate/core": ">=11.0.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export class ParserInitializer {
if (settings.initialNavigation) {
return new Promise<void>(resolve => {
// @ts-ignore
const oldAfterPreactivation = router.hooks.afterPreactivation;
const oldAfterPreactivation = router.afterPreactivation;
let firstInit = true;
// @ts-ignore
router.hooks.afterPreactivation = () => {
router.afterPreactivation = () => {
if (firstInit) {
resolve();
firstInit = false;
Expand Down
33 changes: 18 additions & 15 deletions projects/ngx-translate-router/src/lib/localize-router.parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Routes, Route, NavigationExtras, Params } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { Observable, Observer } from 'rxjs';
import { firstValueFrom, Observable, Observer } from 'rxjs';
import { Location } from '@angular/common';
import { CacheMechanism, LocalizeRouterSettings } from './localize-router.config';
import { Inject, Injectable } from '@angular/core';
Expand Down Expand Up @@ -83,7 +83,7 @@ export abstract class LocalizeParser {
let children: Routes = [];
/** if set prefix is enforced */
if (this.settings.alwaysSetPrefix) {
const baseRoute = { path: '', redirectTo: this.defaultLang, pathMatch: 'full' };
const baseRoute: Route = { path: '', redirectTo: this.defaultLang, pathMatch: 'full' };

/** extract potential wildcard route */
const wildcardIndex = routes.findIndex((route: Route) => route.path === '**');
Expand Down Expand Up @@ -123,8 +123,9 @@ export abstract class LocalizeParser {
}

/** translate routes */
const res = this.translateRoutes(selectedLanguage);
return res.toPromise();
return firstValueFrom(
this.translateRoutes(selectedLanguage)
);
}

initChildRoutes(routes: Routes) {
Expand Down Expand Up @@ -169,23 +170,25 @@ export abstract class LocalizeParser {
*/
private _translateRouteTree(routes: Routes): void {
routes.forEach((route: Route) => {
const skipRouteLocalization = (route.data && route.data['skipRouteLocalization']);
const skipRouteLocalization = (route.data && route.data['skipRouteLocalization']);
const localizeRedirection = !skipRouteLocalization || skipRouteLocalization['localizeRedirectTo'];

if (route.redirectTo && localizeRedirection) {
this._translateProperty(route, 'redirectTo', !route.redirectTo.indexOf('/'));
}

if (!skipRouteLocalization) {
if (route.path !== null && route.path !== undefined/* && route.path !== '**'*/) {
this._translateProperty(route, 'path');
}
if (route.children) {
this._translateRouteTree(route.children);
}
if (route.loadChildren && (<any>route)._loadedConfig) {
this._translateRouteTree((<any>route)._loadedConfig.routes);
}
if (skipRouteLocalization) {
return;
}

if (route.path !== null && route.path !== undefined/* && route.path !== '**'*/) {
this._translateProperty(route, 'path');
}
if (route.children) {
this._translateRouteTree(route.children);
}
if (route.loadChildren && (<any>route)._loadedRoutes?.length) {
this._translateRouteTree((<any>route)._loadedRoutes);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export class LocalizeRouterService {
* Traverses through the tree to assemble new translated url
*/
private traverseRouteSnapshot(snapshot: ActivatedRouteSnapshot): string {

if (snapshot.firstChild && snapshot.routeConfig) {
return `${this.parseSegmentValue(snapshot)}/${this.traverseRouteSnapshot(snapshot.firstChild)}`;
} else if (snapshot.firstChild) {
Expand Down Expand Up @@ -242,7 +241,7 @@ export class LocalizeRouterService {
this.applyConfigToRouter(this.parser.routes);
// Clear global extras
this.lastExtras = undefined;
// Init new navigation with same url to take new congif in consideration
// Init new navigation with same url to take new config in consideration
this.router.navigateByUrl(currentEvent.url, lastExtras);
// Fire route change event
this.routerEvents.next(currentLang);
Expand Down
12 changes: 8 additions & 4 deletions projects/ngx-translate-router/src/lib/localized-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ export class LocalizedRouter extends Router {
const isBrowser = isPlatformBrowser(platformId);
// __proto__ is needed for preloaded modules be doesn't work with SSR
// @ts-ignore
const configLoader = isBrowser ? this.configLoader.__proto__ : this.configLoader;
configLoader.loadModuleFactory = (loadChildren: LoadChildren) => {
const configLoader = (isBrowser ? this.configLoader.__proto__ : this.configLoader);

configLoader.loadModuleFactoryOrRoutes = (loadChildren: LoadChildren) => {
return wrapIntoObservable(loadChildren()).pipe(mergeMap((t: any) => {
let compiled: Observable<NgModuleFactory<any>>;
if (t instanceof NgModuleFactory) {
let compiled: Observable<NgModuleFactory<any> | Array<any>>;
if (t instanceof NgModuleFactory || Array.isArray(t)) {
compiled = of(t);
} else {
compiled = from(compiler.compileModuleAsync(t)) as Observable<NgModuleFactory<any>>;
}
return compiled.pipe(map(factory => {
if (Array.isArray(factory)) {
return factory;
}
return {
moduleType: factory.moduleType,
create: (parentInjector: Injector) => {
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-translate-router/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"target": "es2015",
"target": "es2020",
"module": "es2015",
"moduleResolution": "node",
"declaration": true,
Expand Down
42 changes: 22 additions & 20 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { AppRoutingModule } from './app-routing.module';
import { NotFoundComponent } from './not-found/not-found.component';
import { firstValueFrom } from 'rxjs';
import { filter, first, tap } from 'rxjs/operators';
import { NavigationEnd, Router } from '@angular/router';

Expand All @@ -20,20 +21,21 @@ export function createTranslateLoader(http: HttpClient) {
export const appInitializerFactory = (injector: Injector) => {
return () => {
const localize = injector.get(LocalizeRouterService);
return localize.hooks.initialized
.pipe(
tap(() => {
const router = injector.get(Router);
router.events.pipe(
filter(url => url instanceof NavigationEnd),
first()
).subscribe((route: NavigationEnd) => {
console.log(router.url, route.url);
router.navigate(['/fr/testounet/bobie']);
});
})
)
.toPromise();
return firstValueFrom(
localize.hooks.initialized
.pipe(
tap(() => {
const router = injector.get(Router);
router.events.pipe(
filter(url => url instanceof NavigationEnd),
first()
).subscribe((route: NavigationEnd) => {
console.log(router.url, route.url);
router.navigate(['/fr/testounet/bobie']);
});
})
)
);
}
};

Expand All @@ -48,11 +50,11 @@ export const appInitializerFactory = (injector: Injector) => {
HttpClientModule,
AppRoutingModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient]
}
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient]
}
})
],
// providers: [
Expand All @@ -65,4 +67,4 @@ export const appInitializerFactory = (injector: Injector) => {
// ],
bootstrap: [AppComponent]
})
export class AppModule {}
export class AppModule { }
2 changes: 1 addition & 1 deletion src/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
import 'core-js/es/reflect';
// import 'core-js/es/reflect';

/**
* By default, zone.js will patch all possible macroTask and DomEvents
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es5",
"target": "es2020",
"typeRoots": [
"node_modules/@types"
],
Expand Down

0 comments on commit adaf3fa

Please sign in to comment.