Search code examples
androidwebviewandroid-webviewlocale

How to change Language in android WebView


How can I change content language in my WebView. My exampe (below) works good, but it changing only when I change phone language. The problem is when I change language inside of my App - menu, text, names etc. changing except content in webview.

public class WebViewActivity extends Activity {

WebView mWebView;

        public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.web_view_activity); // ID Activity


    mWebView = (WebView) findViewById(R.id.webview); // ID webview
    mWebView.getSettings().setJavaScriptEnabled(true); // Enable JavaScript
    mWebView.getSettings().setAllowFileAccess(true); // File access

            String lang = Locale.getDefault().getLanguage();
    String filename = "www/index.html";
    if (lang.equals("en")) {
      filename = "www/index.en.html";
    }
    mWebView.loadUrl("file:///android_asset/" + filename);

LANGUAGE CLASS

import java.util.Locale;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.preference.PreferenceActivity;

import com.training.MainActivity;
import com.training.programs.R;


public class LanguageLocale extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener {

    PreferenceManager manager;
    ListPreference listPreference;
    SharedPreferences sharedPreference;
    Locale RUSSIAN= new Locale("ru", "ua");
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.language_option_preference);
        sharedPreference = PreferenceManager.getDefaultSharedPreferences(this);

        manager = getPreferenceManager();
        listPreference = (ListPreference) manager.findPreference("language_setting");

        listPreference.setOnPreferenceChangeListener(this);
    }



    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {

        Resources resource = getResources();
        Configuration config = resource.getConfiguration();

        int pos = Integer.parseInt((String) newValue);
        if (pos == 1) {
            sharedPreference.edit().putString("language", "en").commit();
            config.locale = Locale.ENGLISH;
            listPreference.setValue("1");

        } else if (pos == 2) {
            sharedPreference.edit().putString("language", "ru").commit();

            config.locale = RUSSIAN;
            listPreference.setValue("2");
        } else {
            sharedPreference.edit().putString("language", "auto").commit();
            config.locale = Locale.getDefault();
            listPreference.setValue("0");
        }

        getBaseContext().getResources().updateConfiguration(config, null);

        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.startActivity(intent);
        return false;
    }
}

Solution

  • You probably keep your app lang some where, so you should add a method to get the right lang :

    private String mLang;
    public String getMyAppLang(){        
        if (mLang==null)
            mLang = Locale.getDefault().getLanguage();
        return mLang;
    }
     public void setMyAppLang(String lang){
        mLang = lang;
     }
    

    When the user modify the lang inside your app, you call setMyAppLang(lang);
    And in your activity code:

    String lang = getMyAppLang();
    

    Instead of:

    String lang = Locale.getDefault().getLanguage();