2017-10-21 46 views
1

我在使用ngx-translation時遇到問題。它在兩種模式下工作 - 瀏覽器&服務器。 在瀏覽器模式下一切正常,但在服務器模式下,翻譯不能滿足HTML源文件。ngx-translate在服務器模式下不顯示翻譯

我認爲一個問題是我的裝載機 - 我的用戶@ NGX的通用/翻譯裝載機,但是當我在構造函數中添加translate.get,以測試它,它工作正常:

translate.get('works', {}).subscribe((res: string) => { 
    console.log(res); 
}); 

這個節目會導致節點控制檯(「 - WORKS」處於「en」模式或「 - DZIALA」處於波蘭模式),所以問題出現在模板呈現時。

app.component.ts

import {Component, OnInit} from '@angular/core'; 
import {TranslateService} from '@ngx-translate/core'; 

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

    constructor(public translate: TranslateService) { 
    translate.setDefaultLang('en'); 
    translate.use('en'); 

    translate.get('works', {}).subscribe((res: string) => { 
     console.log(res); 
    }); 
    } 

    ngOnInit() { 
    } 

    swtitchLang(lang: string) { 
    this.translate.use(lang); 
    } 
} 

<button (click)="swtitchLang('pl')">PL</button> 
<button (click)="swtitchLang('en')">EN</button> 

<ul> 
    <li><a routerLink="/" [translate]="'menu.home'"></a></li> 
    <li><a routerLink="about" [translate]="'menu.about'"></a></li> 
</ul> 
<h4>TEXT: <span [translate]="'works'"></span></h4> 

<router-outlet></router-outlet> 

不用翻譯文本是我的HTML源文件正常顯示(例如單詞 「text:」 app.component.html或「PL 」,‘EN’,但文本之後對譯routerLink和字:不顯示來源是這樣的:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title>My Home</title> 
     <base href="/"> 
     <meta name="viewport" content="width=device-width,initial-scale=1"> 
     <link rel="icon" type="image/x-icon" href="favicon.ico"> 
     <link href="styles.d41d8cd98f00b204e980.bundle.css" rel="stylesheet"> 
     <style ng-transition="ang4-seo-pre"></style> 
     <meta name="author" content="Coursetro.com"> 
     <meta name="keywords" content="angular seo, angular 4 universal, etc"> 
     <meta name="description" content="This is my Angular SEO-based App, enjoy it!"> 
    </head> 
    <body> 
     <app-root _nghost-c0="" ng-version="4.4.6"> 
      <button _ngcontent-c0="">PL</button> 
      <button _ngcontent-c0="">EN</button> 
      <ul _ngcontent-c0=""> 
       <li _ngcontent-c0=""> 
        <a _ngcontent-c0="" routerLink="/" href="/"></a> 
       </li> 
       <li _ngcontent-c0=""> 
        <a _ngcontent-c0="" routerLink="about" href="/about"></a> 
       </li> 
      </ul> 
      <h4 _ngcontent-c0="">TEXT: 
       <span _ngcontent-c0=""></span> 
      </h4> 
      <router-outlet _ngcontent-c0=""></router-outlet> 
     </app-root> 
     <script type="text/javascript" src="inline.3df9f7567cf0da6bbb0f.bundle.js"></script> 
     <script type="text/javascript" src="polyfills.14173651b8ae6311a4b5.bundle.js"></script> 
     <script type="text/javascript" src="vendor.aeb6cf8ce8b6e01353e0.bundle.js"></script> 
     <script type="text/javascript" src="main.203dfaf526cd10912720.bundle.js"></script> 
    </body> 
</html> 
+0

爲什麼不爲你的翻譯服務定義一個管道,這樣你就可以將數據綁定到它的字符串?結果將是'{{'some_string'|翻譯}} –

+0

謝謝 - 它的工作正常。你能解釋爲什麼嗎?我發現,'

'和'
{{ 'works' | translate }}
'工作,但是''不起作用。 –

+0

我的猜測是這是一個計時問題。例如,您正在嘗試在文件加載或服務準備就緒之前進行翻譯的情況。定義一個管道繞過它。 –

回答

0

爲什麼不DEFI爲您的翻譯服務提供一個管道,以便您可以將字符串數據綁定到它?結果將是{{'some_string' | translate}}