scelis / twine

Twine is a command line tool for managing your strings and their translations.
Other
840 stars 151 forks source link

Improved Android HTML escaping #228

Closed sebastianludwig closed 6 years ago

sebastianludwig commented 6 years ago

Previously discussed in #212.

The original issue was that Twine escaped all < with &lt. This made it impossible to use references to styled strings directly in a layout file. #216 changed this behaviour to always leave styling tags alone and never escape <b> and friends. This however made it very cumbersome to combine placeholders with styling. Previous to #216 it was possible to write

String text = getString(R.string.styledPlaceholder, "Twine");
styledPlaceholderGetStringTextView.setText(Html.fromHtml(text));

as suggested by the documentation.

Currently (after #216) users have to write

SpannedString text = new SpannedString(getText(R.string.styledPlaceholder));
String html = Html.toHtml(text);
String formatted = String.format(html, "Twine");
styledPlaceholderGetStringTextView.setText(Html.fromHtml(formatted));

This PR tries to fix the introduced regression and offer the best of both worlds:

TL;DR: We made it worse, this makes it better.

As you were involved in the previous discussion @Aqua-Ye, @jpm-polymorph, any thoughts about this?

jpm-polymorph commented 6 years ago

👍

I'm happy with this solution. It's unfortunate that we have to handle the two cases in two separate ways, but we can blame Android for that as they handle strings in two different ways.

scelis commented 6 years ago

This looks great! Thanks so much for all of the investigations and the fix for this complicated issue. One minor suggestion for how to improve the README. If you have time to fix that up, great! Otherwise I'll merge this in the next day or two and update it myself.

scelis commented 6 years ago

Actually, I'll get this merged now.