2017-10-06 207 views
0

我用這個教程沒有翻譯

https://ionicframework.com/docs/developer-resources/ng2-translate/

運行的一切,我可以把JSON格式,但不能打印輸出

en.json

{ 
    "HELLO": "hola", 
    "hi":"sa" 
    } 

NGX-轉換輸出的回報。 ts構造函數;

constructor(public navCtrl: NavController, public navParams: NavParams, public translate: TranslateService) { 
    translate.setDefaultLang('en'); 
    } 

.html block;

<div style="width: 100%;">   
     <p class="baslik">ACCOUNT</p> 
     <button ion-item> 
      <ion-label>{{ 'hi' | translate }}</ion-label> 
     </button> 
     <button ion-item> 
      <ion-label>{{ 'hi' | translate }}</ion-label> 
     </button> 
    </div> 

但是輸出;

當添加按鈕點擊事件,該代碼塊;

this.translate.use('en').subscribe(
     value => { 
     // value is our translated string 
     console.log(value); 
     } 

en.json return log;

enter image description here

回答

1

我跟着你提供,並可能重現你的問題的教程。

我也有在GitHub上NGX-翻譯存儲庫的正式文件一看,有到教程的差異。

在本教程中,他們使用的HTTP類角的HTTP模塊的加載文件:

export function createTranslateLoader(http: Http) { 
    return new TranslateHttpLoader(http, './assets/i18n/', '.json'); 
} 

在正式文件,他們正在使用HttpClient的類角通用模塊的。更改你的應用程序模塊如下:

import { HttpClientModule, HttpClient } from '@angular/common/http'; 

export function createTranslateLoader(http: HttpClient) { 
    return new TranslateHttpLoader(http, './assets/i18n/', '.json'); 
} 

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

還有就是官方文檔

NB的評論:如果你仍然對角< 4.3,請使用HTTP從@角/ HTTP與[email protected]

+0

我已經添加了教程中的所有代碼,但返回的是輸出 –

+0

您正在使用哪個角度版本? – Daniomi

+0

4.1.3 ---------------- –

1

我不熟悉您使用的翻譯框架,但它要使用自定義翻譯的載文看,但你永遠不叫:

translate.setTranslation('en', myTranslation); 

其中myTranslation是你的對象。

在你的代碼,你設置爲默認的'en'是一個字符串(它在引號),看起來是某種標識符,翻譯服務使用決定什麼後備語言是:

方法:

setDefaultLang(郎鹹平:字符串):設置默認語言爲 後備

你想使用:

setTranslation(郎鹹平:字符串,翻譯:對象,shouldMerge:布爾= FALSE):手動,如果你想翻譯附加替換它們

,而不是設置翻譯給定的語言,設置shouldMerge爲true的對象