0

My code is running well and is changing language from English to Hindi but when we again click on it it won't change back to english language

       SharedPreferences sharedPreferences = getSharedPreferences("Language",MODE_PRIVATE);
        SharedPreferences.Editor editor1 = sharedPreferences.edit();


        language.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sharedPreferences.getString("lang","eng").equals("eng")){
                    Toast.makeText(Settings.this, "Changing Language to Hindi", Toast.LENGTH_SHORT).show();
                    context = LocaleHelper.setLocale(Settings.this,"hi");
                    resources = context.getResources();
                    editor1.putString("lang","hi");
                    editor1.apply();
                    startActivity(new Intent(Settings.this,Settings.class));
                }
                else {
                    Toast.makeText(Settings.this, "Changing Language to English", Toast.LENGTH_SHORT).show();
                    context = LocaleHelper.setLocale(Settings.this,"eng");
                    resources = context.getResources();
                    editor1.putString("lang","eng");
                    editor1.apply();
                    startActivity(new Intent(Settings.this,Settings.class));
                }
            }
        });

I am getting the Toast message correctly When clicking on language it is showing changing language to Hindi , when we click again, it shows changing language to English but the language is changing

I am not sure what to do

1 Answers1

0

Try .setLocale(Settings.this,"en"); instead of .setLocale(Settings.this,"eng");.

The language code might be causing the issue, for language code reference refer to this link.

Update

This method worked for me. Implement your setLocale strategy in your BaseActivity. Also create a LanguageHelper class.

open class BaseActivity : AppCompatActivity() {

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(LanguageHelper(base!!).changeBaseContextLocale())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LanguageHelper(this).updateLocaleForLegacy()
    }
}

LanguageHelper class:

class LanguageHelper(val context: Context) {

    private fun getSelectedLocaleFromSharedPref(): String {
        ...
    }

    fun changeBaseContextLocale(): Context? {
        val locale = Locale(getSelectedLocaleFromSharedPref())
        Locale.setDefault(locale)
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            updateLocale()
        } else context
    }

    // update the locale for API 24 and above.
    private fun updateLocale(): Context {
        val configuration = Configuration(context.resources.configuration)
        configuration.setLocale(Locale(getSelectedLocaleFromSharedPref()));
        return context.createConfigurationContext(configuration);
    }


    // update the locale for API 23 and below.
    fun updateLocaleForLegacy(): Context? {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) return context

        val resources: Resources = context.resources
        val configuration: Configuration = resources.getConfiguration()
        configuration.setLocale(Locale(getSelectedLocaleFromSharedPref()))
        resources.updateConfiguration(configuration, resources.getDisplayMetrics())
        return context
    }
}

In the settings, only store the selected language locale (for example: "en") in the shared preferences and then restart the app, then the language will change.

Mostafa Arian Nejad
  • 1,278
  • 1
  • 19
  • 32