Localize Android application so I can switch locale inside app
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 s开发者_开发问答et 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
.
精彩评论