How to restore an Android app's default locale

前端 未结 2 1025
北海茫月
北海茫月 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:54
    Locale defaultLocale = Locale.getDefault();
    

    Edit: Sorry, I just realized this does not in fact answer your question, since you're overriding the default to effect your change.

    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题