Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load alternative strings.xml from resources programmatically

I want to provide three different langauges. The default language is english so all my string values in the values folder are in enlish. I created two other folders

  • values-de for the german language
  • values-cn for the chinese language

each folder contains a strings.xml file where i defined the values in german for the de folder and in chinese for the cn folder.

My question now is: How can i load a different language programmatically because i want to provide buttons in my app interface where the user can switch the language. The settings of the device wont be editable for our users. Our users can just see the app itself and nothing else so i have to provide the language switching from within my application.

like image 660
Mulgard Avatar asked Jan 20 '26 14:01

Mulgard


1 Answers

You can change the configuration at runtime

Do this in onCreate of the activity

String languageToUse  = "de"; // The language you want to change
Locale locale = new Locale(languageToUse); 
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, 
      getBaseContext().getResources().getDisplayMetrics());
like image 168
Rohit5k2 Avatar answered Jan 22 '26 02:01

Rohit5k2