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.
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;
}
}
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.
As Siva suggests, you can check the user's locale.
In Android, this can be done by using Locale.getDefault(). Although I wouldn't strictly compare it to a 2-letter code, current Android implementation has it being a 2-letter language code, an underscore, and a two-letter country code. Ie., de_US would be German as spoken in the United States.
This is not the way the industry is moving, but its the best-supported pattern as of Java 6. Java 7, once supported by Android should support ISO 639 alpha-3 codes that are more future-proof.
Related
I'm trying to get user language with two-letters but no success until here.
Use Locale.getDefault().getISO3Language(); returns code with three digits (eng, por, jap).
Use substring method to cut does not work for all codes, so what's the solution?
Instead of getISO3Language(), use getLanguage() and you will get user language like en, ja, pt etc.
Locale.getDefault().getLanguage();
Checkout official documentation for more information about getLanguage()
I have a piece of code where I'm trying to change the language in my app, using a Spinner View Component. While working with this, I've come to realize that I'm probably not using the most smooth method, but more intrestingly, I've noticed that two strings that LOOK the same are not equal when compared. Why is it like this, and how should I do if I want to "check" if the language is a certain one?
if (myLocale.toLanguageTag()=="sv")
{
//Changing language from Swedish to English
}
else
{
Toast.makeText(parent.getContext(),
myLocale.toString(), Toast.LENGTH_SHORT).show();
//Here, the toast will tell me what myLocale.toString is "sv",
//and so is myLocale.toLanguageTag(). Yet they are not equal...
}
As stated in the documentation:
Locale.toString() returns a string representation of this Locale object, consisting of language, country, variant, script, and extensions, whatever is available, as below:
language + "_" + country + "_" + (variant + "_#" | "#") + script + "-" + extensions
Language is always lower case, country is always upper case, script is always title case, and extensions are always lower case.
for example en_US, en
Whereas Locale.toLanguageTag() returns you the same stuff (language, country, and variant), but as a tag. Here Tag means some code given for language, country and variant defined by some IETF's BCP 47 standard (BCP = 'Best Current Practice').
for example en-US
The only difference I can see is the _ and - or perhaps some language/country codes too.
In nutshell, both of them return String; one returns a normal string representation of the Locale whereas the later returns a string as a well-formed IETF BCP 47 language tag representing the locale.
The documentation also suggests using toString only in debugging mode:
This behavior is designed to support debugging and to be compatible with previous uses of toString that expected language, country, and variant fields only. To represent a Locale as a String for interchange purposes, use toLanguageTag().
Hope it helps.
Use .equals() for strings comparison.
A good explanation can be found here
if ("sv".equals(myLocale.toLanguageTag()))
I am trying to create multilangualge app and faced with a problem! I have a string in values\strings.xml translated in German language values-de\strings.xml. I am trying to compare user input with those strings. If my input is in English and device's language is also English, everything works fine, but if I switch device's language to German and input a string in English, contains() and equals() methods will return false. Is there a way to compare strings in different languages? Thanks in advance! Also, sorry for my English!
if (mystring.contains(context.getResources().getString(R.string.testString))) {
check = true;
}
if (mystring.equals(context.getResources().getString(R.string.testString))) {
check = true;
}
In android when you call getResources() it will always get the resources of the default locale, to get one from other locales you must specify explicitly which locale you want to use, and you can find how to do it here :
https://stackoverflow.com/a/33629163/6171845
I'm using 3 language(English, Korean, Japanese) in my app.
Is it possible to distinguish String the language in EditText?
A String or EditText doesn't contain information about the language, therefore you need a workaround.
Options:
Check the keyboard language.
Scan the String for code points in a specific range to identify Asian languages. It won't work if they share the same character set, for example english and german.
Implement your own solution.
I have the following idea:
In German we have four extra letters (ä, ö, ü, ß) and I don't know any other language which has these vocals but I think French people with their accents also know this problem. We have a lot of apps in the Google Play store for cities, bus stations, trains and other stuff like that. Now it is really exhausting that we always have to write these letters if we are on the go. It would be much easier to write Munchen (=München [de] = Munich [en]), Osterreich (Österreich [de] = Austria [en]) or something like Uberwasserstrasse (Überwasserstraße [de] = Over-Water-Street [en]). So my question is now:
A lot of apps show suggestions for our just typed word. I think in the code it is something like this:
String current = editText.getText().toString();
db.lookUp(current); // Of course SQL statement
Can we hook this so that Android thinks that we have typed an ä, ö, ü, ß if we write an a, o, u, ss and the system looks for words with one of these vowels and suggests both? Here I do not want to ask for code - I want to discuss if we are able to write a hack or hook for the Android system. Also, root-rights can be assumed with the solution. I'm looking forward to your ideas.
You could do this the other way around, by "normalizing" typed characters into their related non-diacritical versions. You can use the java.Text.Normalizer class for this. A good snippet can be found in this article:
public static String removeAccents(String text) {
return text == null ? null :
Normalizer.normalize(text, Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
When applied to "Münich", this returns "Munich". That way, you can use a simple string comparison using these normalized versions.
This wouldn't work for "ß" though. If that's the only special case, you could handle it separately.
What you are looking for is called accent-insensitive collating sequence. SQLite's COLLATE operator can be used to do such searches, but I learned from another post that there might be bugs you'll need to look out for.