Switch language inside android application

How to implement language switching without having to manually set the locale inside an Android application? I know that the application will load strings.xml by language at startup, but I do not want this choice to be made based on the locale of the system, but instead was specified in the settings.

Or, manually configures the locale?

+8
source share
2 answers

You can extend the Application class (you should also declare it in the manifest) and put something like this in it.

Whenever you want to change the language, you can call

((App)getApplicationContext()).changeLang(lang); 

from your activity. R.string.locale_lang is just the key that is stored in the strings.xml file for general settings

 public class App extends Application { private Locale locale = null; @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (locale != null) { Locale.setDefault(locale); Configuration config = new Configuration(newConfig); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); } } @Override public void onCreate() { super.onCreate(); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); String lang = settings.getString(getString(R.string.locale_lang), ""); changeLang(lang); } public void changeLang(String lang) { Configuration config = getBaseContext().getResources().getConfiguration(); if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) { Editor ed = PreferenceManager.getDefaultSharedPreferences(this).edit(); ed.putString(getString(R.string.locale_lang), lang); ed.commit(); locale = new Locale(lang); Locale.setDefault(locale); Configuration conf = new Configuration(config); conf.locale = locale; getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics()); } } public String getLang(){ return PreferenceManager.getDefaultSharedPreferences(this).getString(this.getString(R.string.locale_lang), ""); } } 
+18
source

Good. Let them make the easy way. In the Layout folder, make 2 copies of the XML layouts. Name one of them main.xml (your local language), and the other mainen.xml for English. In the values ​​folder, the strings.xml file also contains two lines of text for both languages: <string name="hello">Tere, Maailm!</string> for the local language and <string name="hello_en">Hello World!</string> for English. Returning to the xml layouts, main.xml contains android:text="@string/hello" for your text, and the second duplicate mainen.xml contains all the same layouts and lines with a slight difference when receiving the English version of a line of text from the strings.xml file : android:text="@string/hello_en" . And programmatically, to set the title if necessary and indicate at the beginning of each action which layout to choose, use the languageToLoad global variable that was declared and created in your first (initial) class: protected static boolean languageToLoad = true; , in the same class, the onCreate method should contain several switches (you also need to define and name them in the corresponding xml format): '

  // ... View radio1 = findViewById(R.id.Et); radio1.setOnClickListener(this); View radio2 = findViewById(R.id.En); radio2.setOnClickListener(this); // ...' 

And then in the class:

  // ... public void onClick(View v1) { switch (v1.getId()) { case R.id.Et: languageToLoad = true; break; case R.id.En: languageToLoad = false; break; // ...' 

And later in the program, in the onCreate method of your various actions:

  //... public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (YourSuperClass.languageToLoad) { setContentView(R.layout.youractivity); // estonian setTitle(R.string.youractivity_title); } else { setContentView(R.layout.youractivityen); // english setTitle(R.string.youractivity_title_en); } Intent i = getIntent(); //...' 
0
source

Source: https://habr.com/ru/post/954504/


All Articles