开发者

Localize Android application so I can switch locale inside app

开发者 https://www.devze.com 2023-03-09 14:39 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消