Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

android - How to programmatically change language?

I want to programmatically change the language.

So I have built two xml files.

values-it
-->string.xml

values-en
-->string.xml

This is the code in MainActivity to change the language of the whole application:

//ITALIAN

Resources res = getApplicationContext().getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("it");
res.updateConfiguration(conf, dm);

//ENGLISH

Resources res2 = getApplicationContext().getResources();
DisplayMetrics dm2 = res2.getDisplayMetrics();
android.content.res.Configuration conf2 = res2.getConfiguration();
conf2.locale = new Locale("en");
res2.updateConfiguration(conf2, dm2);

Now if I set the English language (for example) the code is executed with no error, but the label doesn't not change its text.
If I change the orientation of my device, the label changes its text correctly.

Now how can I modify my code to automatically refresh the label?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
 AlertDialog.Builder builder = new AlertDialog.Builder(DashboardActivity.this);
            builder.setTitle(R.string.languages);
            // Add the buttons
            builder.setPositiveButton(R.string.english, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    String languageToLoad = "en"; // your language
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config,
                            getBaseContext().getResources().getDisplayMetrics());
                    dialog.dismiss();
                    rEditor.putString("language", languageToLoad);
                    rEditor.commit();



                    Intent refresh = new Intent(DashboardActivity.this, DashboardActivity.class);
                    startActivity(refresh);
                    finish();

                }
            });
            builder.setNegativeButton(R.string.gujarati, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog

                    String languageToLoad = "gu"; // your language
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config,
                            getBaseContext().getResources().getDisplayMetrics());
                    dialog.dismiss();
                    rEditor.putString("language", languageToLoad);
                    rEditor.commit();


                    Intent refresh = new Intent(DashboardActivity.this, DashboardActivity.class);
                    startActivity(refresh);
                    finish();

                }
            });

            builder.create().show();

you have to reload activity to show new language text means restart.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...