【问题标题】:How to get language (locale) currently Android app displays?如何获取当前 Android 应用程序显示的语言(区域设置)?
【发布时间】:2012-01-31 04:55:54
【问题描述】:

如何了解当前 Android 应用用于向用户显示文本的语言(区域设置)?

我知道我可以使用Locale.getDefault() 来获取默认的操作系统区域设置。但如果应用不支持此区域设置,则它可能与应用用于显示文本和其他资源的区域设置不同。


我需要确定应用程序显示的语言(区域设置),因此应用程序可以将语言传递给服务器,因此它可以本地化返回的结果。

【问题讨论】:

    标签: android localization locale


    【解决方案1】:

    我自己的解决方案是添加到 strings.xml 键值对 locale=<locale code>,因此 context.getResources().getString(R.string.locale) 将返回特定于使用的区域设置的区域设置代码。

    【讨论】:

    • 我猜这个解决方案应该是你问题的一部分。
    • @YaqubAhmad 我后来找到了这个解决方案。但我仍在等待可能的更好解决方案。
    • 我喜欢这个解决方案,因为这样我保证只有在使用我的翻译资源时才能获得语言环境代码。那是我唯一想要的时候。否则,如果我在获取语言环境时犯了某种错误,我可能会得到一种语言的日期和另一种语言的其他字符串资源。
    【解决方案2】:

    它在您的应用配置中,因此您可以通过以下方式获取它:

    getResources().getConfiguration().locale
    

    这与

    不同
    Locale.getDefault()
    

    并显示应用使用的可以不同的区域设置。

    它可能会有所不同,因为开发者可以通过更新应用配置来改变它, 检查:Resources.updateConfiguration

    【讨论】:

    • 您列出的第一个 fn 的文档显示“当前用户对区域设置的偏好,对应于区域设置资源限定符”。如果这是“当前用户偏好”,那么它与第二个 fn 有何不同。无论哪种方式,Locale 类的哪个函数调用一次以获取可以传递回服务器的区域设置代码字符串(相当于资源扩展)(如原始问题)?
    • 这个答案提到了获取Locale(第一行代码)的正确方法。但是有区别是错误的:Locale.getDefault() 相当于第一个例子。如果要获取语言代码,请致电getLanguage()
    • 不幸的是,这不像我需要的那样工作。如果设置系统语言,例如对于 Dansk(丹麦语),getResources().getConfiguration().locale 返回 da_DK,同时显示英文字符串(来自 values/strings.xml)。
    • getResources().getConfiguration().locale.getLanguage();是正确的方法
    【解决方案3】:

    以下代码适用于我:

    @TargetApi(Build.VERSION_CODES.M)
    private String getCurrentDefaultLocaleStr(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        return locale.getDefault().toString();
    }
    

    注意:在 Build.VERSION_CODES.N 上获取语言环境的方式略有变化。对于 N 及以上,代码应如下面的代码块所示;但是,我只是在上面的代码块中对 M 及以下执行此操作,并且它也与 N 及以上兼容。

        // Build.VERSION_CODES.N
        Locale locale = context.getResources().getConfiguration().getLocales().get(0);
    

    我支持的国家语言如下:

    English:             en 
    Traditional Chinese: zh_TW_#Hant 
    Simplified Chinese:  zh_CN_#Hans
    Spanish:             es_ES
    French:              fr_FR
    Japanese:            ja_JP
    German:              de_DE
    

    有一个以不同格式提供相同语言环境信息的方法列表:

    locale.getDefault().getCountry();
    locale.getDefault().getISO3Language();
    locale.getDefault().getISO3Country();
    locale.getDefault().getDisplayCountry();
    locale.getDefault().getDisplayName();
    locale.getDefault().getDisplayLanguage();
    locale.getDefault().getLanguage();
    locale.getDefault().toString();

    【讨论】:

    • Locale 上没有非静态的 getDefault() 方法
    【解决方案4】:

    您可以使用下面的代码。 例如,下面介绍的功能可以放在扩展应用程序类的类中。

    public class MyApplication extends Application {
        ...
        public static String APP_LANG;
        private Context ctx = getBaseContext();
        private Resources res = ctx.getResources();
        public SharedPreferences settingPrefs;
        ...
    
        public void restoreAppLanguage(){
        /**
        *Use this method to store the app language with other preferences.
        *This makes it possible to use the language set before, at any time, whenever 
        *the app will started.
        */
            settingPrefs = getSharedPreferences("ConfigData", MODE_PRIVATE);
            String lang = settingPrefs.getString("AppLanguage", "");
            if(!settingPrefs.getAll().isEmpty() && lang.length() == 2){
                Locale myLocale;
                myLocale = new Locale(lang);
                Locale.setDefault(myLocale);
                Configuration config = new Configuration();
                config.locale = myLocale;
                res.updateConfiguration( config, res.getDisplayMetrics() );
            }
        }
    
        public void storeAppLanguage(int lang) {
        /**
        *Store app language on demand
        */
            settingPrefs = getSharedPreferences("ConfigData", MODE_PRIVATE);
            Editor ed = settingPrefs.edit();
    
            Locale myLocale;
            myLocale = new Locale(lang);
            Locale.setDefault(myLocale);
            Configuration config = new Configuration();
            config.locale = myLocale;
            res.updateConfiguration( config, res.getDisplayMetrics() );
            ed.putString("AppLanguage", lang);
    
            ed.commit();
        }
    
        public void setAppLanguage(String lang){
        /**
        *Use this method together with getAppLanguage() to set and then restore
        *language, whereever you need, for example, the specifically localized
        *resources.
        */
            Locale myLocale;
            myLocale = new Locale(lang);
            Locale.setDefault(myLocale);
            Configuration config = new Configuration();
            config.locale = myLocale;
            res.updateConfiguration( config, res.getDisplayMetrics() );
        }
    
        public void getAppLanguage(){
        /**
        *Use this method to obtain the current app language name
        */
            settingPrefs = getSharedPreferences("ConfigData",MODE_PRIVATE);
            String lang = settingPrefs.getString("AppLanguage", "");
            if(!settingPrefs.getAll().isEmpty() && lang.length() == 2){
                APP_LANG = lang;
            }
            else APP_LANG = Locale.getDefault().getLanguage();
        }
    }
    

    然后我们可以在主代码中的任何地方编写:

    public class MainActivity extends ActionBarActivity {
        ...
        MyApplication app;
        ... 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
            app = (MyApplication)getApplicationContext();
            ...
            if(settingPrefs.getAll().isEmpty()){
                //Create language preference if it is not
                app.storeAppLanguage(Locale.getDefault().getLanguage());
            }
    
            //Restore previously used language
            app.restoreAppLanguage();
            ...
        }
        ...
        void SomethingToDo(){
            String lang = "en"; //"fr", "de", "es", "it", "ru" etc.
            ...
            app.getAppLanguage();
            app.setAppLanguage(lang);
            ...
            //do anything
            ...
            app.setAppLanguage(app.APP_LANG);
        }
        ...
    }
    

    在您的情况下,您很快可以使用getAppLanguage(),然后检查公共变量APP_LANG 以获取当前使用的语言。

    【讨论】:

      【解决方案5】:

      在 Kotlin 中,您可以将以下扩展功能添加到您的项目中:

      fun Context.getCurrentLocale(): Locale {
          return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
              this.resources.configuration.locales.get(0);
          } else{
              this.resources.configuration.locale;
          }
      }
      

      并在没有任何警告的情况下使用它:

      val locale = applicationContext.getCurrentLocale()

      【讨论】:

        【解决方案6】:
         public boolean CheckLocale(Context context, String app_language) {
                Locale myLocale = new Locale(app_language);
                Resources res = context.getResources();
                Configuration conf = res.getConfiguration();
                conf.locale = myLocale;
                if(res.getConfiguration().locale==myLocale)
                    return true;
                else 
        
                 return false;
        
            }
        

        使用这个方法。

        【讨论】:

          猜你喜欢
          • 2019-04-28
          • 2012-04-20
          • 1970-01-01
          • 2015-07-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-13
          • 2013-04-19
          相关资源
          最近更新 更多