Skip to content

Commit d0bd9f8

Browse files
committed
Fix: Preserve query parameters (#62)
1 parent 2e20bdf commit d0bd9f8

File tree

6 files changed

+43
-29
lines changed

6 files changed

+43
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Apart from providing routes which are mandatory, and parser loader you can provi
105105
LocalizeRouterModule.forRoot(routes, {
106106
parser: {
107107
provide: LocalizeParser,
108-
useFactory: (translate, location, settigns) =>
108+
useFactory: (translate, location, settings) =>
109109
new ManualParserLoader(translate, location, settings, ['en','de',...], 'YOUR_PREFIX'),
110110
deps: [TranslateService, Location, LocalizeRouterSettings]
111111
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "localize-router",
3-
"version": "1.0.0-alpha.3",
3+
"version": "1.0.0-rc.0",
44
"description": "An implementation of routes localization for Angular 2",
55
"scripts": {
66
"test": "karma start",

src/localize-router.parser.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,17 @@ export abstract class LocalizeParser {
213213
* @returns {string}
214214
*/
215215
translateRoute(path: string): string {
216-
let pathSegments = path.split('/');
216+
let queryParts = path.split('?');
217+
if (queryParts.length > 2) {
218+
throw 'There should be only one query parameter block in the URL';
219+
}
220+
let pathSegments = queryParts[0].split('/');
217221

218222
/** collect observables */
219-
return pathSegments.map((part: string) => part.length ? this.translateText(part) : part).join('/');
223+
return pathSegments
224+
.map((part: string) => part.length ? this.translateText(part) : part)
225+
.join('/') +
226+
(queryParts.length > 1 ? `?${queryParts[1]}` : '');
220227
}
221228

222229
/**

src/localize-router.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class LocalizeRouterService {
8686
let result: any[] = [];
8787
(path as Array<any>).forEach((segment: any, index: number) => {
8888
if (typeof segment === 'string') {
89-
let res = this.parser.translateRoute(segment);
89+
const res = this.parser.translateRoute(segment);
9090
if (!index && !segment.indexOf('/')) {
9191
result.push(`/${this.parser.urlPrefix}${res}`);
9292
} else {

tests/localize-router.parser.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,4 +490,30 @@ describe('LocalizeParser', () => {
490490
expect(routes[2]).toEqual({ path: 'about', data: { skipRouteLocalization: true } });
491491
}));
492492

493+
it('should translate route', fakeAsync(() => {
494+
loader = new ManualParserLoader(translate, location, settings, locales, prefix);
495+
spyOn(loader, 'translateRoutes').and.callThrough();
496+
localStorage.setItem('LOCALIZE_DEFAULT_LANGUAGE', 'en');
497+
498+
routes = [{ path: 'home', component: DummyComponent }];
499+
loader.load(routes);
500+
tick();
501+
502+
expect(routes[0].children[0].path).toEqual('home_en');
503+
expect(loader.translateRoute('home/whatever')).toEqual('home_en/whatever');
504+
}));
505+
it('should keep query params while translate route', fakeAsync(() => {
506+
loader = new ManualParserLoader(translate, location, settings, locales, prefix);
507+
spyOn(loader, 'translateRoutes').and.callThrough();
508+
localStorage.setItem('LOCALIZE_DEFAULT_LANGUAGE', 'en');
509+
510+
routes = [{ path: 'home', component: DummyComponent }];
511+
loader.load(routes);
512+
tick();
513+
514+
expect(routes[0].children[0].path).toEqual('home_en');
515+
expect(loader.translateRoute('home/whatever?test=123')).toEqual('home_en/whatever?test=123');
516+
expect(loader.translateRoute('home?test=123')).toEqual('home_en?test=123');
517+
expect(() => loader.translateRoute('home?test=123?test=123')).toThrow();
518+
}));
493519
});

tests/localize-router.service.spec.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { Injector } from '@angular/core';
2-
import {
3-
BaseRequestOptions, ConnectionBackend, Http, HttpModule, RequestOptions, XHRBackend
4-
} from '@angular/http';
52
import { LocalizeRouterService } from '../src/localize-router.service';
63
import { LocalizeParser } from '../src/localize-router.parser';
74
import { LocalizeRouterModule } from '../src/localize-router.module';
@@ -11,7 +8,6 @@ import { Observable } from 'rxjs/Observable';
118
import { Subject } from 'rxjs/Subject';
129
import { TranslateService } from '@ngx-translate/core';
1310
import { CommonModule, Location } from '@angular/common';
14-
import { MockBackend, MockConnection } from '@angular/http/testing';
1511

1612
class FakeTranslateService {
1713
defLang: string;
@@ -25,11 +21,8 @@ class FakeTranslateService {
2521
};
2622

2723
setDefaultLang = (lang: string) => { this.defLang = lang; };
28-
2924
use = (lang: string) => { this.currentLang = lang; };
30-
3125
get = (input: string) => Observable.of(this.content[input] || input);
32-
3326
getBrowserLang = () => this.browserLang;
3427
}
3528

@@ -60,37 +53,28 @@ describe('LocalizeRouterService', () => {
6053
let localizeRouterService: LocalizeRouterService;
6154
let routes: Routes;
6255

63-
let backend: any;
64-
let connection: MockConnection;
65-
6656
beforeEach(() => {
67-
routes = [{ path: '', component: DummyComponent }];
57+
routes = [
58+
{ path: 'home', component: DummyComponent },
59+
{ path: 'home/about', component: DummyComponent }
60+
];
6861

6962
TestBed.configureTestingModule({
70-
imports: [HttpModule, CommonModule, LocalizeRouterModule.forRoot(routes)],
63+
imports: [CommonModule, LocalizeRouterModule.forRoot(routes)],
7164
providers: [
7265
{ provide: Router, useClass: FakeRouter },
7366
{ provide: TranslateService, useClass: FakeTranslateService },
7467
{ provide: Location, useClass: FakeLocation },
75-
{ provide: XHRBackend, useClass: MockBackend },
76-
{ provide: ConnectionBackend, useClass: MockBackend },
77-
{ provide: RequestOptions, useClass: BaseRequestOptions },
78-
Http
7968
]
8069
});
8170
injector = getTestBed();
8271
parser = injector.get(LocalizeParser);
8372
router = injector.get(Router);
84-
85-
backend = injector.get(XHRBackend);
86-
backend.connections.subscribe((c: MockConnection) => connection = c);
8773
});
8874

8975
afterEach(() => {
9076
injector = void 0;
9177
localizeRouterService = void 0;
92-
backend = void 0;
93-
connection = void 0;
9478
});
9579

9680
it('is defined', () => {
@@ -102,9 +86,6 @@ describe('LocalizeRouterService', () => {
10286

10387
it('should initialize routerEvents', () => {
10488
localizeRouterService = new LocalizeRouterService(parser, router);
105-
106-
// mockBackendResponse(connection, '{"TEST": "This is a test", "TEST2": "This is another test"}');
107-
10889
expect(localizeRouterService.routerEvents).toBeDefined();
10990
});
11091

0 commit comments

Comments
 (0)