Search code examples
angular-translate

How to translate and use values in array order?


I have a string "Hello, my name is {0}. I'm from {1}" and a sorted array ["Michael", "Berlin"].

I want to put the first array element in {0}, the second in {1}, and so on. How can I achieve this with angular-translate?


Solution

  • If you can convert your array from simple "keyless" sorted to a nice json associative object, it will be easier to use a template like

    Hello, my name is {name}. I'm from {city}
    

    and then let the module do the work e.g. through the directive (taking over the array definition from the other answer):

    <span translate translate-values="{'name': vm.array[0], 'city': vm.array[1]}"></span>
    

    Using this approach, you'll get what you want. But it would be advisable to take a structured object already at the beginning instead of a simple array.

    Documentation on the replacement functionality can be found here