I am having troubles in displaying the correct language in the main.java. On some devices (eg. galaxy nexus) the language is correctly displayed like the /res/values-de. On some devices, eg htc legend or samsung ace, the main.java is in English, and the following pages are in German. The logging in the main.java says the locale is to "DE"...
Has anybody an idea how to solve this?
Any help would be very appreciated.
It depends on your phone language settings.
You can change language settings in code, but not forget to change to default after closing your app.
//in onCreate();
Resources standardResources = getApplicationContext();
DisplayMetrics metrics = standardResources.getDisplayMetrics();
Configuration config = new Configuration(standardResources.getConfiguration());
config.locale = new Locale("de") // change
standardResources.updateConfiguration(config, metrics);
// in onDestroy()
config.locale = Locale.getDefault(); // default
standardResources.updateConfiguration(config, metrics);
Check to see if all your country codes are correct
http://colincooper.net/blog/2011/02/17/android-supported-language-and-locales/
Related
I created multi language (English, Russian, Uzbek) app. I put 4 string resoureses in 4 folders (values, values-en, values-ru, values-uz) as docs. When I change app language updates resourses configuration in App Controller like below:
Settings.LANGUAGE = prefs.getString(User.LANG, Settings.RUSSIAN);
Locale locale = new Locale(Settings.LANGUAGE);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
After that App restarts by calling App controller's method like below:
public void reStart() {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
After them It works well almost all devises. But on Samsung Galaxy S6 (SM-G920F), it works as crazy. Some words are in english and others are in Uzbek and ets.
So, How to fix this error? isn't the concepts of "Supporting Different Languages" supported by (applicable to) all devices?
By the way, I have checked that all resources are given in corresponding languages (as shown in attached image):
From my observations, weird behaviour was affecting only Activity titles, and I found that I was setting translations of activity titles in Manifest file. Only these translations were misbehaving. All other dynamically set translations were working fine.
So, to fix the problem, I removed all activity labels from Manifest file, then set activity titles in onCreate method as below:
getSupportActionBar().setTitle(R.string.title_activity_followers);
Problem solved.
I been trying to change locale using the next code:
private void changeLocal(Locale locale){
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
refresh();
}
private void refresh() {
finish();
Intent myIntent = new Intent(this, getClass());
startActivity(myIntent);
}
It works for French but not for Hebrew. So I looked in to device supported locales by calling Locale.getAvailableLocales() and I found out that French is there but Hebrew not.
If I put hardcoded Hebrew text I can see it, so it is installed, but I do not know how to force the device to use it.
Please help me force the device using Hebrew.
P.S
Wasn't been able to make this work without the refresh even when adding
android:configChanges="locale|layoutDirection"
to my Manifest it does not call the onConfigurationChanged of my Activity
Since there are 2 distinct standards, you have to double your values folder.
Add your Hebrew strings to these folders:
values-he
and
values-iw
Some devices will use one folder. Other devices will use the other one.
Note that he is actually deprecated. So, most devices will use iw
In my app, i am asking for language, can be either English or German. The selected language and its iso code is saved into preferences. On the basis of selected language i need to change all the texts into corresponding language.
For this, i have created res/values and res/values-de; each folder containing a strings.xml file. Issues are:
1) I am opening camera as well as a screen using opengl. After navigating via both of them, the texts does not change completely into german(if was chosen). Some text values change into German, rest not even on the same page.
2) Even without going through camera and opengl screens, the results are not achieved 100% always but gives a better result always as compared to case 1.
My implementations:
1) in onResume() of splash screen, i am changing locale based on preferences with the help of config.locale().
2) in manifest file, each activity is set with activity:configChanges="locale".
3) in camera activity and opengl activity, onConfigurationChanged() is overridden in which i am again setting locale as per preferences.
please guide how to solve the locale issue.
Check Point 1:
Partial updation of resources. Make sure you have resources named correctly in all your languages.
Also, you can give this code a try. It is how i update locale of my app:
public void updateLocale(String language) {
Locale myLocale = new Locale(language);
Locale.setDefault(myLocale);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
Let me know if it helps !
I believe you need to add something to your AndroidManifest file indicating locale change. Here is an example: http://android.programmerguru.com/android-localization-at-runtime/
I had a similar problem with my app. App has force language changing option between Turkish and English. If my device language is English and if I use the app in Turkish, I can easily convert the language with the code below:
public void setAppLanguage(String languageCode) {
String countryCode;
if (languageCode.equals("tr")){
countryCode = "TR";
}else{
countryCode = "US";
}
Locale locale = new Locale(languageCode, countryCode);
Locale.setDefault(locale);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = locale;
res.updateConfiguration(conf, dm);
loginPrefsEditor.putString("uLang",languageCode);
loginPrefsEditor.apply();
}
However, when I open the camera and return from it, app language changes back to default phone language. App kills my activity when I open the camera. After return from the camera, it refreshes everything. So in order to solve this problem, I put my force change language method in onResume() method of every activity.
By doing this I would be able to solve this issue.
I would like to change my app locale programmatically. I use the following code to do the task which is in onCreate() method of Activity:
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration config= new Configuration();
config.locale = new Locale("fr"); //use French locale
res.updateConfiguration(config, dm);
//following code will navigate among fragments
...
}
My app only has one Activity which hosts several fragments.
If I run my app on Android 4.0 platform, it is working nicely, every fragment display with "French" language.
But if I run on Android 2.2 platform, only the first fragment display in French language, the next replaced fragment still show English (my phone setting use English locale).
Why it works only on Android 4.0 platform ??
It looks like some bug in Android 2.x
Because if you change locale not in onCreate() but in createMenu() or inside some spinner.setOnItemSelectedListener() it will work.
It is workaround but try to call some event after start your app and change locale in it.
How do I localize application so it uses specific locale regardless of what locale set on device? I want make it possible for users to set language of their choice.
So far I have code like this in my Application class:
#Override
public void onCreate()
{
//Set locale
String l = Preferences.getLocale(getApplicationContext());
if (!l.equals(""))
{
Locale locale = new Locale(l);
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(
config, getBaseContext().getResources().getDisplayMetrics());
}
LogData.InsertMessage(getApplicationContext(), "Application started");
}
Problem that I have is that it seems like I display in set locale just fine (TextViews)
But Menu captions and toasts will fall to default locale.
Is there any 1-2-3 on how to get it working properly? I uses 2.2 version
This post explains how to force localization in your app.
Ok, I figured why I had this problem.. I needed to override onConfigurationChanged in my application class. That is much more elegant solution than to specify locale on each Activity.