How to change the application language by user cho

2020-06-30 03:57发布

In my project there is a option for the user to select language. The whole application should change by selecting the language. Language is fetched from the server side.

I referred various sites and links, but couldn't find a better solution. Localization is not possible, because it's a huge app and also language is not fixed , it is fetched from the server side and it can be varied.

Is any other solution is available? Please help...

标签: android lang
2条回答
看我几分像从前
2楼-- · 2020-06-30 04:25

Although its not recommended to use separate language for your app other than the Android system's . But you can still change it .

Below is the code :

private void setLocale (String localeCode , Bundle b ){
    Log.d(TAG+"set location function: "+localeCode);
    locale = new Locale(localeCode);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    UserDetail.this.getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    onCreate(null);
}

Use this method call on some user trigger:

setLocale("en-us",savedInstanceStat); // for english
setLocale("ar",savedInstanceStat); // for arabic

To learn more about android locals: http://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/

查看更多
可以哭但决不认输i
3楼-- · 2020-06-30 04:27

You can use something like this:

Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);

This will set the locale of the app to the desired one, not changing the global locale set on the device. All of the native localisation mechanisms will work with the context locale.

查看更多
登录 后发表回答