angularjsangular-translate

angular-translate - variable and interpolation in the same translation key


Can I have a variable and an interpolation in the same translation key?

Something like this:

"notice" : "{{subject}} {RES, select, is{is} are{are} other{are}}",

gives me the error on the web browser console:

Object { message: "Expected [ \t\n\r] or [0-9a-zA-Z$_] but \"{\" found.", expected: (8) […], found: "{", location: {…}, name: "SyntaxError", stack: "" }

When {{subject}} is removed, it works.

Thank you in advance for help.


Solution

  • If you are trying to use angular-translate pluralization interpolation message format in combination with normal variable interpolation, it can be done differently. From the docs:

    The drawback

    Actually it's pretty cool that we are able to use MessageFormat as our interpolation engine. Unfortunately, when replacing MessageFormat interpolation with angular-translate's default interpolation, there's a big problem. Take a look at the following code: Do you see any difference?

    {   
       "DEFAULT_INTERPOLATION": "This is a translation that uses default
       interpolation with a dynamic value: {{value}}",   
    
      "MF_INTERPOLATION": "This is a translation that uses MessageFormat
       interpolation with a dynamic value: {value}" 
    }
    

    Exactly. MessageFormat uses a different syntax for its interpolation. This means, when using MessageFormat interpolation, you have to rock over all of your translations, check if they use any kind of variable replacement and update them to match the right interpolation syntax. This isn't a cool thing.