I'm working on an Angular2 app, wich use ngx-translate for text translation and localize-router in order to append the language to the route url.
Now, without using localize-router, everithing is working fine and I can change language (via dropdown button) and see text translations applied.
After installing localize-router, if I load the home page, I can see that the language is correctly appended to the url. But the problem is that when I change the language, the component (localize-router) redirect the user to the homepage (with the new language name append to the url) instead of remaining to the current page.
So at website loading the language is correctly appended, if I try to navigate, the url are correctly translated, but when I'm on a page different from the home and I try to change the language, I'm redirected to the home page with the new language appended.
Here there are my files and configs:
app.module.ts
@NgModule({
imports: [
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [Http]
}
}),
RouterModule.forRoot(routes,{useHash: true}),
LocalizeRouterModule.forRoot(routes),
app.routes.ts
const appRoutes: Routes = [
{
path: '',
component: DefaultLayoutComponent,
children: [
{
path: '',
component: HomeComponent,
pathMatch: 'full'
},
{
path: 'error',
component: ErrorComponent
},
{
path: 'dashboard',
children: [
{
path: 'home',
component: DashboardComponent,
canActivate: [AuthGuard]
},
app.component.ts
@Component({
selector: 'my-app',
template: '<router-outlet></router-outlet>',
moduleId: module.id,
})
export class AppComponent implements OnInit {
constructor(
private translate: TranslateService,
public router: Router,
) {
this.translate.addLangs(['ita', 'eng']);
this.translate.setDefaultLang('ita');
this.translate.use('ita');
DefaulLayoutComponent.html
... my html common section ...
<router-outlet></router-outlet>
... the remaining common html section ...
topbar.component.ts It handles the menu bar, when i click on dropdown the following function (inside topbar component) is called:
changeLanguage(lang: string){
this.translate.use(lang);
this.localizeService.changeLanguage(lang);
topbar.component.html (I just wrote the button template)
<button (click)="changeLanguage('ita')">ITA</button>
<button (click)="changeLanguage('eng')">ENG</button>
Folder Structure
- app
- app.module.ts
- app.component.ts
- other "main" stuff
- components
- defaultLayout
- defaultLayoutComponent.ts
- defaultLayoutComponent.html
- other components
The version of software used are:
"@angular/common": "~2.4.1",
"@angular/compiler": "~2.4.1",
"@angular/compiler-cli": "~2.4.1",
"@angular/core": "~2.4.1",
"@angular/forms": "~2.4.1",
"@angular/http": "~2.4.1",
"@angular/platform-browser": "~2.4.1",
"@angular/platform-browser-dynamic": "~2.4.1",
"@angular/platform-server": "~2.4.1",
"@angular/router": "~3.2.3",
"@angular/upgrade": "~2.4.1",
"@ngx-translate/core": "^6.0.1",
"@ngx-translate/http-loader": "^0.1.0",
"localize-router": "^0.7.1",
I cannot upgrade from angular2 to angular4 or greather.
So what I'm doing wrong?
Why when i'm for example in this page:
and I change the language I'm redirected to
I'm guessing if the problem could be in my route configuration, if I print a toString of the ActivatedRouteSnapshot object (independently from the current page) I always get
Route(url:'', path:'')