I have a en.json
file as below:
{
"doorClosing": {
"defaultMessage": "Doors closing",
"description": "Elevator doors are being closed"
},
"floorSelected": {
"defaultMessage": "Floor selected: {floorSelected}",
"description": "`The floor ${lift.floorSelected} has been selected`"
},
"floorSelectedInvalid": {
"defaultMessage": "Invalid floor",
"description": "The selected floor is not valid"
},
"idle": {
"defaultMessage": "",
"description": ""
},
"init": {
"defaultMessage": "Initialisation",
"description": "The system is being initialized"
},
"liftMoving": {
"defaultMessage": "Elevator moving",
"description": "Elevator is being moved to the floor selected"
}
}
Then later in the code, I use let dashboardMsg = intl.formatMessage({id:msg});
where msg is one of the key contained in the json file.
About the key floorSelected
, when I use it ${lift.floorSelected}
is not translated by the number contained in this variable.
What's wrong? Is there another way or do I have to do that manually?
What you want is called formatted argument. It's part of the ICU Message Syntax which you can see here for more reference.
In your code
You can also specify the number type for the argument to customize how the number should be formatted. Here is an example:
When you call this method
It will be translated to this
Live Demo