jeresig / i18n-node-2

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
507 stars 79 forks source link

Feature Request: General Place-holders #84

Open dortamur opened 8 years ago

dortamur commented 8 years ago

Would it be feasible to add support for general place-holders, where one localised string could refer to another one? This would enable common terms/phrases to be used in the content of others, without requiring parameter parsing at the code level.

For example;

{
   "contact service desk": "contact Service Desk (x3000)",
   "wrong id": "You've entered a wrong ID. Please try again, or {{contact service desk}} for further assistance",
   "db error": "Oops! Something is wrong with our servers. If this problem persists, please {{contact service desk}}"
}

In this way, any mention of contacting the Service Desk is consistent, and if the message (eg; the phone extension) changes, it's reflected across all other references.

Preferably, such a feature would be able to be enabled/disabled, and the place-holder syntax be customisable, to avoid clashes with real content syntax in templates, etc...

Here's an idea for an alternative syntax:

{
   "contact service desk": "contact Service Desk (x3000)",
   "wrong id": ["You've entered a wrong ID. Please try again, or %p for further assistance", "contact service desk"],
   "db error": ["Oops! Something is wrong with our servers. If this problem persists, please %p", "contact service desk"]
}

This syntax is similar to the "%s" place-holder syntax, but in this case, the substituted value is another i18n entry, named within the locales config.

gjuchault commented 8 years ago

I do believe that is already working, by putting %s, and in argument another i18n.__ call

var greeting = i18n.__('Hello %s, how are you today? How was your %s?',
    'Marcus', i18n.__('weekend'));```
dortamur commented 8 years ago

I know you can pass in parameters from code - my suggestion is to support parameters internally to the list of localised strings. In that way, one string could include another, without the calling application needing to be aware that any parameter substitution is taking place.

In my example, I showed a string for contacting the servicedesk, and then referenced it in several other strings. If the contact details for the servicedesk ever changed, you'd only have to change the one string, rather than all the strings it might be referenced.