Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for interpolation #18

Open
jeremycod opened this issue Jul 31, 2020 · 2 comments
Open

Support for interpolation #18

jeremycod opened this issue Jul 31, 2020 · 2 comments

Comments

@jeremycod
Copy link

Hi,

I've applied this approach in my project and only now I've realized it doesn't support interpolation.
Any suggestion how to solve that without having to completely apply different library? I'm using serverless approach, so it was a little bit tricky to make multilanguage approach work.

@fwojciec
Copy link
Owner

I don't use it so I've never implemented. This was never intended as a full-featured library, but more like an idea to get people started in creating their own implementation. All I can suggest is have a look at how other libraries do interpolation and see if you can implement your own version - I don't expect this to be particularly difficult. Good luck!

@ogostos
Copy link

ogostos commented Aug 10, 2020

Basic idea is pretty straightforward

You pass translated string (stored accordingly) and variables to be plugged to helper function.

const interpolation = (str, ...val) => {
  let i = 0;
  return str ? str.replace(/({\w+})/g, () => val[i++]) : str;
};

and if your translation has to incorporate any variable simply store it like so

...
"someKey": "{this} will be interpolated, and also {this one}"; // strings in brackets are only descriptive, they do not effect anything
...

E.g.

interpolation(translationStrings[someKey], 'one', 'two') // => One will be interpolated, and also two

Brackets {} are arbitrary, you can pick whatever symbol you want, the one which suits to your needs and fits translations. Just remember to update regexp in interpolation helper function.

You can define helper function in your shared translation logic, e.g for this project we have context object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants