Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect string/user input language

Tags:

android

I wanted to know if there is a way to detect if the user's input is in greek charset.

Edit:
Just to be more clear, I need to recognize the language the user types and not the phone's locale.

For example, my phone is in English and let's say my keyboard is in Russian, the getDefault() returns "en", but I need to have "ru" at that point.


I do not know if this is available out of the box from android, maybe an approach to detect the string's character codes and see if is in English alphabet or in another. Any points on this?

I imagine something like
if character belongs to K then is English
(where K is the essemble of english characters)


Solution:

Finally I used regular expression to determine if the string is in English.

String pattern = "^[A-Za-z0-9. ]+$";
if (string.matches(pattern) 
   // is English
else
   // is not English

If someone has to propose a better solution I will mark it as answer.

like image 699
Vame Avatar asked Feb 25 '12 08:02

Vame


2 Answers

You can use following method instead of pattern matching:

boolean isEnglish = true;
for ( char c : s.toCharArray() ) {
  if ( Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN ) {
    isEnglish = false;
    break;
  }
}
like image 90
Andrey Starodubtsev Avatar answered Nov 15 '22 14:11

Andrey Starodubtsev


Locale.getDefault().getLanguage().equals("gr")

In other way:

 contains(Charset) 

EDIT:

After some more time of browsing, I have come across CharsetDetector and Character Set Detection.

Here you have method detect() but am not sure how best this can be utilized.

like image 38
Siva Charan Avatar answered Nov 15 '22 14:11

Siva Charan