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.
Related
Basically what I am trying to do is,
I start with MainActivity(activity_main) go to SettingActivity(activity_setting) & change my locale from given options(like french, english, dutch etc.)
So what I have done till now is...
OnClick of language name it re-create SettingActivity(activity-setting) & change it's string values according to language selected.
What I really want is Without re-creating Activity, All string values should be applied according to that selected language.
All suggestions & Answers are greatly appreciated.
Thank you in Advance.
Simply change the language property first.
Then call a new function that sets all strings displayed in the UI to the right language.
Pseudocode:
TextView myTextView = (TextView) findViewbyId(R.id.tv1);
myTextView.setText(yourCustomGetLocaleFunction("some id of the string you want to display", "some language name"));
But that's a bad approach. You should let Android handle the language of your app. Just localize the strings.xml file (you can load strings from the file from code).
Edit:
To change the language of your app for the moment, you can use the Code from this solution:
How to change android app language without changing phone language?
So I Have a TextView in my calculator app that I eventually display the results in it ...
It works ... but recently during the testing I found out that if The Default Language of That device is set to a non-english language (for example arabic or farsi) numbers in TextView get to be shown in that specific language (not english) and in a completely different format !!
I used this code to generate the result
result = String.format("%.4f", mResultBeforeFormatting);
resultTextView.settext(result);
also to note is that if I set the TextView with a hard coded string the issue doesn't happen
resultTextView.settext("343");
The formats used by the String.format method will by default be the formats specified by the device's default locale.
If you want to force the use of a specific locale, use the String.format method that accepts a locale parameter.
For example:
result = String.format(Locale.ENGLISH, "%.4f", mResultBeforeFormatting);
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.
I get a response from a webservice that will return several language codes (ISO 639-2) as options. I now want to translate those into a human understandable word.
For example:
eng-> English
ger -> German
fre -> French
How would you translate those words. Should I be using the strings.xml? But how will I get the Resource ID of those words?
Thanks a lot
You can convert 639-2 codes to 639-1 code using answer for this question
and after you get a 2 letter code construct Locale object and use getDisplayLanguage method
In my android app I need to know which is the language of the phone in this format :fr (for francais), en (for english),etc.. How can I do this? I know that I can display language with this :
Locale.getDefault().getDisplayLanguage()
but the result is francais,english,...
Can I obtain somehow the initials of languages?
Locale.getDefault().getLanguage()
Returns the language code for this Locale or the empty string if no language was set.
http://developer.android.com/reference/java/util/Locale.html#getLanguage()
How about using
Locale.getDefault().getLanguage();
I think you can try one of these two.
Locale.getDefault().getLanguage();
This will give language iso code i.e. "de", "ru". OR
Resources.getSystem().getConfiguration().locale;
This returns global shared Resources object that provides access to only system resources.