Core: Parameters in translation

Created on 31 Oct 2016  路  2Comments  路  Source: ngx-translate/core

I'm submitting a ... (check one with "x")

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[x ] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request

Current behavior

Hi guys, I'm getting trsanslation in my component thanks to the getTranslation function. The point is I can have multiple messages with multiple parameters and I can't figure it out how to get the translation tieh the parameters.

Here is my structure:

infobulle: [
                    {
                            codeMessage: "test1",
                            params: [
                                    "20 $"
                            ]
                    },
                    {
                            codeMessage: "test1",
                            params: [
                                    "50 $"
                            ]
                    }
            ]

codeMessage: "The value of {{value}} is low." is my translation.

Here is my result:

The level of {{value}} is low. The level of {{value}} is low.

I see that there is another function which is get() with parameters but when i'm trying that:

this._translateService.get(codeMessage, parametres).subscribe((res:string) => {
                messageFinal += res;
                console.log(messageFinal);
            });

I have the same result.

Expected/desired behavior

The level of 20$ is low. The level of 50$ is low.

Please tell us about your environment:

  • ng2-translate version: 2.5.0

  • Angular version: 2.0.0

  • Browser: [ Chrome 43 ]

  • Language: [ TypeScript 2.0.2 ]

Most helpful comment

Not really sure if I understand your setup correctly, but I think you are missing the key of the translation parameter you are trying to pass along.

When you have the following en.json:

{
    "test1": "My value is: {{value}}"
}

Every time you try to translate the key: test1, you need to specify this key and the value you want {{value}} to be translated with. So a correct call would be:

translateService.get('test1', {value: 'something'}).subscribe(res => {
   console.log('The result is:', res);
}); 

The {value: 'something'} is necessary as you could also want to pass along multiple parameters like so:

en.json:

{
    "test1": "My first value is: {{value1}}, my second value is: {{value2}}"
}

component:

translateService.get('test1', {value1: 'something', value2: 'something else'}).subscribe(res => {
   console.log('The result is:', res);
}); 

I also made a Plunker to demonstrate your desired behaviour: http://plnkr.co/edit/jubRTLSmEryVAUA3xArW?p=preview

All 2 comments

Not really sure if I understand your setup correctly, but I think you are missing the key of the translation parameter you are trying to pass along.

When you have the following en.json:

{
    "test1": "My value is: {{value}}"
}

Every time you try to translate the key: test1, you need to specify this key and the value you want {{value}} to be translated with. So a correct call would be:

translateService.get('test1', {value: 'something'}).subscribe(res => {
   console.log('The result is:', res);
}); 

The {value: 'something'} is necessary as you could also want to pass along multiple parameters like so:

en.json:

{
    "test1": "My first value is: {{value1}}, my second value is: {{value2}}"
}

component:

translateService.get('test1', {value1: 'something', value2: 'something else'}).subscribe(res => {
   console.log('The result is:', res);
}); 

I also made a Plunker to demonstrate your desired behaviour: http://plnkr.co/edit/jubRTLSmEryVAUA3xArW?p=preview

Thanks a lot it's what I needed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jellene4eva picture jellene4eva  路  3Comments

bjornharvold picture bjornharvold  路  3Comments

louisdoe picture louisdoe  路  3Comments

rbaumi picture rbaumi  路  4Comments

guysan picture guysan  路  4Comments