Angular 2+动态翻译页面标题使用" TranslateService" &安培; "名称"

时间:2017-09-13 10:39:54

标签: angular ngx-translate

我正在尝试在我的应用中实现多语言功能,并且我正在使用ngx-translate。现在我的应用程序可以将语言从英语切换到俄语,并且它的工作正常,但我希望我的应用程序具有动态page title。我使用Title服务并且工作正常,但是当我切换语言时,title仍然是默认语言。

  

我的APP中如何动态翻译标题

为语法错误道歉

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';

    // i18n
    import {HttpClientModule, HttpClient} from '@angular/common/http';
    import {TranslateModule, TranslateLoader} from '@ngx-translate/core';
    import {TranslateHttpLoader} from '@ngx-translate/http-loader';
    import { AppRoutingModule } from './app-routing.module';   
    import { AppComponent } from './app.component';

    // i18n function
    export function HttpLoaderFactory(http: HttpClient) {
      return new TranslateHttpLoader(http);
    }

    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        HttpClientModule,
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useFactory: HttpLoaderFactory,
            deps: [HttpClient]
          }
        }),
        AppRoutingModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

app.companent.ts

import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { Title } from '@angular/platform-browser';
import { TranslateService } from '@ngx-translate/core';

import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  constructor (
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private titleService: Title,
    private translateService: TranslateService
  ) { }

  ngOnInit() {
    this.defaultLanguage();
    this.routerTitle();
  }

  routerTitle() {
    this.router.events
      .filter((event) => event instanceof NavigationEnd)
      .map(() => this.activatedRoute)
      .map((route) => {
        while (route.firstChild) route = route.firstChild;
        return route;
      })
      .filter((route) => route.outlet === 'primary')
      .mergeMap((route) => route.data)
      .subscribe((event) => this.titleService.setTitle(this.translateService.instant(event['title'])));
  }

  defaultLanguage() {
    this.translateService.addLangs(['tj', 'ru', 'en']);
    let lang = localStorage.getItem('language');
    if(lang !== null) {
      this.translateService.use(lang);
    }
    else {
      this.translateService.setDefaultLang('tj');
    }
  }

  switchLanguage(language: string) {
    this.translateService.use(language);
    localStorage.setItem('language', language);
  }

}

1 个答案:

答案 0 :(得分:0)

将instant()方法更改为stream():

this.titleService.setTitle(this.translateService.stream(event['title'])));