【问题标题】:How to know my location and which the current string file is using - Android如何知道我的位置以及当前使用的字符串文件 - Android
【发布时间】:2016-10-23 17:13:42
【问题描述】:

我的 android 应用程序中有 2 个字符串文件,支持 2 种语言,PTEN。但是我需要知道哪个是正在使用的字符串文件,因为我需要在我的 SQLite 数据库中添加当前使用的语言。

实际上,我正在使用此代码来检测我的 SQLite 数据库中的当前语言,但此功能仅在用户在配置屏幕中手动更改语言时才有效。因为我不知道如何在用户第一次打开应用程序时选择第一种语言。

if(!dbl.selectIni().getCurrent_lang().equalsIgnoreCase("system")){
  String languageToLoad  = dbl.selectIni().getCurrent_lang();
  Locale locale = new Locale(languageToLoad);
  Locale.setDefault(locale);
  Configuration config = new Configuration();
  config.locale = locale; 
  getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}

【问题讨论】:

  • Locale.getDefault().getLanguage(); 怎么样?
  • 谢谢!这正是我想要的。
  • 我添加了更多选项以在我的答案中获取默认语言。

标签: java android sqlite android-studio locale


【解决方案1】:

你可以使用:

Locale.getDefault().getLanguage();

如果您想在用户从设置更改语言时获取语言环境,或者使用以下代码:

defaultLocale = Resources.getSystem().getConfiguration().locale;

无论为应用程序/活动设置哪个默认语言环境,它都会获取系统语言环境。 阅读https://developer.android.com/reference/android/content/res/Resources.html#getSystem%28%29

但请记住Resources.getSystem() 引用系统资源,如果使用incorrectly 可能会导致崩溃。

对于其他选项,您可以使用以下内容:

Locale current = getResources().getConfiguration().locale;

来自https://stackoverflow.com/a/14389640/4758255

请注意,这已在 Configuration 类中被弃用,有关此建议,请参阅最新文档:locale This field was deprecated in API level 24. Do not set or read this directly. Use getLocales() and setLocales(LocaleList). If only the primary locale is needed, getLocales().get(0) is now the preferred accessor

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2011-01-02
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多