Load different files depending on language

372 views Asked by At

I'm showing in a WebView a local html file with text.

How can I load the english version of it depending on the language of the phone?

Here is my code:

public class Seccion1 extends Fragment {

WebView mWebView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View rootView = inflater.inflate(R.layout.seccion1, container, false);

    mWebView = (WebView)rootView.findViewById(R.id.lectura_webView);

    //Archivo de ejemplo. Cambiar por el adecuado.
    mWebView.loadUrl("file:///android_asset/productividad/GooglePlay2.html");

    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setDisplayZoomControls(false);

    mWebView.setLongClickable(true);
    mWebView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            return true;
        }
    });

    return rootView;

}

@Override
public void onResume() {
    super.onResume();

    // Buscar AdView como recurso y cargar una solicitud.
    AdView adView = (AdView)this.getView().findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {

        Uri uri = Uri.parse("https://www.twitter.com/XXX");
        startActivity( new Intent( Intent.ACTION_VIEW, uri ) );

        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Thank you!

P.D: I'm adding this text because it says thay my post is mostly code. I think the question is clear enough.

Edit 1:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.seccion4, container, false);

    mWebView = (WebView)rootView.findViewById(R.id.lectura_webView);

    //Archivo de ejemplo. Cambiar por el adecuado.
    //mWebView.loadUrl("file:///android_asset/textos_ES/Extra.html");

    String language= Locale.getDefault().getLanguage();
    String imageUrl="";
    if (language.equals("Spanish")){
        imageUrl="file:///android_asset/textos/Extra.html";
    }
    else {
        imageUrl="file:///android_asset/textos/Ejemplo1.html";
    }
    mWebView.loadUrl(imageUrl);

    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setDisplayZoomControls(false);

    mWebView.setLongClickable(true);
    mWebView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            return true;
        }
    });

    return rootView;
}
2

There are 2 answers

2
Anson Yao On BEST ANSWER

I think @Duggu has made it very clear. You can try something like

String language=Locale.getDefault().getLanguage();
String imageUrl="";
if (language.equals("English")){
    imageUrl="english.html"
}
if (language.equals("French")){
   imageUrl="french.html"
}
webView.loadUrl(imageUrl);
1
duggu On

Try below code :-

Locale.getDefault().getDisplayLanguage();

Above method return language of current device .