How to detect the user's language and do one thing or another?

2

I just need to know if it's English go to url2 and if it's Spanish url. I have some layout with strings in Spanish and English and I know how to use them so that it comes out depending on the language of the user, but being a URL, I am not able to make it go to one URL or another.

I tried to put a string and compare or assign numbers but nothing, I'm not capable. The solution would be if (it's English) {go to url2} otherwise (Spanish)

public class Competidores extends android.support.v4.app.Fragment {

WebView appWeb;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.four_tab, container, false);


    String url = " https://rudeboysapp.wordpress.com/";
    String url2 = " https://rudeboysappenglish.wordpress.com/";

  if(l7 == "es") {

        appWeb = (WebView) v.findViewById(R.id.webView);
        //Habilitamos el javaScript y el zoom
        appWeb.getSettings().setJavaScriptEnabled(true);
        // appWeb.getSettings().setBuiltInZoomControls(true);
        //Cargamos el enlace definido
        appWeb.loadUrl(url);
        //Este método es para que el navegador se quede en nuestra aplicación
        appWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });

    }else {


        appWeb = (WebView) v.findViewById(R.id.webView);
        //Habilitamos el javaScript y el zoom
        appWeb.getSettings().setJavaScriptEnabled(true);
        // appWeb.getSettings().setBuiltInZoomControls(true);
        //Cargamos el enlace definido
        appWeb.loadUrl(url2);
        //Este método es para que el navegador se quede en nuestra aplicación
        appWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });

    }




    return v;
}
    
asked by Rf Mvs 05.02.2017 в 10:09
source

1 answer

3

You must pick up the system language using the Locale object and its getDefault () method:

Locale.getDefault().toString();

Depending on what I give you, it will be one language or another. For example, American English is "en_US" while British English is "en_EN" and Spanish of Spain is "es_ES" ... and so on. If you notice, always the first two acronyms are the language and the next two behind the "_" are the country.

If you are only interested in the language, regardless of the country, you have the getLanguage () method:

    String l7 = Locale.getDefault().getLanguage(); // es

This object also has other methods that you may find useful such as:

    String l1 = Locale.getDefault().getCountry(); // español
    String l2 = Locale.getDefault().getDisplayCountry(); // España
    String l3 = Locale.getDefault().getDisplayLanguage(); // español
    String l4 = Locale.getDefault().getDisplayName();        // español (España)
    String l5 = Locale.getDefault().getDisplayVariant(); //""
    String l6 = Locale.getDefault().getISO3Language(); //spa
    String l7 = Locale.getDefault().getLanguage(); // es
    String l8 = Locale.getDefault().toString(); // es_ES
    
answered by 05.02.2017 / 12:10
source