How can I change language of my application?

后端 未结 2 1140
暗喜
暗喜 2020-12-05 06:28

In my application I have a option of language selection.

There are three languages: English, German & Spanish. When I select an option, the entire application la

2条回答
  •  伪装坚强ぢ
    2020-12-05 06:34

    You just add the value folder according to the language. For example, I have added 3 languages: English, Arabic and Hindi. In res folder create values-ar for arabic and values-hi for hindi to hold all strings used in application. Now I have a listview of languages. So when the user clicks on one of the language, the language of application will be changed and the phone language will remain same. Here is the code ..

      listview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                String language = ((TextView) view).getText().toString();
                if (language.equals("English")) {
                    Locale locale = new Locale("en");
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources()
                            .updateConfiguration(
                                    config,
                                    getBaseContext().getResources()
                                            .getDisplayMetrics());
                    Toast.makeText(ChangeLanguage.this, "Locale in English",
                            Toast.LENGTH_LONG).show();
    
                } else if (language.equals("Arabic")) {
    
                    Locale locale = new Locale("ar");
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources()
                            .updateConfiguration(
                                    config,
                                    getBaseContext().getResources()
                                            .getDisplayMetrics());
                    Toast.makeText(ChangeLanguage.this, "Locale in Arabic",
                            Toast.LENGTH_LONG).show();
                }else if (language.equals("Hindi")) {
    
                    Locale locale = new Locale("hi");
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources()
                            .updateConfiguration(
                                    config,
                                    getBaseContext().getResources()
                                            .getDisplayMetrics());
                    Toast.makeText(ChangeLanguage.this, "Locale in Hindi",
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(ChangeLanguage.this,
                            "Locale in not changed!", Toast.LENGTH_LONG).show();
                }
                /*
                 * Toast.makeText(getApplicationContext(), language,
                 * Toast.LENGTH_SHORT) .show();
                 */
    
                GetterSetter.getInstance().setLanguage(changelanguage);
                startActivity(new Intent(ChangeLanguage.this,
                        MainSettings.class));
                main.tabhost.setCurrentTab(3);
            }
        });
    

提交回复
热议问题