How to restore an Android app's default locale

前端 未结 2 1031
北海茫月
北海茫月 2021-02-05 17:22

I have this Android app that\'s localized in several languages. If the appropriate string for the device\'s locale exists, the app automatically displays it. So far, so good.

2条回答
  •  一个人的身影
    2021-02-05 17:55

    I resolved this issue by storing the existing setting on first run of the app (before any changes are made to the default locale). In a block that only runs the first time my app runs, I call:

    m_preferences.edit().putString(
                            MyConstants.PREFERENCE_SYSTEM_LANGUAGE,
                            getResources().getConfiguration().locale.getLanguage()
                            ).commit();
    

    Later, when I need to retrieve it:

    languageToLoad = m_preferences.getString(
                            MyConstants.PREFERENCE_SYSTEM_LANGUAGE,
                            MyConstants.LANGUAGE_DEFAULT);
    

    When changing the language, I save that in another preference and handle the updates to the view.

    Edit: After poking around a bit more, I found the system setting. I switched to using this, rather than the above code.

    Resources.getSystem().getConfiguration().locale.getLanguage();
    

提交回复
热议问题