角度:通过静态变量访问{ngx-translate / core

时间:2018-07-07 17:13:16

标签: angular static internationalization translate

我正在使用快速应用模板https://github.com/emonney/QuickApp。它使用@ ngx-translate / core进行国际化。当翻译非静态变量的内容时,翻译函数可以正常工作,我的疑问是:如何从静态变量中翻译内容。

翻译服务文件:

import { Injectable } from '@angular/core';
import { TranslateService, TranslateLoader } from '@ngx-translate/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/observable/of';
@Injectable()
export class AppTranslationService {
    private _languageChanged = new Subject<string>();
    readonly defaultLanguage = "en";
    constructor(private translate: TranslateService) {
        this.setDefaultLanguage(this.defaultLanguage);
    }
    addLanguages(lang: string[]) {
        this.translate.addLangs(lang);
    }
    setDefaultLanguage(lang: string) {
        this.translate.setDefaultLang(lang);
    }
    getDefaultLanguage() {
        return this.translate.defaultLang;
    }
    getBrowserLanguage() {
        return this.translate.getBrowserLang();
    }
    useBrowserLanguage(): string | void {
        let browserLang = this.getBrowserLanguage();

        if (browserLang.match(/en|fr|de|ar|ko|pt/)) {
            this.changeLanguage(browserLang);
            return browserLang;
        }
    }
    changeLanguage(language: string = "en") {
        if (!language)
            language = this.translate.defaultLang;
        if (language != this.translate.currentLang) {
            setTimeout(() => {
                this.translate.use(language);
                this._languageChanged.next(language);
            });
        }
        return language;
    }
    getTranslation(key: string | Array<string>, interpolateParams?: Object): string | any {
        return this.translate.instant(key, interpolateParams);
    }
    getTranslationAsync(key: string | Array<string>, interpolateParams?: Object): Observable<string | any> {
        return this.translate.get(key, interpolateParams);
    }
    languageChangedEvent() {
        return this._languageChanged.asObservable();
    }
}

使用非静态功能进行翻译:

import { Component, OnInit, AfterViewInit, TemplateRef, ViewChild, Input } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { AlertService, DialogType, MessageSeverity } from '../../services/alert.service';
import { AppTranslationService } from "../../services/app-translation.service";
import { AccountService } from '../../services/account.service';
import { Utilities } from '../../services/utilities';
import { Role } from '../../models/role.model';
import { Permission } from '../../models/permission.model';
import { RoleEditorComponent } from "./role-editor.component";
@Component({
    selector: 'roles-management',
    templateUrl: './roles-management.component.html',
    styleUrls: ['./roles-management.component.css']
})
export class RolesManagementComponent implements OnInit, AfterViewInit {
    columns: any[] = [];
    rows: Role[] = [];
    rowsCache: Role[] = [];
    allPermissions: Permission[] = [];
    editedRole: Role;
    sourceRole: Role;
    editingRoleName: { name: string };
    loadingIndicator: boolean;

    @ViewChild('indexTemplate')
    indexTemplate: TemplateRef<any>;

    @ViewChild('actionsTemplate')
    actionsTemplate: TemplateRef<any>;

    @ViewChild('editorModal')
    editorModal: ModalDirective;

    @ViewChild('roleEditor')
    roleEditor: RoleEditorComponent;

    constructor(private alertService: AlertService, private translationService: AppTranslationService, private accountService: AccountService) {
    }


    ngOnInit() {

        let gT = (key: string) => this.translationService.getTranslation(key);

        this.columns = [
            { prop: "index", name: '#', width: 50, cellTemplate: this.indexTemplate, canAutoResize: false },
            { prop: 'name', name: gT('roles.management.Name'), width: 200 },
            { prop: 'description', name: gT('roles.management.Description'), width: 350 },
            { prop: 'usersCount', name: gT('roles.management.Users'), width: 80 },
            { name: '', width: 130, cellTemplate: this.actionsTemplate, resizeable: false, canAutoResize: false, sortable: false, draggable: false }
        ];

        this.loadData();
    }
...

我需要翻译的静态变量:

import { Injectable } from '@angular/core';
import { HttpResponseBase, HttpResponse, HttpErrorResponse } from '@angular/common/http';
@Injectable()
export class Utilities {

    public static captionAndMessageSeparator = ":";
    public static noNetworkMessageCaption = "No Network";
    public static noNetworkMessageDetail = "The server cannot be reached";
    public static accessDeniedMessageCaption = "Access Denied!";
    public static accessDeniedMessageDetail = "";

我试图将转换函数转换为静态函数,但以下行不能在静态函数内使用:

return this.translate.instant(key, interpolateParams);

0 个答案:

没有答案