WebView中更改语言

WebView中更改语言

本文介绍了如何在Android WebView中更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在WebView中更改内容语言.我的示例(如下)效果很好,但是仅当我更改电话语言时,它才会更改.问题是当我在应用程序内更改语言 时-菜单,文本,名称等发生了变化,但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);

语言课

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;
    }
}

推荐答案

您可能会将应用程序的lang保留在某个位置,因此您应该添加一种获取正确lang的方法:

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;
 }

当用户修改应用内的lang时,您会调用setMyAppLang(lang);
在您的活动代码中:

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

String lang = getMyAppLang();

代替:

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

这篇关于如何在Android WebView中更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 20:19