ttag-org / ttag

:orange_book: simple approach for javascript localization
https://ttag.js.org/
MIT License
345 stars 43 forks source link

Translation template substitution expression format #153

Closed dimaqq closed 5 years ago

dimaqq commented 5 years ago

TIL that ttag is finicky about template format:

Suppose I have code: <div>{t`User ${match.params.id}`}</div> (no spaces around the expression)

ttag update produces msgid "User ${ match.params.id }" in the translation file (with spaces around the expression)

If the translation is: msgstr "𝓤𝓼𝓮𝓻 ${match.params.id}" (without spaces around the expression), then template will not be substituted by expression value.

OTOH, if the translation is: msgstr "𝓤𝓼𝓮𝓻 ${ match.params.id }" (with spaces around the expression), then substitution works.

This was very unexpected to me, as JavaScript ignores whitespace in expressions.

I've ran into this, because I've tried https://www.smartcat.ai which offers a few "automated" translation engines, of which only Google Neural understands the templates (the rest translate the words match, etc.), and the translations are often usable. Even though, that automated translation engine removes extra whitespace from around the expression :(

If the whitespace around the expression is important, let's document that. If it's not important, let's follow JavaScript semantics.

AlexMost commented 5 years ago

HI @dimaqq ! That seems like a bug, because initially babel-plugin-ttag was designed to ignore spaces.

AlexMost commented 5 years ago

Actually babel-plugin-ttag works correct, so that it ignores spaces. And I have found that the actual ttag library does not handle that case. Going to fix that.

AlexMost commented 5 years ago

you can try fix in 1.7.4

dimaqq commented 5 years ago

❤️ Thank you! ❤️

piecyk commented 1 year ago

Hi @AlexMost i hit similar issue, but with google translate.

Having a string tHello ${name}, it will be extracted as msgid "Hello ${ name }", passing this string to translate, will produce Hallo ${ Name } but if msgid would not add spaces but just msgid "Hello ${name}" then it works correctly Hallo ${name}

Can i control if spaces are added? Thanks!