开发者

How can I change language of my application?

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开发者_如何学C language should be changed.

How can I make this possible?


Do you mean that you want to use another language than the default language in the phone? I have that in one application, and this is what I had to do.

Add this to your activity declaration in the AndroidManifest.xml

<activity
    android:name=".ui.SomeActivity"
    android:configChanges="locale"
    :
    :
</activity>

And then invoke a method like this from onCreate in your activity:

public static void setLanguage(Context context, String languageToLoad) {
    Log.d(TAG, "setting language");
    Locale locale = new Locale(languageToLoad); //e.g "sv"
    Locale systemLocale = SystemLocale.getInstance().getCurrentLocale(context);
    if (systemLocale != null && systemLocale.equals(locale)) {
       Log.d(TAG, "Already correct language set");
       return;
    }
    Locale.setDefault(locale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = locale;
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    Log.d(TAG, "Language set");
}


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);
        }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜