1

JavaScript (ES6), TypeScript

I have the localized resources of string type. I need to use these strings as a format. This is the example of my localized string: getValue: The '{0}' key not found in the '{1}' map. I need replace {0} and {1} by the values. Is exist any simple way to do it without using of replace() or indexOf() + splice()?

P.S. Template literals cannot be used in this case, unfortunately.

6
  • 2
    Take a look at Template Literals and see if those fit your use case. Commented Oct 12, 2017 at 15:22
  • 1
    No. Template strings in JS can help sometimes but interpolated at the point they are defined and do not have a simple printf formatting method because reasons. Just use handlebars like everybody else. Commented Oct 12, 2017 at 15:25
  • @zero298 I know about it and it can't be used in this case. Commented Oct 12, 2017 at 15:27
  • See stackoverflow.com/questions/20070158/… for both string literals, and a string format polyfill. Commented Oct 12, 2017 at 15:29
  • I would edit your question just to be clearer that these strings have to be transformed at run time and why Template Literals won't work. You have an interesting question, but I wouldn't want it to get hammered with answers telling you to use literals when they won't work. Commented Oct 12, 2017 at 15:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.