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
Related
May i know what is the difference between Locale.getDefault().getLanguage() and mContext.getResources().getConfiguration().locale.getLanguage()
Sorry for my draft but i need a detailed distinction between them.
Locale.getDefault().getLanguage();
Returns the language name for this Locale or the empty string if no
language was set, though document states that getLanguage() returns language code but it doesn't.
getResources().getConfiguration().locale.getLanguage()
Returns the Language code.
I am trying to get the current device locale with the region like "en_us","en_gb".
I am calling Locale.getDefault().getLanguage() and it returns only the two letters code en.
Format like "en_us" or "en_gb" has "language code"_"country code"
A Locale object contains both country code and language code.
So you can use below snippet to format your own code..
String cCode = Locale.getDefault().getCountry();
String lCode = Locale.getDefault().getLanguage();
String code = lCode+"_"+cCode;
or
you can use toString() method on Locale object to get the data
String code = Locale.getDefault().toString();
The default Locale is constructed statically at runtime for your application process from the system property settings, so it will represent the Locale selected on that device when the application was launched. Typically, this is fine, but it does mean that if the user changes their Locale in settings after your application process is running, the value of getDefaultLocale() probably will not be immediately updated.
If you need to trap events like this for some reason in your application, you might instead try obtaining the Locale available from the resource Configuration object, i.e.
Locale current = getResources().getConfiguration().locale;
You may find that this value is updated more quickly after a settings change if that is necessary for your application.
i have tested this :)
i have got from this as link may be deleted so Answer copied :)
It's worth pointing out that locale codes and language tags are related but different. Locale codes have an underscore separator (e.g. fr_CA), and language tags have a dash separator (e.g. fr-ca). I'm sure there are some deeper differences but that's beyond my pay grade.
This answer gives the result of various methods on the Locale class: https://stackoverflow.com/a/23168383
It looks like you want the toString() method (to get the locale code) or the toLanguageTag() (to get the language tag).
Use
Locale.getDisplayName();
This is shorthand for
Locale.getDisplayName(Locale.getDefault());
The documentation is in here:http://developer.android.com/reference/java/util/Locale.html
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 am making an android app in which i want to change the language of app on selecting a particular language in spinner without changing the language of device.I have made different string files for all language. Now what to do next??
Can anyone please help me over this?
thanks
Most simple way is just to change VM's locale, like:
Locale locale=new Locale("zh"); //Chinese
Locale.setDefault(locale); //set VM's default locale
Once I wanted to implement a multiple language feature for my application. Where that language was not even supported by android.
I made the appropriate strings for all the application and stored them in my resources.
Then I kept the language selection in my shared preferences, so that when user opens the app the next time, we can show the previously selected language. I implemented the code and language changing in my OnResume() function of Activity. the code was like:
if(SelectedLanguage.compareTo("ar")==0)
{
String text = getString(R.string.ar_Options);
tv_Options.setText(ArabicUtilities.reshape(text));
text = getString(R.string.ar_Minimize);
tv_Minimize.setText(ArabicUtilities.reshape(text));
}
else
{
String text = getString(R.string.en_Options);
tv_Options.setText(text);
text = getString(R.string.en_Minimize);
tv_Minimize.setText(text);
}
I hope you get the basic idea. In this way you don't have to change the lanuguage of your device and you can provide multiple languages for your application.
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.