Access to Angular2 translation from a component or service

I am currently reworking my first Angular2 application based on recommendations at https://angular.io/docs/ts/latest/cookbook/i18n.html

The examples always indicate how to apply the attribute i18nto the code template and how the template code is interpolated.

How can I access localized text from component code (file .ts) or inside a service? I need this to interact with some JavaScript libraries that I use, where I need to call a JavaScript function with localized text.

+4
source share
2 answers

ng2-translate, TranslateService:

constructor(private translateService: TranslateService) { }

get(translationKey: string), Observable.

this.translateService.get('stringToTranslate').subscribe(
    translation => {
        console.log(translation);
    });
+6

.

import {Component, Input, OnInit} from '@angular/core';

@Component({
    selector: 'app-sandbox',
    templateUrl: 'sandbox.html'
})
export class SandboxComponent implements OnInit {
    @Input()
    public title: string;

    constructor() {
    }

    ngOnInit() {
        console.log('Translated title ', this.title);
    }
}

:

<app-sandbox i18n-title title="Sandbox"></app-sandbox>

, . title ts.

+3

Source: https://habr.com/ru/post/1660510/


All Articles