0

I would like to use ES6 template strings as templates for the translations in my Node.js application.

I have a JSON file en_GB.json like this:

{
  "app.template": "This is ${foo} I ${bar}",
  "app.foo": "bar"
}

In Node I do this:

const translations = require('./en_GB.json')
const foo = 'what'
const bar = 'want'

console.log(translations['app.template']) // Outputs This is ${foo} I ${bar}

What I want to output is "This is what I want'

Is this possible without using a helper function?

Hedge
  • 16,142
  • 42
  • 141
  • 246

1 Answers1

1

Will this work?

https://www.npmjs.com/package/stringinject

https://github.com/tjcafferkey/stringinject

var string = stringInject("This is a {0} string for {1}", ["test", "stringInject"]);

// This is a test string for stringInject 
codebytom
  • 418
  • 4
  • 12
  • No, I need to be able to specify the variables by name. Also I am looking for a native way to do this. – Hedge Jul 19 '17 at 10:57
  • 1
    I have updated my NPM package to allow for this. Is this what you're looking for? https://www.npmjs.com/package/stringinject – codebytom Jul 19 '17 at 15:25