One of the great things about lodash is that it gives you all the building blocks to create some really powerful functions.

I was working on a site that needed to use dynamic translations. The way this is done today is usually through a function that is loaded in your components and called with a key that then gets mapped to whatever language you are using.

You can pass in variables to the function in order to translate sentences that include placeholders. For example, you might want to have a welcome message like “Hello {name_of_user}!”. Pretty common use case as you can imagine.

I managed to get a simple version of an intl function working by using a combination of get, template, and memoize functions in lodash.

What we are doing here is using first getting a value from our translation file. We use the template function to parse the value we find from our translation function using a pattern that looks for single words wrapped with curly braces. Like {this}. We can then pass in any variables we wanted to replace in that string. Finally, we use memoize to avoid recompiling the template on each additional call. This will just return the cached results of any translations instead of grabbing the value and parsing the string again.

If you find yourself needing a simple translation function, this could be a good option if you already have lodash in your project.