Skip to content

Commit 4cd3fb6

Browse files
committed
feat(LocalizeRouterParser): Apply alwaysSetPrefix to multi langs Fix #63
Don't prefix language for default language is `alwaysSetPrefix` is false
1 parent e9a5afa commit 4cd3fb6

12 files changed

+129
-91
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ coverage/
1313
# overrides for js
1414
!config/*
1515
!demo/cli/*js
16-
!copy.js
1716
!karma.conf.js
1817
!webpack.config.js
1918

.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ coverage
1818
config
1919
karma.conf.js
2020
webpack.config.js
21-
copy.js
2221
tsconfig.json
2322
tslint.json
2423
!*.metadata.json

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ before_install:
1212
- npm i -g npm@^3
1313
before_script:
1414
- npm prune
15-
after_success:
16-
- npm run semantic-release
1715
branches:
1816
except:
1917
- "/^v\\d+\\.\\d+\\.\\d+$/"
2018
before_script:
2119
- export DISPLAY=:99.0
22-
- sh -e /etc/init.d/xvfb start
20+
- sh -e /etc/init.d/xvfb start

MIGRATION_GUIDE.md

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,53 @@ In some cases your local `node_modules` might get in broken state in which case
1717

1818
3. The module initialization `forRoot` method has changed a bit. Instead of loader, it expects an object of parameters.
1919

20-
```typescript
20+
```ts
21+
import { Http } from '@angular/http';
22+
import { TranslateService } from '@ngx-translate/core';
23+
import { Location } from '@angular/common';
24+
import { LocalizeRouterModule, LocalizeParser, StaticParserLoader } from 'localize-router';
25+
2126
LocalizeRouterModule.forRoot(routes, {
2227
provide: LocalizeParser,
2328
useFactory: (translate, location, http) =>
24-
new StaticParserLoader(translate, location, http, 'your/path/to/config.json'),
29+
new StaticParserLoader(translate, location, http, 'your/path/to/config.json'),
2530
deps: [TranslateService, Location, Http]
2631
})
2732
```
2833

2934
Is now:
3035

31-
```typescript
36+
```ts
37+
import { Http } from '@angular/http';
38+
import { TranslateService } from '@ngx-translate/core';
39+
import { Location } from '@angular/common';
40+
import { LocalizeRouterModule, LocalizeParser, StaticParserLoader, LocalizeRouterSettings } from 'localize-router';
41+
3242
LocalizeRouterModule.forRoot(routes, {
3343
parser: {
34-
provide: LocalizeParser,
35-
useFactory: (translate, location, settings, http) =>
36-
new StaticParserLoader(translate, location, settings, http, 'your/path/to/config.json'),
37-
deps: [TranslateService, Location, LocalizeRouterSettings, Http]
44+
provide: LocalizeParser,
45+
useFactory: (translate, location, settings, http) =>
46+
new StaticParserLoader(translate, location, settings, http, 'your/path/to/config.json'),
47+
deps: [TranslateService, Location, LocalizeRouterSettings, Http]
3848
}
3949
})
4050
```
4151

4252
You can now also provide additional settings regarding url prefixes and default language cache mechanisms:
4353

44-
```typescript
45-
LocalizeRouterModule.forRoot({
46-
useCachedLang: { provide: USE_CACHED_LANG, useValue: booleanValue },
47-
alwaysSetPrefix: { provide: ALWAYS_SET_PREFIX, useValue: booleanValue },
48-
cacheName: { provide: CACHE_NAME, useValue: stringValue },
49-
cacheMechanism: { provide: CACHE_MECHANISM, useValue: typeOfCacheMechanism },
50-
defaultLangFunction: { provide: DEFAULT_LANG_FUNCTION, useValue: typeOfDefaultLanguageFunction },
54+
```ts
55+
LocalizeRouterModule.forRoot(routes, {
56+
parser: {
57+
provide: LocalizeParser,
58+
useFactory: (translate, location, settings, http) =>
59+
new StaticParserLoader(translate, location, settings, http, 'your/path/to/config.json'),
60+
deps: [TranslateService, Location, LocalizeRouterSettings, Http]
61+
},
62+
useCachedLang: { provide: USE_CACHED_LANG, useValue: booleanValue },
63+
alwaysSetPrefix: { provide: ALWAYS_SET_PREFIX, useValue: booleanValue },
64+
cacheName: { provide: CACHE_NAME, useValue: stringValue },
65+
cacheMechanism: { provide: CACHE_MECHANISM, useValue: typeOfCacheMechanism },
66+
defaultLangFunction: { provide: DEFAULT_LANG_FUNCTION, useValue: typeOfDefaultLanguageFunction },
5167
})
5268
```
5369

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ export function localizeLoaderFactory(translate: TranslateService, location: Loc
292292
#### Properties
293293
- `parser`: Provider for loading of LocalizeParser. Default value is `StaticParserLoader`.
294294
- `useCachedLang`: boolean. Flag whether default language should be cached. Default value is `true`.
295-
- `alwaysSetPrefix`: boolean. Flag whether language should always prefix the url if only one language is present. Default value is `true`.
295+
- `alwaysSetPrefix`: boolean. Flag whether language should always prefix the url. Default value is `true`.
296+
When value is `false`, prefix will not be used for for default language (this includes the situation when there is only one language).
297+
- `prefixDefaultLanguage`: boolean. Flag whether default language should be prefixed or not. In order to maintain constant default language, specifying`defaultLangFunction` is recommended. Default value is `true`.
296298
- `cacheMechanism`: CacheMechanism.LocalStorage || CacheMechanism.Cookie. Default value is `CacheMechanism.LocalStorage`.
297299
- `cacheName`: string. Name of cookie/local store. Default value is `LOCALIZE_DEFAULT_LANGUAGE`.
298300
- `defaultLangFunction`: (languages: string[], cachedLang?: string, browserLang?: string) => string. Override method for custom logic for picking default language, when no language is provided via url. Default value is `undefined`.
@@ -325,6 +327,7 @@ yoursite.com/en/users/John%20Doe/profile -> yoursite.com/de/benutzer/John%20Doe/
325327
- `locales`: Array of used language codes
326328
- `currentLang`: Currently selected language
327329
- `routes`: Active translated routes
330+
- `urlPrefix`: Language prefix for current language. Empty string if `alwaysSetPrefix=false` and `currentLang` is same as default language.
328331

329332
#### Methods:
330333
- `translateRoutes(language: string): Observable<any>`: Translates all the routes and sets language and current

copy.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

demo/cli/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +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": "^0.7.1",
26+
"localize-router": "1.0.0-alpha.1",
27+
"localize-router-http-loader": "0.0.1",
2728
"rxjs": "^5.1.0",
2829
"zone.js": "^0.8.4"
2930
},

demo/cli/src/app/app-routing.module.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { NgModule } from '@angular/core';
22
import { RouterModule } from '@angular/router';
3-
import { LocalizeRouterModule } from 'localize-router';
3+
import { LocalizeRouterModule, LocalizeRouterSettings, LocalizeParser } from 'localize-router';
4+
import { LocalizeRouterHttpLoader } from 'localize-router-http-loader';
5+
import { Http } from '@angular/http';
6+
import { TranslateService } from '@ngx-translate/core';
7+
import { Location } from '@angular/common';
8+
9+
export function HttpLoaderFactory(translate: TranslateService, location: Location, settings: LocalizeRouterSettings, http: Http) {
10+
return new LocalizeRouterHttpLoader(translate, location, settings, http);
11+
}
412

513
const routes = [
614
{ path: 'lazy', loadChildren: './+lazy/lazy.module#LazyModule' }
@@ -10,7 +18,11 @@ const routes = [
1018
imports: [
1119
RouterModule.forRoot(routes),
1220
LocalizeRouterModule.forRoot(routes, {
13-
useCachedLang: false
21+
parser: {
22+
provide: LocalizeParser,
23+
useFactory: HttpLoaderFactory,
24+
deps: [TranslateService, Location, LocalizeRouterSettings, Http]
25+
}
1426
})
1527
],
1628
exports: [ RouterModule, LocalizeRouterModule ]

demo/cli/tslint.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
140
2626
],
2727
"member-access": false,
28-
"member-ordering": [
29-
true,
30-
"static-before-instance",
31-
"variables-before-functions"
32-
],
3328
"no-arg": true,
3429
"no-bitwise": true,
3530
"no-console": [
@@ -69,9 +64,7 @@
6964
"single"
7065
],
7166
"radix": true,
72-
"semicolon": [
73-
"always"
74-
],
67+
"semicolon": [ true, "always" ],
7568
"triple-equals": [
7669
true,
7770
"allow-null-check"

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "localize-router",
3-
"version": "1.0.0-alpha.1",
3+
"version": "1.0.0-alpha.2",
44
"description": "An implementation of routes localization for Angular 2",
55
"scripts": {
66
"test": "karma start",
77
"test-watch": "karma start --singleRun=false --autoWatch=true",
88
"commit": "npm run prepublish && npm test",
99
"prepublish": "ngc && npm run build",
10-
"build": "webpack && node copy.js"
10+
"build": "webpack"
1111
},
1212
"repository": {
1313
"type": "git",

0 commit comments

Comments
 (0)