149. interpolation

Is this helpful ?

from all members: - accepted / - tried

Have you ever added i18n support to your projects?

Take i18next as an example, generally the keys and translations are kept separately, like this JSON below.

  "evaluation": "BFE.dev is {{evaluation}}"

At places where this key is used, we can then interpolate the string by passing a data object.

t('evaluation', {evaluation: 'fantastic'});
// "BFE.dev is fantastic"

Now, please create a similar t() function which accepts the translation directly.

1. it supports {{ and }} as delimiters

Let's make it clearer and simpler, when a new pair {{ is met, characters until the following }} are treated as the property name.

For all the other cases, they should not be treated as delimiters.

t('BFE.dev is {{{evaluation}', {evaluation: 'fantastic'});
// "BFE.dev is {{{evaluation}"

t('BFE.dev is {{{evaluation}}}', {'{evaluation': 'fantastic'});
// "BFE.dev is fantastic}"

t('BFE.dev is {{evalu ation}}', {'evalu ation': 'fantastic'});
// "BFE.dev is fantastic"

2. if no data is passed or no property exists, just leave it empty

t('BFE.dev is {{evaluation}}');
// "BFE.dev is "

Let's try to solve this problem within 15 mintues.