【问题标题】:ng2 translate and @Input()ng2 翻译和@Input()
【发布时间】:2017-03-29 10:43:33
【问题描述】:

我有以下问题。如何在ng2-translate 中从angular2 翻译@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"
}
}

【问题讨论】:

  • 我认为您不能像这样翻译动态输入,除非您已经对给定输入进行了翻译。如果我错了,请纠正我。
  • 但这只是一个数字。我不想做 {{ 'Contact.Text' |翻译:电话号码 }} {{ 电话 }}。我想把这个电话发给 en.json
  • 不好意思问,为什么要把电话号码发给en.json?任何语言的数字都不一样吗?我想你有你的理由,我只是好奇。
  • 有时我想使用“您的电话号码:12334。谢谢”。你知道,文本中的数字(中心)不是最后:)

标签: angular input ng2-translate


【解决方案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-translate https://github.com/ngx-translate 而不是ng2-translate

【讨论】:

  • 您认为我可以更改 TranslatePipe 以获取变量(不是对象吗?)。 ngx 适用于每个角度版本?
  • 也许你可以克隆项目然后修改以使用另一种方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多