我正在编写一个具有选项菜单的应用程序,其中包含三个语言选项:自动、英语、希腊语。
当用户更改设置时,我将其保存在共享首选项中,并使用新任务标志调用我的开始活动
当该活动开始时(甚至在 setContentView 之前)我调用以下方法:
string language = MainApplication.Language; //this is from shared preferences
if (language == null || language.Equals("auto")) {
language = Java.Util.Locale.Default.Language; //if language is not set, or the setting is auto then load the default language
MainApplication.Language = "auto"; //save into the shared preferences that the language was set to auto
}
App.instance.set_language(language); //update the data layer's language
Java.Util.Locale locale = new Java.Util.Locale(language);
Java.Util.Locale.Default = locale; //change the default locale
Configuration config = new Configuration();//create a new configuration and set its locale
config.Locale = locale;
Application.Context.Resources.UpdateConfiguration(config, null);//update the system locale
Run Code Online (Sandbox Code Playgroud)
上面是用 Xamarin 写的,所以它是 C# 但它是相同的 Java 代码
我的问题是,如果我将语言更改为希腊语,例如,当上面的代码运行时,它会将默认语言环境设置为 "el" ,但是如果用户在此之后选择自动选项,因为应用程序的默认语言环境是 "el "它保留了该设置,即使我的系统语言是英语。
当我重新启动应用程序时,它可以正常工作,因为应用程序的默认语言环境是系统的语言环境
所以我的问题是,有没有办法获得系统的语言环境而不是应用程序?
ps我知道我总是可以将系统的语言环境存储在 Application 中并在 OnConfigurationChanged 中更改它,如果有另一种方式,我只是在徘徊
你可以通过 adb shell getprop | grep locale
它将打印类似的输出,如:
[persist.sys.locale]: [tr-TR]
[ro.product.locale]: [tr-TR]
Run Code Online (Sandbox Code Playgroud)
所以你可以运行一个进程:
Process process = Runtime.getRunTime().exec("getprop");
Run Code Online (Sandbox Code Playgroud)
并使用 . 检查输出字符串BufferedReader。
或者基本上检查这个答案。
| 归档时间: |
|
| 查看次数: |
2168 次 |
| 最近记录: |