i18n-msg icon indicating copy to clipboard operation
i18n-msg copied to clipboard

Add support for placeholders

Open soonick opened this issue 10 years ago • 1 comments

I need translated strings with placeholders, so I looked at the i18n chrome spec(https://developer.chrome.com/extensions/i18n-messages) and created this based on it: https://github.com/ebidel/i18n-msg/pull/25

I'm trying to follow the spec without implementing all of it so it can probably be extended in the future.

Currently there are two ways we can use placeholders:

Placeholders in the json file

{
  "bye": {
    "message": "Come back to $our_site$ soon!",
    "placeholders": {
      "our_site": {
        "content": "Example.com",
      }
    }
  }
}

These work pretty much as documented by chrome. We have a placeholders attribute that contains the placeholders. In this case there is one called our_site with a content of Example.com. When we find this placeholder we can do a simple string replacement. Replace $our_site$ with with the content of the our_site placeholder.

Placeholders in a component attribute

{
  "bye": {
    "message": "Come back to $our_site$ soon!",
    "placeholders": {
      "our_site": {
        "content": "$1",
      }
    }
  }
}

In this case the content of the placeholder is $1. In this case I am currently just checking that the first character is a $. If it is then I assume you are looking for an attribute. In this example, since it is $1 it will look for the first placeholder passed to the component($2 is the second, and so on). The placeholders are passed as an array in an attribute called placeholders:

<i18n-msg msgid="seconds" placeholders='["Example.com"]'>PLACEHOLDER_STRING</i18n-msg>

The result in both cases is: Come back to Example.com soon!

soonick avatar Nov 11 '15 07:11 soonick

+1

rohitkhode avatar Jan 18 '16 06:01 rohitkhode