出于某种原因,下面的代码不会翻译我的字符串,即它显示我的 json KEY(例如:MY_NEW_SITES)。希望你会发现我正在做的事情有问题
这是代码
main.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'
});
}
})
]);
网站.ts
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 = [];
}
}
网站.html
<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>
在 app/assets/i18n 下,我有 2 个 json 文件,即 en.json 和 fr.json
前任:
en.json
{
"MY_NEW_SITES": "My New Sites"
}
fr.json
{
"MY_NEW_SITES": "Mes nouveaux sites"
}