Skip to content

Commit

Permalink
Angular 8 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
HMubaireek committed Jun 1, 2019
1 parent ca881c2 commit 550139f
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 145 deletions.
42 changes: 21 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,44 @@
},
"private": true,
"dependencies": {
"@angular/common": "^6.0.2",
"@angular/core": "^6.0.2",
"@angular/compiler": "^6.0.2",
"@angular/animations": "^6.0.2",
"@angular/forms": "^6.0.2",
"@angular/http": "^6.0.2",
"@angular/platform-browser": "^6.0.2",
"@angular/platform-browser-dynamic": "^6.0.2",
"@angular/router": "^6.0.2",
"@angular/animations": "^8.0.0",
"@angular/common": "^8.0.0",
"@angular/compiler": "^8.0.0",
"@angular/core": "^8.0.0",
"@angular/forms": "^8.0.0",
"@angular/http": "^7.2.15",
"@angular/platform-browser": "^8.0.0",
"@angular/platform-browser-dynamic": "^8.0.0",
"@angular/router": "^8.0.0",
"@ngx-translate/core": "^10.0.2",
"@ngx-translate/http-loader": "^3.0.1",
"core-js": "^2.5.4",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
"zone.js": "^0.9.1"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.2",
"@angular-devkit/build-ng-packagr": "~0.6.8",
"@angular-devkit/build-angular": "~0.6.3",
"ng-packagr": "^3.0.0-rc.2",
"tsickle": ">=0.25.5",
"tslib": "^1.7.1",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.3",
"@angular/language-service": "^6.0.2",
"@angular-devkit/build-angular": "^0.800.0",
"@angular-devkit/build-ng-packagr": "^0.800.0",
"@angular/cli": "~8.0.0",
"@angular/compiler-cli": "^8.0.0",
"@angular/language-service": "^8.0.0",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"codelyzer": "^5.0.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"ng-packagr": "^5.2.0",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
"tsickle": ">=0.25.5",
"tslib": "^1.7.1",
"tslint": "~5.9.1",
"typescript": "~3.4.5"
}
}
10 changes: 5 additions & 5 deletions projects/ngx-translate-router-http-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"name": "@gilsdav/ngx-translate-router-http-loader",
"version": "1.0.0",
"homepage": "https://github.com/gilsdav/ngx-translate-router#readme",
"license" : "MIT",
"license": "MIT",
"author": {
"name": "David Gilson",
"email": "[email protected]",
"url": "https://github.com/gilsdav"
},
"peerDependencies": {
"@gilsdav/ngx-translate-router": "^1.0.2",
"@angular/common": "^6.0.0-rc.0 || ^6.0.0",
"@angular/core": "^6.0.0-rc.0 || ^6.0.0",
"@angular/router": "^6.0.0-rc.0 || ^6.0.0",
"@angular/common": "^8.0.0",
"@angular/core": "^8.0.0",
"@angular/router": "^8.0.0",
"@ngx-translate/core": "^10.0.0"
}
}
}
10 changes: 5 additions & 5 deletions projects/ngx-translate-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
"name": "@gilsdav/ngx-translate-router",
"version": "1.0.2",
"homepage": "https://github.com/gilsdav/ngx-translate-router#readme",
"license" : "MIT",
"license": "MIT",
"author": {
"name": "David Gilson",
"email": "[email protected]",
"url": "https://github.com/gilsdav"
},
"peerDependencies": {
"@angular/common": "^6.0.0-rc.0 || ^6.0.0",
"@angular/core": "^6.0.0-rc.0 || ^6.0.0",
"@angular/router": "^6.0.0-rc.0 || ^6.0.0",
"@angular/common": "^8.0.0",
"@angular/core": "^8.0.0",
"@angular/router": "^8.0.0",
"@ngx-translate/core": "^10.0.0"
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
RAW_ROUTES,
USE_CACHED_LANG
} from './localize-router.config';
import { LocalizeRouterConfigLoader } from './localize-router-config-loader';
import { GilsdavReuseStrategy } from './gilsdav-reuse-strategy';

@Injectable()
Expand Down Expand Up @@ -79,7 +78,6 @@ export class LocalizeRouterModule {
},
LocalizeRouterService,
ParserInitializer,
{ provide: NgModuleFactoryLoader, useClass: LocalizeRouterConfigLoader },
{
provide: APP_INITIALIZER,
multi: true,
Expand Down
14 changes: 12 additions & 2 deletions projects/ngx-translate-router/src/lib/localize-router.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,20 @@ export abstract class LocalizeParser {
return this.settings.alwaysSetPrefix || this.currentLang !== this.defaultLang ? this.currentLang : '';
}

removeOldLangFromRoute(path: string) {
let local = this.locales.find(l => path.startsWith(`/${l}/`));
if (local) {
path = path.replace(`/${local}`, '');
}
return path;
}

/**
* Translate route and return observable
*/
translateRoute(path: string): string {
path = this.removeOldLangFromRoute(path);

const queryParts = path.split('?');
if (queryParts.length > 2) {
throw Error('There should be only one query parameter block in the URL');
Expand Down Expand Up @@ -357,7 +367,7 @@ export class ManualParserLoader extends LocalizeParser {
*/
load(routes: Routes): Promise<any> {
return new Promise((resolve: any) => {
this.init(routes).then(resolve);
this.init(routes).then(resolve).catch(err => console.log('Error', err));
});
}
}
Expand All @@ -368,4 +378,4 @@ export class DummyLocalizeParser extends LocalizeParser {
this.init(routes).then(resolve);
});
}
}
}
77 changes: 18 additions & 59 deletions projects/ngx-translate-router/src/lib/localize-router.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Inject } from '@angular/core';
// import { Location } from '@angular/common';
import { Router, NavigationStart, ActivatedRouteSnapshot, NavigationExtras, ActivatedRoute } from '@angular/router';
import { NavigationExtras, NavigationStart, RouteConfigLoadEnd, Router } from '@angular/router';
import { Subject } from 'rxjs';
import { filter, pairwise } from 'rxjs/operators';

import { LocalizeParser } from './localize-router.parser';
import { filter, pairwise, skip } from 'rxjs/operators';
import { LocalizeRouterSettings } from './localize-router.config';
import { LocalizeParser } from './localize-router.parser';


/**
* Localization service
Expand All @@ -18,13 +17,10 @@ export class LocalizeRouterService {
* CTOR
*/
constructor(
@Inject(LocalizeParser) public parser: LocalizeParser,
@Inject(LocalizeRouterSettings) public settings: LocalizeRouterSettings,
@Inject(Router) private router: Router,
@Inject(ActivatedRoute) private route: ActivatedRoute/*,
@Inject(Location) private location: Location*/
) {
this.routerEvents = new Subject<string>();
@Inject(LocalizeParser) public parser: LocalizeParser,
@Inject(LocalizeRouterSettings) public settings: LocalizeRouterSettings,
@Inject(Router) private router: Router) {
this.routerEvents = new Subject<string>();
}

/**
Expand All @@ -39,6 +35,13 @@ export class LocalizeRouterService {
pairwise()
)
.subscribe(this._routeChanged());
this.lazyModuleLoaded();
}

lazyModuleLoaded() {
this.router.events.pipe(filter(event => event instanceof RouteConfigLoadEnd), skip(1)).subscribe((e: RouteConfigLoadEnd) => {
this.parser.initChildRoutes([].concat(...[e.route]));
});
}

/**
Expand All @@ -49,11 +52,11 @@ export class LocalizeRouterService {
// console.log(this.route);
// }
if (lang !== this.parser.currentLang) {
const rootSnapshot: ActivatedRouteSnapshot = this.router.routerState.snapshot.root;
let url: string = this.router.url; //this.router.routerState.snapshot.root;

this.parser.translateRoutes(lang).subscribe(() => {

let url = this.traverseRouteSnapshot(rootSnapshot);
//let url = this.traverseRouteSnapshot(rootSnapshot);
url = this.translateRoute(url) as string;

if (!this.settings.alwaysSetPrefix) {
Expand Down Expand Up @@ -97,50 +100,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) {
return this.traverseRouteSnapshot(snapshot.firstChild);
} else {
return this.parseSegmentValue(snapshot);
}
/* if (snapshot.firstChild && snapshot.firstChild.routeConfig && snapshot.firstChild.routeConfig.path) {
if (snapshot.firstChild.routeConfig.path !== '**') {
return this.parseSegmentValue(snapshot) + '/' + this.traverseRouteSnapshot(snapshot.firstChild);
} else {
return this.parseSegmentValue(snapshot.firstChild);
}
}
return this.parseSegmentValue(snapshot); */
}

/**
* Extracts new segment value based on routeConfig and url
*/
private parseSegmentValue(snapshot: ActivatedRouteSnapshot): string {
if (snapshot.data.localizeRouter) {
const path = snapshot.data.localizeRouter.path;
const subPathSegments = path.split('/');
return subPathSegments.map((s: string, i: number) => s.indexOf(':') === 0 ? snapshot.url[i].path : s).join('/');
} else {
return '';
}
/* if (snapshot.routeConfig) {
if (snapshot.routeConfig.path === '**') {
return snapshot.url.filter((segment: UrlSegment) => segment.path).map((segment: UrlSegment) => segment.path).join('/');
} else {
const subPathSegments = snapshot.routeConfig.path.split('/');
return subPathSegments.map((s: string, i: number) => s.indexOf(':') === 0 ? snapshot.url[i].path : s).join('/');
}
}
return ''; */
}

/**
* Translate route to current language
* If new language is explicitly provided then replace language part in url with new language
Expand Down Expand Up @@ -183,4 +142,4 @@ export class LocalizeRouterService {
}
};
}
}
}
2 changes: 1 addition & 1 deletion projects/ngx-translate-router/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export * from './lib/localize-router.parser';
export * from './lib/localize-router.service';
export * from './lib/localize-router.pipe';
export * from './lib/localize-router.config';
export * from './lib/localize-router-config-loader';
export * from './lib/gilsdav-reuse-strategy';

10 changes: 5 additions & 5 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@
"check-type"
],
"no-output-on-prefix": true,
"use-input-property-decorator": true,
"use-output-property-decorator": true,
"use-host-property-decorator": true,
"no-inputs-metadata-property": true,
"no-outputs-metadata-property": true,
"no-host-metadata-property": true,
"no-input-rename": true,
"no-output-rename": true,
"use-life-cycle-interface": true,
"use-lifecycle-interface": true,
"use-pipe-transform-interface": true,
"component-class-suffix": true,
"directive-class-suffix": true
}
}
}

0 comments on commit 550139f

Please sign in to comment.