Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap - the best way to discover the country iso code from a user

Tags:

cordova

iso

what is the best and fastest way to get the country iso code by starting an App build with phonegap? There is a function to get the geo coordinates, but not the Iso code. For getting the iso code maybe it is necessary to call an external webservice and get the iso by given coordinates. Is there a simplest way?

Thanks Nik

like image 786
Nik Avatar asked Dec 04 '25 06:12

Nik


1 Answers

I found navigator.language sometimes only gave me the language and not the locale (eg "en" rather than "en-gb"). To solve this in android I created a new java class in src/com called DeviceCulture.java with this code:

package com;
import java.util.Locale;

public class DeviceCulture {

    public String get() {
        try{            
            return Locale.getDefault().toString();//get the locale from the Android Device      
        }catch(Exception e){
            return "";
        }   
    }
}

Then add this before the super.loadUrl line in /src/com/phonegap/YOUR_APP/App.java

DeviceCulture deviceCulture = new DeviceCulture();
appView.addJavascriptInterface(deviceCulture , "DeviceCulture");

Then in your javascript use this to get the locale (eg "en_GB")

window.DeviceCulture.get();
like image 52
Patrick64 Avatar answered Dec 06 '25 05:12

Patrick64



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!