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 TextView
after 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...
However, note that in a Java program, a double quotation mark requires the backslash escape character. Example: -- a single quotation mark is the escape character -- for a single quotation mark VALUES 'Joe''s umbrella' -- in ij, you don't need to escape the double quotation marks VALUES 'He said, "hello!"' n = stmt.
For instance, in Java a double-quote is a string while a single-quote is a character. Defining char letter = "a" in Java will cause an error in the same way as String s = 'a bunch of letters' will. Always better to use double-quotes when storing strings.
You can print double quotes in java by escape double quotes using backslash character( \ ). When you print on console using System. out. println, you use double quotes and whatever value inside double quotes is printed.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With