2017-03-29 39 views
0

我有以下问题。如何在ng2-translate中翻译@Input() from angular2?将非常感谢任何帮助。如果您对我的代码有任何其他问题,请询问。ng2 translate和@Input()

我越来越

您的电话号码:123 {{电话}}

我的代码

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

@Component({ 
    selector: 'child', 
    template: ` 
     <span> {{ 'Contact.Text' | translate:telNumber:telephone }}</span> 
     <input type="text" placeholder="{{ 'Contact.Input' | translate }}"> 
    ` 
}) 

export class ChildComponent implements OnInit { 
    @Input() telephone: number; // <- this one 
    telNumber = { value: 123 }; 
    constructor() { } 
    ngOnInit() { } 
} 

en.json

{ 
"Navigation": { 
    "First": "Main Page", 
    "Second": "Menu", 
    "Third": "Contact", 
    "ChangeLanguage": "Change language" 
}, 
"Mainpage": { 
    "Title": "Welcome to the home page", 
    "Content": "This home page is the most beautiful homepage you have ever seen" 
}, 
"Menu": { 
    "Title": "Animals", 
    "FirstAnimal": "Dog", 
    "SecondAnimal": "Cat", 
    "ThirdAnimal": "Cow", 
    "FourthAnimal": "Pig", 
    "FifthAnimal": "Bird" 
}, 
"Contact": { 
    "Title": "Contact to us", 
    "Text": "Your phone number: {{ value }} {{ telephone }}", 
    "Input": "Deutschland Name" 
} 
} 
+0

我不认为你可以像这样翻译动态输入,除非你已经有了给定输入的翻译。纠正我,如果我错了。 – John

+0

但它只是一个数字。我不想做{{'Contact.Text'| translate:telNumber}} {{telephone}}。我想将此电话发送至en.json – qwerty1234567

+1

对不起,请问,但您为什么要将电话号码发送到'en.json'?任何语言的数字都不一样?我想你有你的理由,我只是好奇。 – John

回答

1

试试这个

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

@Component({ 
    selector: 'child', 
    template: ` 
     <span> {{ 'Contact.Text' | translate:telParams }}</span> 
     <input type="text" placeholder="{{ 'Contact.Input' | translate }}"> 
    ` 
}) 

export class ChildComponent implements OnInit { 
    @Input() telephone: number; // <- this one 
    get telParams() { 
     return { value: 123, telephone: this.telephone }; 
    } 
    constructor() { } 
    ngOnInit() { } 
} 

顺便说一句,使用ngx-translatehttps://github.com/ngx-translate,而不是ng2-translate

+0

你认为我可以改变TranslatePipe采取变量(不是对象?)。 ngx是为每个角度版本? – qwerty1234567

+0

也许你可以克隆项目,然后修改使用另一种方法。 –

相关问题