Localize Android application so I can switch locale inside app - android

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.

Related

RTL is not working properly

I am currently working on an Android app that needs to support languages English and Arabic, i am using this code to switch from a language to the other.
String languageToLoad; // your language
if (languageSwitch.isChecked()) {
languageToLoad = "ar";//arabic
} else {
languageToLoad = "en";//english
}
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
// store it in the cache for any further use
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
SharedPreferencesUtil.putString(this, "language", languageToLoad);
It was working fine, i've made 2 layouts folders, a normal one and a layout-ar. It was working correctly but sometimes it gets messed up and instead of showing the Arabic one, it shows the English layout and the app continues on running in the English mode only.
At the end you should restart you activity. Try this.
public void updateActivity() {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
It turns out that the webview is the one responsible for the problem, once the webview is generated it deletes all the overrided local data and inserts the one from the device,in order to correct the problem you need to follow the steps found in this link.

android - admob reset Locale to default values

In my app I set locale this way:
public void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
I call this method in Activity. All work fine until I put admob banner after that all resources change to default.
I found similar issue here Admob reset app locale to system default but that solution didn't help me. What should I do to solve the problem ?
One solution that I have found is to set all resources in code depends on selected locale but it's too long for me and not convenient if you have more than two language.
Or if someone has a multi-language app with admob banner, please give me an example how do you implement localization.
helps for me:
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
setPref();
}
setPref() is method for set locale

Android: Change to unsupported locale

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

How to change language setting of the whole system in android without Intent?

I'm trying to change the language of the whole system of Android Phone on my application, cause our goal is to customized a Settings application.
I've tried this, but didn't work:
Configuration conf = Resources.getSystem().getConfiguration();
conf.locale = toSet; //toSet is a Locale which I want to set to the system
conf.setToDefaults();
the other try didn't work either:
Locale.setDefault(toSet)
There are ways to change the language of application, but not systems.
Is there any ways that I can reach the goal?
Will getting the root permissions works for me?
Or is there any ways to modify the UI interface of the Intent-Activity?
I've found that, in the source code, "platform/packages/apps/Settings/src/com/android/settings/LocalePicker.java", "com.android.internal.app.LocalePicker" might help, and in the "platform/frameworks/base/core/java/com/android/internal/app", there is a function which named "updateLocale", I guess it might help, but the class such as "IActivityManager" cause compile error in the Eclipse.
Does anyone have any ideas? Thanks!
try this
String languageToLoad = "zh";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);
AndroidManifest:
add android:configChanges="locale"

changing locale in app on Android 2.2 platform (on Android 4.0 is ok) in my case

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.

Categories

Resources