Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a charset of a string is the same as the charset of the device's set language [duplicate]

Tags:

java

android

I want to detect if a string contains characters that are not in the device's language characters

Is it possible?

Some of my app users write in arabic, the rest write in english. I need to translate text only when the text is in arabic and the user's device is in english or the other way around

like image 586
code511788465541441 Avatar asked Nov 01 '22 08:11

code511788465541441


1 Answers

You can get the device language by

Locale.getDefault().getDisplayLanguage();

And then do a checking on the input string to see if any character in the string is in the range between \u0600 and \u06FF (Arabic charset in Unicode), then it should do the trick

Here is the answer of how to check if the string is in a specific charset

public boolean isEncoded(String text){
    Charset charset = Charset.forName("US-ASCII");
    String checked=new String(text.getBytes(charset),charset);
    return !checked.equals(text);
}
like image 65
albusshin Avatar answered Nov 14 '22 02:11

albusshin