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
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 want to convert language codes to their display forms (with their native letters). For example:
Need to convert these language codes :en, ru, it, ja
Result should be this list:
English, Русский, Italiano, 日本語
How to do that?
The Locale class should do what you want.
getCountry() will give you a country code, for which you can obtain the name via getDisplayName().
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 can easily set a Currency using it's ISO 4217 code, such as "USD". This will allow me to grab the dollar symbol using .getSymbol(), however, how the heck do I get the display name "dollar" out of the Currency?
I have a spinner, I'd like to populate with the names of currencies like "dollar", "euro", "yen", etc. however, I can't extract these from the ISO 4217 codes. Shouldn't this be quite easy?
Please note, I can't use simple string arrays to solve this. That's because I grab the default locale and add it's currency to the spinner mentioned above. This is the reason I need to be able to grab currency display names dynamically, I don't know all the currencies users may use.
Thanks!
Refer to this question.
Can I get a text description of an ISO currency code in Java?
It might answer your question. There are many answers on that page.
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.