显示JSON键而不是翻译字符串

时间:2016-06-02 00:50:54

标签: nativescript ng2-translate

由于某种原因,下面的代码不会翻译我的字符串,即它显示我的json KEY(例如:MY_NEW_SITES)。希望你会看到我正在做的事情出错了

这是代码

main.ts

RewriteEngine on


RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example.com/$1 [NE,L,R]

site.ts

    // this import should be first in order to load some required settings (like globals and reflect-metadata)
    import { nativeScriptBootstrap } from "nativescript-angular/application";
    import { AppComponent } from "./app.component";

    // angular 
    import { Component, provide } from '@angular/core';
    import { HTTP_PROVIDERS } from '@angular/http';

    // libs
    import { TranslateLoader, TranslateService, TranslatePipe } from 'ng2-translate/ng2-translate';
    import { TNSTranslateLoader } from 'nativescript-ng2-translate/nativescript-ng2-translate';
    import { TNSFontIconService } from 'nativescript-ng2-fonticon';

    nativeScriptBootstrap(AppComponent, [
      HTTP_PROVIDERS,
      provide(TranslateLoader, {
        useFactory: () => {
          // pass in the path to your locale files
          return new TNSTranslateLoader('assets/i18n');
        }
      }),
      TranslateService,
      provide(TNSFontIconService, {
          useFactory: () => {
            return new TNSFontIconService({
              'fa': 'font-awesome.css'
            });
          }
        })  
    ]);

sites.html

import { Component, OnInit, ViewChild } from "@angular/core";

import { TranslateService, TranslatePipe } from 'ng2-translate/ng2-translate';

import { TNSFontIconService, TNSFontIconPipe } from 'nativescript-ng2-fonticon';
import { TNSTranslateLoader } from 'nativescript-ng2-translate/nativescript-ng2-translate';

@Component({
  templateUrl: 'pages/sites/sites.html',
  pipes: [TranslatePipe, TNSFontIconPipe]
})

export class SitesPage {
  public sites: ISiteInfo[];

  constructor(
              private fonticon: TNSFontIconService,
              translate: TranslateService) {

    this.sites = [];
  }
}

在app / assets / i18n下,我有2个json文件,即en.json和fr.json

例如:

en.json

<ActionBar [title]="'MY_NEW_SITES' | translate">
  <ActionItem (tap)="settings()">
    <StackLayout>
      <Label [text]="'fa-cog' | fonticon" class="fa"></Label>
    </StackLayout>
  </ActionItem>
  <ActionItem (tap)="addSite()"
      ios.position="right"
      android.position="popactionBarup">
    <StackLayout>
      <Label [text]="'fa-plus' | fonticon" class="fa"></Label>
    </StackLayout>
  </ActionItem>
</ActionBar>

  <StackLayout>
      <Label [text]="'MY_NEW_SITES' | translate"></Label>
  </StackLayout>

fr.json

{
   "MY_NEW_SITES": "My New Sites"
}

1 个答案:

答案 0 :(得分:1)

因为你没有过世 你的财产MY_NEW_SITES但字符串'MY_NEW_SITES'

更改此

  <StackLayout>
      <Label [text]="'MY_NEW_SITES' | translate"></Label>
  </StackLayout>

到这个

  <StackLayout>
      <Label [text]="MY_NEW_SITES | translate"></Label>
  </StackLayout>

您的问题已解决(同样适用于您的标题绑定)