Clicking a button to switch the language

前端 未结 2 541
逝去的感伤
逝去的感伤 2020-12-14 23:50

When I click on the \"sub_changelang\" button, it should change the program language to French for example. I got the following code to change the locale but I have no idea

相关标签:
2条回答
  • 2020-12-15 00:24

    You can use activity.this.recreate().But it will support from API level 11.

    0 讨论(0)
  • 2020-12-15 00:34

    I am using this code to set locale

    String languageToLoad  = "fr_FR";
         Locale locale = new Locale(languageToLoad); 
         Locale.setDefault(locale);
         Configuration config = new Configuration();
         config.locale = locale;
         context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());
    
    Intent intent = new Intent(XYZ.this, XYZ.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    

    here context is application Base Context. Please also try "fr" instead of "fr_FR" because I am working for Arabic locale and its working fine.

    You need to restart your Activity after changing locale.

    0 讨论(0)
提交回复
热议问题