如何恢复一个Android应用程序的默认语言环境应用程序、语言、环境、Android

2023-09-04 13:08:05 作者:成全比深爱更需要勇气

我有这样的Andr​​oid应用程序,本地化的几种语言。如果相应的字符串为设备的区域设置存在,该应用程序会自动显示。到目前为止,一切都很好。

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.

接下来,我想允许应用程序的用户切换到我提供的语言之一。我可以通过覆盖默认区域做到这一点。没有问题就在这里,无论是。

Next, I want to allow the app's users to switch to one of the languages I've provided. I can do this by overriding the default locale. No problem here, either.

下面的问题:有没有办法为我提供的语言列表,让用户恢复默认值,自动语言环境的缺省的选项?考虑以下情形: 1.设备的区域设置为德语。应用德语显示的一切。 2.用户希望的应用程序是在法国,所以他们改变从应用程序内部的语言,和应用程序变成法国的结果。 3.同一用户更改设备的区域设置为西班牙语。应用程序仍然是法国人。 4.他们选择了默认设置。该应用程序更改为西班牙语,并保持在区域设置被改变改变。

The question here: Is there a way for me to provide a "default" option in the language list that would allow users to restore the default, automatic locale? Consider the following scenario: 1. Device's locale is set to German. App displays everything in German. 2. User wants the app to be in French, so they change the language from inside the app, and the app turns French as a result. 3. The same user changes the device's locale to Spanish. App is still French. 4. They select the "Default" setting. The app changes to Spanish, and keeps changing when the locale is changed.

如果这不是自动可行的,那么有没有办法,我让设备范围内的位置,这样如果选择我的默认选项来设置我的应用程序呢?

If this isn't doable automatically, then is there a way for me to get the device-wide locale so that I can set my app to it if my "default" option is selected?

推荐答案

我通过存储上的应用程序(之前的任何更改了默认的语言环境)的第一次运行的现有设置解决了这个问题。如果只运行在第一时间我的应用程序运行在块,我呼吁:

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

当更改语言,我保存在另一个preference和处理的更新视图。

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

修改:张望了一下戳多后,我发现系统设置。我切换到使用这个,而不是上面的code。

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();