Change app language programmatically in Android

2018-12-31 00:49发布

Is it possible to change the language of an app programmatically while still using Android resources?

If not, is it possible to request a resource in an specific language?

I would like to let the user change the language of the app from the app.

30条回答
浪荡孟婆
2楼-- · 2018-12-31 01:08

There are some steps that you should implement

First, you need to change the locale of your configuration

Resources resources = context.getResources();

Configuration configuration = resources.getConfiguration();
configuration.locale = new Locale(language);

resources.updateConfiguration(configuration, resources.getDisplayMetrics());

Second, if you want your changes to apply directly to the layout that is visible, you either can update the views directly or you can just call activity.recreate() to restart the current activity.

And also you have to persist your changes because after user closes your application then you would lose the language change.

I explained more detailed solution on my blog post Change Language Programmatically in Android

Basically, you just call LocaleHelper.onCreate() on your application class and if you want to change locale on the fly you can call LocaleHelper.setLocale()

查看更多
只靠听说
3楼-- · 2018-12-31 01:09

Just adding an extra piece that tripped me up.

While the other answers work fine with "de" for example

String lang = "de";
Locale locale = new Locale(lang); 
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, 
    getBaseContext().getResources().getDisplayMetrics());

The above wont work with for example "fr_BE" locale so it would use the values-fr-rBE folder or similar.

Needs the following slight change to work with "fr_BE"

String lang = "fr";

//create a string for country
String country = "BE";
//use constructor with country
Locale locale = new Locale(lang, country);

Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, 
    getBaseContext().getResources().getDisplayMetrics());
查看更多
姐姐魅力值爆表
4楼-- · 2018-12-31 01:11

First you create directory name values-"Language name" like hindi than write "hi" and same string file name copy in this directory and change value do not change parameter after set below code in your action like button etc....

Locale myLocale = new Locale("hi");
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(Home.this, Home.class);
startActivity(refresh);
finish(); 
查看更多
时光乱了年华
5楼-- · 2018-12-31 01:12

Just handle in method

@Override public void onConfigurationChanged(android.content.res.Configuration newConfig).

Follow the Link

I think it is useful

查看更多
柔情千种
6楼-- · 2018-12-31 01:13

At first create multi string.xml for different languages; then use this block of code in onCreate() method:

super.onCreate(savedInstanceState);
String languageToLoad  = "fr"; // change your language here
Locale locale = new Locale(languageToLoad); 
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, 
  getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.main);
查看更多
荒废的爱情
7楼-- · 2018-12-31 01:14

In example we set English language:

 Configuration config = GetBaseContext().getResources().getConfiguration();
 Locale locale = new Locale("en");
 Locale.setDefault(locale);
 config.locale = locale;
 GetBaseContext().getResources().updateConfiguration(config, 
            GetBaseContext().getResources().getDisplayMetrics());

Please, remember this works only if language is found in Device system also, not only in application

查看更多
登录 后发表回答