-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTML tags in strings #212
Comments
Awesome, thanks for filling out the issue template 👍 (it's new, that's why I'm excited ;-)) Currenty the backticks only preserve leading and trailing spaces. Why do you want to preserve the formatting tags? And which Android method are you using to read the Strings? |
I use the strings directly in my layout.xml file like so:
When a the string is surrounded by |
I see. Do you know of a way to store the string with HTML tag formatting in the XML file so it can be retrieved in code and be used in a layout? As far as I know unescaped tags will be stripped/lost when using |
I found a nice clear explanation here: Another solution is to wrap the HTML in |
Awesome find, thank you very much! So the solution would be:
right? |
That is a possibility, but feels hackish. I would suggest letting the grave (`) escaped strings mean do not escape. And also allowing substrings to be escaped. In that case you have freedom to do things like:
|
Mh, I actually thought it'd be a lot cleaner and closer to how it's intended to work on Android. Why do you think that would be hackish? |
I just prefer having one generic rule ( don't touch anything inside `` ) above having 4 exceptions ( |
@scelis, what's your opinion? |
I would rather not make the rules around Twine backtick escaping more complicated, especially as this particular issue seems to only manifest with the Android formatter. I tend to agree with @sebastianludwig's suggestions above, which don't seem hacky to me as they appear to be documented in the Android string resources documentation. |
Also, why is the |
That is as suggested by the Android docs 🤷♂️ |
I think we should have a way to choose if we want the Consider this output containing a mix of encoded and not encoded strings: This is maybe a use case that is not so common, but preferably our solution must be versatile enough to support this. Just not encoding a specific list of strings, including |
I don't have an Android dev environment at hand, but shouldn't these (rather exotic) cases be solvable with
|
In strings.xml:
This is an acceptable solution. Another tag we should support is |
This feature makes string formatting much more difficult (or impossible). |
@Aqua-Ye We tried to match the behavior and expectations as explained in the Android docs here: http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling Can you use:
And then in your code use: |
Where do I pass "world" in your snippet? From the doc, we should pass escaped entities in this case:
|
Ok, so then does this work for your scenario?
|
Yes. The problem is that you now always generate:
|
Ah, sorry, I think I understand now. Perhaps we are being too clever with Twine escaping HTML entities and instead Twine should just pass along whatever it has to the XML file and we let developers handle this themselves when they are creating their strings. @sebastianludwig thoughts? |
/cc @jpm-polymorph |
Take the string: So not escaping at all won't work either. Perhaps there is some middle ground solution? |
@jpm-polymorph I liked the idea of using ` to preserve the original string. Why didn't you go for this solution instead? |
What if we continue to escape characters and entities that must be escaped (like I'm also not sure that ` preservation works if we want the strings to look one way on iOS and another on Android. How do we accomplish that goal? These Android escaping rules seem really complicated and intense. |
Get parameters for a
This is becoming quite a popular use case with deep linking support in Android. |
Currently I am using two separate strings in my Twine file to support both iOS and Android: This is however more expensive ($$$) to get translated as we are billed per word. For now it is an ok workaround though. The main thing I need is to have unescaped html tags propagating from twine to Android. For these few cases I can have duplicate strings for iOS. Currently I am still editing the exported file by hand every time to check that the escaping didn't break the href. |
If we go back to escaping the About the backtick approach: Even though it certainly could be implemented, I strongly dislike the idea of changing your multi platform localization file depending on how it's used in one platform. It just feels wrong. Another approach: string references in layout XML files can, by definition, only be used for strings without placeholders. If a string contains a placeholder, it must be retrieved and formatted via code. How about:
From a users perspective that would mean:
|
I don't think having 2 different behaviours of escaping depending if the string contains placeholders is a better solution... It will be even more confusing for developers. |
How is using strings in layouts tied to Android Studio? Isn't all that either resolved by the compiler or the Android runtime? I'm a bit lost, could you summarize your proposed solution again? (If you have one :-)) |
Sorry I wasn't thinking straight about Android Studio :/ |
@jpm-polymorph, @scelis opinions? |
The first answer in this thread seems to work: I tested as follows and can confirm it works. strings.xml: MainActivity.java:
Is this sufficient @Aqua-Ye? |
That works perfectly @jpm-polymorph! Thanks. |
Does this mean that there is a valid workaround and that twine is functioning as best as it can? Or is there something we can do to make twine better for Android strings? |
It is still a workaround because this method adds extra "\n\n" at the end of the text, and this is problematic when using with Toast. |
That doesn't sound like a twine thing but an android SDK thing. Shouldn't it be easy to trim the string in code? |
Yes it is easy. It was just to point out the solution proposed by @jpm-polymorph was still a workaround, and didn't seem ideal in my opinion. |
@Aqua-Ye Ok, given all of this knowledge I must ask: Is there something that Twine can do here to make this better without adding Android-specific syntax rules to the Twine text file? |
@scelis Except what we said about generating different strings depending if Twine detects string format, I don't think so, and I can work with what Twine does currently :) |
👍 Thanks! Let's go with this for now and we can always revisit in the future if it becomes a bigger issue or if a cleaner solution is discovered. |
…placeholder to enable users to use the official way to retrieve these strings. Improves scelis#212.
This still doesn't solve core issues. Why are you string escaping the Why was this choice made to not follow a common rule? If you read the documentation more closely you'll see they inform you
So currently the output will not be escaped and there for the android system will strip all style information from your text. I feel that having to wrap my string with This also doesn't support iOS we get really bad results. Currently twice will stripping The current solution is wrong as we are now injecting android code into a cross project cross discipline file for no real good reason and it also breaks another project... |
Hey @ben-goulet, thanks for reaching out. We do not escape the Just to be sure we're talking about the same thing: In your Twine file you want to have If you want to retrieve static text as in
I'd really love a simpler solution! If you have an idea how to use one way to encode/retrieve strings that supports styling in layout references and code, I'm all ears. |
These styled strings shouldn't work in the layouts as the system will strip them out. You must have them string escaped for them appear properly in the layouts. With the current implementation to have this appear correctly we have to handle the strings in two different manners to get the styled strings. if we do getText() to get a charSet which will take a style tag this will return a different bold style then if you had your text using the tag. We use a custom font so if you use the bold tag it will preserve the font where a charSet will ignore the font causing rendering issues. Also wrapping it in CData becomes messy as iOS isn't parsed correctly. |
Mmhh..that's not what I'm seeing in my tests. Please see my test project. The <string name="styled">Hello <b>WORLD</b></string> is referenced in <string name="styled">Hello <b>WORLD</b></string> the string gets displayed as
Yes, that is correct. Strings (styled ones at least) that do not contain placeholders need to be retrieved with
This is weird. First, when you say 'charSet', do you mean
Totally agreed. And if it currently is, I'd consider it a bug. |
@ben-goulet, any updates? |
Why only |
Hey @doganov, very good point indeed! At the time this was implemented, these were the only supported tags: https://web.archive.org/web/20171001083841/https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling I've opened #278 - PRs are welcome ;-) |
Twine version: unknown - from
gem install
- 0.10.1 according to generated filesMinimal input file (test-twine.txt)
Exact twine command that's being run
twine generate-localization-file test-twine.txt strings.xml --lang en
Generated output
The text was updated successfully, but these errors were encountered: