Setting Locale Programmatically not working?

做~自己de王妃 提交于 2019-12-21 05:04:12

问题


I have an activity where I programmatically set the locale to "de" and it doesn't work as expected and displays the default language (English text) that is manually set. Please help

public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //Programmatically sets the locale and language  
        Locale locale = new Locale("de");  
        Locale.setDefault(locale);  
        Configuration config = new Configuration();  
        config.locale = locale;   
        getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());   

       Toast.makeText(getApplicationContext(),Locale.getDefault().getDisplayLanguage(),Toast.LENGTH_LONG).show();  

        setContentView(R.layout.main);  
Intent intent=new Intent(LatestLocalizationActivity.this,AnotherActivity.class);  
       startActivity(intent);  
}

回答1:


had you add the Strings.xml file in res->value-de folder?

public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //Programmatically sets the locale and language
                    setContentView(R.layout.main); 
                    config = getBaseContext().getResources().getConfiguration(); 
                    locale = new Locale("de");
                    Locale.setDefault(locale);
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                    refresh();



       Toast.makeText(getApplicationContext(),Locale.getDefault().getDisplayLanguage(),Toast.LENGTH_LONG).show();  


}



@Override
    public void onConfigurationChanged(Configuration newConfig) {
        Configuration config = getBaseContext().getResources().getConfiguration();
      // refresh your views here
        Locale.setDefault(locale);
        config.locale = locale;
      super.onConfigurationChanged(newConfig);
    }



private void refresh() {
        finish();
        Intent myIntent = new Intent(yourActivity.this, yourActivity.class);
        startActivity(myIntent);
    }



回答2:


Note that while you may be able to hack on things to kind-of get something like this to work, Android does not currently support doing this in a robust way. In particular, the framework works the current configuration in the resources, and will update it when it thinks appropriate. You will be fighting with this, and it is unlikely you are going to be able to have no situations where the configuration reverts back to the system locale.




回答3:


This worked for me:

public static void changeLocale(Context context, Locale locale) {
    Configuration conf = context.getResources().getConfiguration();
    conf.locale = locale;
    Locale.setDefault(locale);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        conf.setLayoutDirection(conf.locale);
    }

    context.getResources().updateConfiguration(conf, context.getResources().getDisplayMetrics());
}

please use your ACTIVITY CONTEXT and not your application context.



来源:https://stackoverflow.com/questions/7171517/setting-locale-programmatically-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!