首页 文章

如何获取当前Android应用程序显示的语言(区域设置)?

提问于
浏览
24

如何获知当前Android应用用于向用户显示文本的语言(语言环境)?

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


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

5 回答

  • 42

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

  • 1

    这是你的应用程序配置,所以你可以得到它:

    getResources().getConfiguration().locale
    

    这不同于

    Locale.getDefault()
    

    并显示应用程序使用的区域设置可能不同 .

    它可能不同,因为开发人员可以通过更新应用程序配置来更改它,请检查:Resources.updateConfiguration

  • 0

    以下代码适用于我:

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

  • 4

    您可以使用以下代码 . 例如,下面给出的函数可以放在扩展Application类的类中 .

    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 以获取当前使用的语言 .

  • 31
    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;
    
        }
    

    使用此方法 .

相关问题