set different language in android studio

前端 未结 2 1433
梦毁少年i
梦毁少年i 2021-01-17 04:21

am new in android studio and I am trying to make my simple app weather info via yahoo weather service JSON data , i would like to make a different language display in my app

相关标签:
2条回答
  • 2021-01-17 04:43

    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

    0 讨论(0)
  • 2021-01-17 04:46
    /***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();
    }
    
    0 讨论(0)
提交回复
热议问题