gettext-extractor icon indicating copy to clipboard operation
gettext-extractor copied to clipboard

(feature/advice) Parse HTML from JS strings

Open MisterTicot opened this issue 6 years ago • 4 comments

In my softwares, I have template literals from which I generate HTML components. Example:

new Gui(`<span><i18n>Hello</i18n>, %{name}!</span>`, { name: "Lukas" })

However, this "Hello" won't get extracted. So for now I do:

new Gui(`<span>${__("Hello")}, %{name}!</span>`, { name: "Lukas" })

However, this is a bad solution as translations can now inject html & executable scripts.

Is there any other way to make those translatable string extractable?

MisterTicot avatar Feb 11 '19 11:02 MisterTicot

Hi, that's currently not possible out of the box, but it would certainly be possible to implement such a feature. Would you expect it to just parse every string as HTML or mark the relevant ones somehow (e.g. using tagged template literals) ?

lukasgeiter avatar Feb 11 '19 19:02 lukasgeiter

I think ideally every string would be parsed including quote/double quote. I'm assuming the parser fails gracefully when the content is not HTML (I think it does), and that tags such as <i18n>...</i18n> don't happen mistakenly.

MisterTicot avatar Feb 12 '19 03:02 MisterTicot

It certainly would. I'm a bit concerned about the performance impact this might have in a larger project with lots of non-html strings. I think the best idea is probably to implement both options, parsing all strings or only those which are marked with a certain tag. I'll try to implement this in the next feature release. Unfortunately I can't give you an estimation on when it will be ready, but I'll let you know when the release is out.

lukasgeiter avatar Feb 13 '19 15:02 lukasgeiter

That's awesome, looking forward for it :)

MisterTicot avatar Feb 13 '19 15:02 MisterTicot