0

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

agusgambina
  • 6,229
  • 14
  • 54
  • 94
  • Which syntax rules do you have to indicate that a substring is to be evaluated? – trincot Nov 10 '22 at 21:16
  • what do you mean `t` is returned after you get the interpolated string, like `mainFunction` returns `targetString` and then sometime later `getTranslationService` returns `t` for you? Is `getTranslationService` like a network request/asyncronous? – Coty Embry Nov 10 '22 at 21:24
  • Thank you for your comment @CotyEmbry, I will try to clarify that. The t is a function that I get asynchronously, but once it gets resolved, `t` can evaluate in a synchronous way. That makes sense? – agusgambina Nov 10 '22 at 22:35
  • @thank you for your comment @trincot, sorry I updated the question because I wrote it in the wrong way the first time. The interpolated string has the `$`, the parameter the function is getting is This is my string $t(some.value) – agusgambina Nov 10 '22 at 22:48
  • How will you know where the substring (that needs evaluation) ends? Like what if the string were `This is my string $1+t(some.value+1)+1`? – trincot Nov 10 '22 at 22:53
  • 1
    Does this answer your question? [Can ES6 template literals be substituted at runtime (or reused)?](https://stackoverflow.com/questions/30003353/can-es6-template-literals-be-substituted-at-runtime-or-reused) – Phil Nov 10 '22 at 23:01
  • @trincot thank you for your suggestion, I tried but it didn’t work. I found the answer I will post it in the question. – agusgambina Nov 13 '22 at 10:18
  • 2
    I don't know what you refer to when you say "I tried it". I didn't propose anything. On the contrary, I asked for clarifications because your question leaves doubt on some aspects. Also, if you have an answer, make sure *not* to post it in the question, but in the answer area. – trincot Nov 13 '22 at 10:19
  • thank you for the comment @Phil, this lead me a little bit to the answer I made some assumptions that were wrong. – agusgambina Nov 13 '22 at 10:19
  • Thank you @trincot I updated the question/answer to put it in the right place. – agusgambina Nov 15 '22 at 12:54

2 Answers2

0

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.

agusgambina
  • 6,229
  • 14
  • 54
  • 94
-2

you can do:

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Yukulélé
  • 15,644
  • 10
  • 70
  • 94