Skip to content

Commit 8fdb355

Browse files
committed
feat: Extract StaticParserLoader and remove Http. Fixes (#64)
1 parent 4cd3fb6 commit 8fdb355

File tree

7 files changed

+61
-134
lines changed

7 files changed

+61
-134
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2016 Greentube
1+
Copyright (c) 2017 Greentube
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

MIGRATION_GUIDE.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ This guide is provided to make the transition as painless as possible.
55

66
Steps to migrate your code are:
77
- update the npm packages
8+
- install new npm packages
89
- update the module import
910

1011
Here is a detailed list of the changes that you need to make:
1112

1213
1. Update in your package.json `localize-router` to the latest 1.x version ([check the current release here](https://github.com/Greentube/localize-router/releases)).
1314

14-
2. Run `npm update` to update your packages.
15+
2. If you were using `StaticParserLoader` you need to install [LocalizeRouterHttpLoader](https://github.com/Greentube/localize-router-http-loader):
16+
17+
```ts
18+
npm install --save localize-router-http-loader
19+
```
20+
21+
3. Run `npm update` to update your existing packages.
1522

1623
In some cases your local `node_modules` might get in broken state in which case you can try running `npm cache clean` or in worst cases deleting entire node_modules folder and running `npm install` again.
1724

18-
3. The module initialization `forRoot` method has changed a bit. Instead of loader, it expects an object of parameters.
25+
4. The module initialization `forRoot` method has changed a bit. Instead of loader, it expects an object of parameters.
1926

2027
```ts
2128
import { Http } from '@angular/http';
@@ -37,26 +44,27 @@ In some cases your local `node_modules` might get in broken state in which case
3744
import { Http } from '@angular/http';
3845
import { TranslateService } from '@ngx-translate/core';
3946
import { Location } from '@angular/common';
40-
import { LocalizeRouterModule, LocalizeParser, StaticParserLoader, LocalizeRouterSettings } from 'localize-router';
47+
import { LocalizeRouterModule, LocalizeParser, LocalizeRouterSettings } from 'localize-router';
48+
import { LocalizeRouterHttpLoader } from 'localize-router-http-loader';
4149
4250
LocalizeRouterModule.forRoot(routes, {
4351
parser: {
4452
provide: LocalizeParser,
4553
useFactory: (translate, location, settings, http) =>
46-
new StaticParserLoader(translate, location, settings, http, 'your/path/to/config.json'),
54+
new LocalizeRouterHttpLoader(translate, location, settings, http, 'your/path/to/config.json'),
4755
deps: [TranslateService, Location, LocalizeRouterSettings, Http]
4856
}
4957
})
5058
```
5159

52-
You can now also provide additional settings regarding url prefixes and default language cache mechanisms:
60+
5. You can now also provide additional settings to additionally configure the way `LocalizeRouter` loads and parses the routes:
5361

5462
```ts
5563
LocalizeRouterModule.forRoot(routes, {
5664
parser: {
5765
provide: LocalizeParser,
5866
useFactory: (translate, location, settings, http) =>
59-
new StaticParserLoader(translate, location, settings, http, 'your/path/to/config.json'),
67+
new LocalizeRouterHttpLoader(translate, location, settings, http, 'your/path/to/config.json'),
6068
deps: [TranslateService, Location, LocalizeRouterSettings, Http]
6169
},
6270
useCachedLang: { provide: USE_CACHED_LANG, useValue: booleanValue },

README.md

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ Demo project can be found [here](https://github.com/meeroslav/localize-router-ex
1212
- [Installation](#installation)
1313
- [Usage](#usage)
1414
- [Initialize module](#initialize-module)
15-
- [Static initialization](#static-initialization)
16-
- [JSON config file](#json-config-file)
15+
- [Http loader](#http-loader)
1716
- [Initialization config](#initialization-config)
1817
- [Manual initialization](#manual-initialization)
1918
- [Server side initialization](#server-side-initialization)
@@ -46,43 +45,43 @@ In order to use `localize-router` you must initialize it with following informat
4645
### Initialize module
4746
Module can be initialized either using static file or manually by passing necessary values.
4847

49-
#### Static initialization
48+
#### Http loader
49+
50+
In order to use Http loader for config files, you must include `localize-router-http-loader` package and use its `LocalizeRouterHttpLoader`.
51+
5052
```ts
5153
import {BrowserModule} from "@angular/platform-browser";
5254
import {NgModule} from '@angular/core';
5355
import {HttpModule} from '@angular/http';
5456
import {TranslateModule} from '@ngx-translate/core';
55-
import {LocalizeRouterModule} from 'localize-router/localize-router';
57+
import {LocalizeRouterModule} from 'localize-router';
58+
import {LocalizeRouterHttpLoader} from 'localize-router-http-loader';
5659
import {RouterModule} from '@angular/router';
5760

5861
import {routes} from './app.routes';
5962

6063
@NgModule({
61-
imports: [
62-
BrowserModule,
63-
HttpModule,
64-
TranslateModule.forRoot(),
65-
LocalizeRouterModule.forRoot(routes),
66-
RouterModule.forRoot(routes)
67-
],
68-
bootstrap: [AppComponent]
69-
})
70-
export class AppModule { }
71-
```
72-
73-
Static file's default path is `assets/locales.json`. You can override the path by calling `StaticParserLoader` on you own:
74-
```ts
75-
LocalizeRouterModule.forRoot(routes, {
76-
parser: {
64+
imports: [
65+
BrowserModule,
66+
HttpModule,
67+
TranslateModule.forRoot(),
68+
LocalizeRouterModule.forRoot(routes, {
69+
parser: {
7770
provide: LocalizeParser,
7871
useFactory: (translate, location, settings, http) =>
79-
new StaticParserLoader(translate, location, settings, http, 'your/path/to/config.json'),
72+
new LocalizeRouterHttpLoader(translate, location, settings, http),
8073
deps: [TranslateService, Location, LocalizeRouterSettings, Http]
81-
}
74+
}
75+
}),
76+
RouterModule.forRoot(routes)
77+
],
78+
bootstrap: [AppComponent]
8279
})
83-
80+
export class AppModule { }
8481
```
8582

83+
More details are available on [localize-router-http-loader](https://github.com/Greentube/localize-router-http-loader).
84+
8685
If you are using child modules or routes you need to initialize them with `forChild` command:
8786
```ts
8887
@NgModule({
@@ -96,41 +95,22 @@ If you are using child modules or routes you need to initialize them with `forCh
9695
export class ChildModule { }
9796
```
9897

99-
#### JSON config file
100-
JSON config file has following structure:
101-
```
102-
{
103-
"locales": ["en", "de", ...],
104-
"prefix": "MY_PREFIX"
105-
}
106-
```
107-
108-
```ts
109-
interface ILocalizeRouterParserConfig {
110-
locales: Array<string>;
111-
prefix?: string;
112-
}
113-
```
114-
115-
Prefix field is not mandatory and default value is empty string.
116-
11798
#### Initialization config
11899
Apart from providing routes which are mandatory, and parser loader you can provide additional configuration for more granular setting of `localize router`. More information at [LocalizeRouterConfig](#localizerouterconfig).
119100

120101

121102
#### Manual initialization
122-
With manual initialization you need to provide information directly:
123-
```ts
124-
LocalizeRouterModule.forRoot(routes, {
125-
parser: {
126-
provide: LocalizeParser,
127-
useFactory: (translate, location, settigns) =>
128-
new ManualParserLoader(translate, location, settings, ['en','de',...], 'YOUR_PREFIX'),
129-
deps: [TranslateService, Location, LocalizeRouterSettings]
130-
}
131-
})
132-
133-
```
103+
With manual initialization you need to provide information directly:
104+
```ts
105+
LocalizeRouterModule.forRoot(routes, {
106+
parser: {
107+
provide: LocalizeParser,
108+
useFactory: (translate, location, settigns) =>
109+
new ManualParserLoader(translate, location, settings, ['en','de',...], 'YOUR_PREFIX'),
110+
deps: [TranslateService, Location, LocalizeRouterSettings]
111+
}
112+
})
113+
```
134114

135115
#### Server side initialization
136116
In order to use server side initialization in isomorphic/universal projects you need to create loader similar to this:
@@ -153,7 +133,6 @@ export class LocalizeUniversalLoader extends LocalizeParser {
153133
export function localizeLoaderFactory(translate: TranslateService, location: Location, settings: LocalizeRouterSettings) {
154134
return new LocalizeUniversalLoader(translate, location, settings);
155135
}
156-
157136
```
158137

159138
Don't forget to create similar loader for `ngx-translate` as well:
@@ -217,7 +196,7 @@ Make sure you therefore place most common language (e.g. 'en') as a first string
217196

218197
Sometimes you might have a need to have certain routes excluded from the localization process e.g. login page, registration page etc. This is possible by setting flag `skipRouteLocalization` on route's data object.
219198

220-
```typescript
199+
```ts
221200
let routes = [
222201
// this route gets localized
223202
{ path: 'home', component: HomeComponent },

demo/cli/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"@ngx-translate/core": "^7.0.0",
2424
"@ngx-translate/http-loader": "^0.1.0",
2525
"core-js": "^2.4.1",
26-
"localize-router": "1.0.0-alpha.1",
27-
"localize-router-http-loader": "0.0.1",
26+
"localize-router": "1.0.0-alpha.3",
27+
"localize-router-http-loader": "0.0.3",
2828
"rxjs": "^5.1.0",
2929
"zone.js": "^0.8.4"
3030
},

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "localize-router",
3-
"version": "1.0.0-alpha.2",
3+
"version": "1.0.0-alpha.3",
44
"description": "An implementation of routes localization for Angular 2",
55
"scripts": {
66
"test": "karma start",
@@ -36,7 +36,7 @@
3636
"@angular/http": ">=2.0.0",
3737
"@angular/router": ">=3.0.0",
3838
"rxjs": "^5.0.0",
39-
"@ngx-translate/core": "^6.0.0"
39+
"@ngx-translate/core": ">=6.0.0"
4040
},
4141
"devDependencies": {
4242
"@angular/animations": "~4.3.3",

src/localize-router.module.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,19 @@ import {
22
NgModule, ModuleWithProviders, APP_INITIALIZER, Optional, SkipSelf,
33
Injectable, Injector
44
} from '@angular/core';
5-
import { HttpModule, Http } from '@angular/http';
65
import { LocalizeRouterService } from './localize-router.service';
7-
import { LocalizeParser, StaticParserLoader } from './localize-router.parser';
6+
import { DummyLocalizeParser, LocalizeParser } from './localize-router.parser';
87
import { RouterModule, Routes } from '@angular/router';
98
import { LocalizeRouterPipe } from './localize-router.pipe';
10-
import { TranslateModule, TranslateService } from '@ngx-translate/core';
11-
import { Location, CommonModule } from '@angular/common';
9+
import { TranslateModule } from '@ngx-translate/core';
10+
import { CommonModule } from '@angular/common';
1211
import {
1312
ALWAYS_SET_PREFIX,
1413
CACHE_MECHANISM, CACHE_NAME, DEFAULT_LANG_FUNCTION, LOCALIZE_ROUTER_FORROOT_GUARD, LocalizeRouterConfig, LocalizeRouterSettings,
1514
RAW_ROUTES,
1615
USE_CACHED_LANG
1716
} from './localize-router.config';
1817

19-
/**
20-
* Helper function for loading external parser
21-
* @param translate
22-
* @param location
23-
* @param settings
24-
* @param http
25-
* @returns {StaticParserLoader}
26-
*/
27-
export function localizeLoaderFactory(translate: TranslateService, location: Location, settings: LocalizeRouterSettings, http: Http) {
28-
return new StaticParserLoader(translate, location, settings, http);
29-
}
30-
3118
@Injectable()
3219
export class ParserInitializer {
3320
parser: LocalizeParser;
@@ -76,7 +63,7 @@ export function getAppInitializer(p: ParserInitializer, parser: LocalizeParser,
7663
}
7764

7865
@NgModule({
79-
imports: [HttpModule, CommonModule, RouterModule, TranslateModule],
66+
imports: [CommonModule, RouterModule, TranslateModule],
8067
declarations: [LocalizeRouterPipe],
8168
exports: [LocalizeRouterPipe]
8269
})
@@ -97,11 +84,7 @@ export class LocalizeRouterModule {
9784
{ provide: CACHE_MECHANISM, useValue: config.cacheMechanism },
9885
{ provide: DEFAULT_LANG_FUNCTION, useValue: config.defaultLangFunction },
9986
LocalizeRouterSettings,
100-
config.parser || {
101-
provide: LocalizeParser,
102-
useFactory: localizeLoaderFactory,
103-
deps: [TranslateService, Location, LocalizeRouterSettings, Http]
104-
},
87+
config.parser || { provide: LocalizeParser, useClass: DummyLocalizeParser },
10588
{
10689
provide: RAW_ROUTES,
10790
multi: true,

src/localize-router.parser.ts

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Injectable } from '@angular/core';
2-
import { Http, Response } from '@angular/http';
32
import { Routes, Route } from '@angular/router';
43
import { TranslateService } from '@ngx-translate/core';
54
import { Observable } from 'rxjs/Observable';
@@ -12,14 +11,6 @@ import { CacheMechanism, LocalizeRouterSettings } from './localize-router.config
1211

1312
const COOKIE_EXPIRY = 30; // 1 month
1413

15-
/**
16-
* Config interface
17-
*/
18-
export interface ILocalizeRouterParserConfig {
19-
locales: Array<string>;
20-
prefix?: string;
21-
}
22-
2314
/**
2415
* Abstract class for parsing localization
2516
*/
@@ -64,7 +55,7 @@ export abstract class LocalizeParser {
6455

6556
this.routes = routes;
6657

67-
if (!this.locales.length) {
58+
if (!this.locales || !this.locales.length) {
6859
return Promise.resolve();
6960
}
7061
/** detect current language */
@@ -396,44 +387,10 @@ export class ManualParserLoader extends LocalizeParser {
396387
}
397388
}
398389

399-
/**
400-
* Load configuration from server
401-
*/
402-
export class StaticParserLoader extends LocalizeParser {
403-
private _dataLoaded: boolean;
404-
405-
/**
406-
* CTOR
407-
* @param translate
408-
* @param location
409-
* @param settings
410-
* @param http
411-
* @param path
412-
*/
413-
constructor(translate: TranslateService, location: Location, settings: LocalizeRouterSettings, private http: Http, private path: string = 'assets/locales.json') {
414-
super(translate, location, settings);
415-
this._dataLoaded = false;
416-
}
417-
418-
/**
419-
* Initialize or append routes
420-
* @param routes
421-
* @returns {Promise<any>}
422-
*/
390+
export class DummyLocalizeParser extends LocalizeParser {
423391
load(routes: Routes): Promise<any> {
424392
return new Promise((resolve: any) => {
425-
if (this._dataLoaded) {
426-
this.init(routes).then(resolve);
427-
} else {
428-
this.http.get(`${this.path}`)
429-
.map((res: Response) => res.json())
430-
.subscribe((data: ILocalizeRouterParserConfig) => {
431-
this._dataLoaded = true;
432-
this.locales = data.locales;
433-
this.prefix = data.prefix || '';
434-
this.init(routes).then(resolve);
435-
});
436-
}
393+
this.init(routes).then(resolve);
437394
});
438395
}
439396
}

0 commit comments

Comments
 (0)