Skip to content

Commit

Permalink
Considered angular routing when calculating prefix used to load bundl…
Browse files Browse the repository at this point in the history
…e files (#13)
  • Loading branch information
joaquimvila authored and alfonsserra committed Jun 13, 2018
1 parent a41cb9e commit 3324cf4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
20 changes: 15 additions & 5 deletions src/app/systelab-translate/LocalizableTranslateStaticLoader.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { Observable } from 'rxjs/Rx';
import { TranslateLoader } from '@ngx-translate/core';
import { HttpClient } from '@angular/common/http';
import { Location } from '@angular/common';

export class LocalizableTranslateStaticLoader implements TranslateLoader {

protected prefix = '';

constructor(private http: HttpClient) {
constructor(private http: HttpClient,
private location: Location) {

if (!(window.location.pathname === '/' || window.location.pathname === '/context.html')) {
if (window.location.pathname.endsWith('index.html')) {
this.prefix = window.location.pathname;
if (this.prefix.endsWith('index.html')) {
// That's the case of Electron when starting from local file.
this.prefix = window.location.pathname.replace('index.html', '');
} else {
this.prefix = window.location.pathname + '/';
this.prefix = this.prefix.substr(0, this.prefix.length - 10);
}
if (this.prefix.endsWith('/')) {
this.prefix = this.prefix.substr(0, this.prefix.length - 1);
}
if (this.prefix.endsWith(this.location.path())) {
// When starting from an Angular application route
this.prefix = this.prefix.substr(0, this.prefix.length - this.location.path().length);
}

this.prefix = (this.prefix !== '') ? this.prefix + '/' : '';
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/app/systelab-translate/systelab-translate.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { HttpClient } from '@angular/common/http';
import { NumberFormatPipe } from './number-format.pipe';
import { DecimalPipe } from '@angular/common';
import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';

// AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient) {
return new LocalizableTranslateStaticLoader(http);
export function HttpLoaderFactory(http: HttpClient, location: Location) {
return new LocalizableTranslateStaticLoader(http, location);
}

@NgModule({
Expand All @@ -18,7 +19,7 @@ export function HttpLoaderFactory(http: HttpClient) {
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
deps: [HttpClient, Location]
}
})
],
Expand All @@ -30,7 +31,10 @@ export function HttpLoaderFactory(http: HttpClient) {
GeneralTranslatePipe,
NumberFormatPipe],
providers: [
DecimalPipe]
DecimalPipe,
Location,
{provide: LocationStrategy, useClass: PathLocationStrategy}
]
})
export class SystelabTranslateModule {
public static forRoot(entryComponents?: Array<Type<any> | any[]>): ModuleWithProviders {
Expand Down

0 comments on commit 3324cf4

Please sign in to comment.