6

I asked a similar question earlier for Swift, and I'm now facing the same problem in Android/Java.

Is there a way in Java for adding quotation marks to a String? The quotation marks should localize properly (see https://en.wikipedia.org/wiki/Quotation_mark) based on the user's language settings. The text is stored without quotes in a database. I'd like to show the string in a TextViewafter adding the quotes.

For instance:

String quote = "To be or not to be..."
// one or more lines of code that add localized quotation marks
// to the beginning and the end of the string
mMyTextView.setText(stringWithQuotes);

For a French user: «To be or not to be...»

For a German user: „To be or not to be...“

For an English (US) user: “To be or not to be...”

Community
  • 1
  • 1
  • 2
    @Reimeus That doesn't set the various types of quotes that the OP is discussing. Just the English quotes. – Susannah Potts Aug 17 '16 at 19:47
  • 6
    http://stackoverflow.com/questions/17433667/java-api-to-get-the-typographically-correct-quotation-marks-for-a-specific-local is the Java answer. However, `LocaleData` does not appear to be in the subset of ICU4J integrated into Android 7.0, and I don't know if ICU4J itself can work on Android. – CommonsWare Aug 17 '16 at 19:51
  • @CommonsWare Interesting, thanks. I'll check if I can use it. –  Aug 17 '16 at 19:56
  • Quickly scanned some info about ICU4J. Does not look so straightforward to use this on Android if at all possible. Might be easier to just handle it with a few `if` statements, especially if the number of supported locales is rather small. Thanks anyway for the pointer. –  Aug 17 '16 at 20:17
  • 1
    @SusannahPotts yes. The comment you replied to was removed before I could see it but I imagine it was just escaping "engineering quotes". It should perhaps be mentioned here that even in English language they are typographically incorrect. I corrected the question accordingly. –  Aug 20 '16 at 12:11

3 Answers3

0

I've not tried this, but you may be able to use Locale.getDefault. You could create an enum for each country with different quotations and supply that in the enum. Then it would just be a matter of finding the locale that matches an enum and substituting in the specified locale's quotation mark character.

Jacob
  • 91
  • 1
  • 9
  • Accepted this one because it recognizes that there seems to be no built-in support for this, so it has to be done "manually". It's a bit fuzzy with respect to the actual implementation, and there are many ways to do it. I posted another answer with a simple solution that I ended up using. –  Aug 20 '16 at 10:29
0

I'd recommend using this Stack Overflow answer to create the text in HTML which has the necessary mechanisms for handling the quotation marks across locales.

Then you can use this Stack Overflow answer to convert the HTML for display in the TextView.

Community
  • 1
  • 1
Scott Shipp
  • 2,241
  • 1
  • 13
  • 20
  • Thanks - nice trick. I did a quick test adding `textView.setText(Html.fromHtml("text"));`to my code but the quotes don't show. Googled it, and it seems that the `` tag is not supported by `TextView`. –  Aug 17 '16 at 21:22
  • On the other hand, this works: `setText(Html.fromHtml("«text»"))`. But it also requires some kind of lookup table to match locale and symbols. Can be achieved without HTML by `setText("\u00abtext\u00bb")`. –  Aug 18 '16 at 07:17
  • Ah that's a bummer about unsupported q tag. I didn't know that. I wonder if the same CSS could be used with another HTML tag it does support though? Maybe not . . . Android may not actually support that either. – Scott Shipp Aug 18 '16 at 15:14
0

I ended up using the following solution:

In order to keep the different types of quotes in sync with the locales supported by my app I create translatable string resources for start and end marks, e.g.

<string name="left_quote">\u00ab</string>
<string name="right_quote">\u00bb</string>

for French quotes and then add them to my string in code. Pretty simple and easy to maintain.