Search code examples
ecmascript-6async-awaitstring-interpolation

ES6 How to process string interpolation if the function is returned after the string


I have a function that returns an interpolated string for example.

`This is my string $t(some.value)`

The issue I am facing is that t is returned after I get the interpolated string. For example

const mainFunction = (targetString) => {
    
    const { t } = getTranslationService();

    return targetString;

}

I want to resolve the value and return the processed string in the mainFunction. I tried with eval but it didn't work


Solution

  • The answer in case it is useful for someone else

    My first assumption was wrong, the function t is async.

    The second was it was more complex than I expected, I needed to create a locale folder with a file en-US.json since how it works and all these it is relying on i18n

    Finally, the target string should be in the JSON file. the t function will call a key from the JSON file and the targetString (enclosed between curly braces) will be translated. All this works in an asynchronic way.