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
249 views
in Technique[技术] by (71.8m points)

locale - How to change language for a whole android application?

I want to write a application that can display either english or chinese. I have already prepared 2 string.xml which is value/strings.xml and value-zh-rHK/strings.xml. But I have no idea how to change the language via ListPreference of android.

xml/preferences.xml:

<?xml version="1.0" encoding="utf-8"?>

<SwitchPreference
    android:key="pref_nightmode"
    android:title="@string/nightmode"
    android:defaultValue="false">
</SwitchPreference>

<ListPreference
    android:key="pref_lang"
    android:title="@string/lang"
    android:dialogTitle="Choose Language"
    android:entries="@array/lang"
    android:entryValues="@array/lang_value"
    android:defaultValue="@string/lang_default">
</ListPreference>

and the Preferences.java

public class Preferences extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(drawer_menu[5]);
    getFragmentManager().beginTransaction().replace(R.id.content_frame, new MainPreferenceFragment()).commit();
}

public static class MainPreferenceFragment extends PreferenceFragment {
    String locale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        PreferenceManager pm = getPreferenceManager();
        ListPreference lang = (ListPreference) pm.findPreference("pref_lang");
        if(lang.getValue().equals("English")) {
            locale = "en_US";
        } else {
            locale = "zh_HK";
        }
    }
}

The activity extends BaseActivity because I have a drawer menu right there.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You can change language settings for app only. Use Locale class and update default configuration. Language change only applied when activity restart or re-start application. Use following locale class

public class LocaleUtils {

    private static Locale sLocale;

    public static void setLocale(Locale locale) {
        sLocale = locale;
        if(sLocale != null) {
            Locale.setDefault(sLocale);
        }
    }

    public static void updateConfig(ContextThemeWrapper wrapper) {
        if(sLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Configuration configuration = new Configuration();
            configuration.setLocale(sLocale);
            wrapper.applyOverrideConfiguration(configuration);
        }
    }

    public static void updateConfig(Application app, Configuration configuration) {
        if(sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //Wrapping the configuration to avoid Activity endless loop
            Configuration config = new Configuration(configuration);
            config.locale = sLocale;
            Resources res = app.getBaseContext().getResources();
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
    }
}

Application class :

public class App extends Application {
    public void onCreate(){
        super.onCreate();
        // get user preferred language set locale accordingly new locale(language,country)
        LocaleUtils.setLocale(new Locale("iw"));
        LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LocaleUtils.updateConfig(this, newConfig);
    }
}

BaseActivity :

public class BaseActivity extends Activity {
    public BaseActivity() {
        LocaleUtils.updateConfig(this);
    }
}

Above answer is taken from : Changing Locale within the app itself

Android Document : http://developer.android.com/reference/java/util/Locale.html


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

...