set different language in android studio

跟風遠走 提交于 2019-12-01 14:48:34
/***You need to create separate String file for both arabic and English***/

English
 Values
  String.xml
Arabic   
 Values-ar
  String.xml

/**You can change the application language using the following code:**/

 language =  "en"; or  language =  "ar";

@Override
public void recreate()
{
    if (android.os.Build.VERSION.SDK_INT >= 14)
    {
        super.recreate();
    }
    else
    {
        startActivity(getIntent());
        finish();
    }
}

private void updateLanguage(String language)
{
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

    SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
    SharedPreferences.Editor editor = languagepref.edit();
    editor.putString("languageToLoad",language);
    editor.apply();

    recreate();
}

To translate an app you must go to the Translations Editor which is accessible when you open strings.xml. You will have to translate every string yourself and put the desired translation into the Translations Editor. To access different translations the user will have to have their Locale on their device set to one of the country codes that you provide support for. For example in your app you would end up having strings.xml and strings.xml (us) and the app would use Strings from strings.xml as default unless their locale was set to us in which case they would use the strings.xml (us) strings

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