0

I use google website translator for my website which lets the user select a country from the dropdown list. Google does the webpage translation. The code is below:

    <div id="google_translate_element"></div>

<script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>

<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

However, I want a way to let the website detect the user's IP and translate automatically based on the result.

For example, a user who only understands French visits a webpage that says "Click ME to Translate to French". Unfortunately the French guy wouldn't be able to know that what the text says. So I would really appreciate help here.

Aliu
  • 53
  • 9
  • 1
    I feel like using IP address or any other location metric is fraught with peril. I live in the US, but there is no way to tell if anyone else with a US based IP will prefer English or Spanish. Also, I think the French person would understand a button that says to translate into French if the text of the button was in French. – ecg8 Dec 28 '18 at 02:28
  • My bad, when you open the webpage with google translate, what it says is "Select Language", so i doubt the French guy would understand, if there is a work around on getting the webpage to auto translate based on IP, i think i will like that and still leave the select language button just incase. – Aliu Dec 28 '18 at 02:31
  • Maybe this post can help you : https://stackoverflow.com/questions/391979/how-to-get-clients-ip-address-using-javascript – Shim-Sao Dec 28 '18 at 02:39
  • 1
    I still think IP address is not a good way to get the info you are looking for. Consider this https://stackoverflow.com/questions/673905/best-way-to-determine-users-locale-within-browser – ecg8 Dec 28 '18 at 02:41
  • @Shim-Sao there are various answers there but pardon, am a newbie. I need a way to integrate it into the google webpage translator script to work automatically. – Aliu Dec 28 '18 at 02:42
  • 2
    You can get the user's preferred language in JavaScript. It's one of the settings they configure for their web browser. – Reactgular Dec 28 '18 at 02:43
  • 1
    Possible duplicate of [JavaScript for detecting browser language preference](https://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference) – Reactgular Dec 28 '18 at 02:43
  • @cgTag how do i get it to work with google translate automatically without the user selecting a language? – Aliu Dec 28 '18 at 03:59
  • Use Geolocation, not IP. –  Dec 28 '18 at 17:42
  • What’s the preferred language of somebody in, say, Belgium, where they speak French, Dutch and German…? – deceze Dec 28 '18 at 17:42

1 Answers1

1

What you want is Geolocation. While you can do it through IP address, HTML5 offers a far simpler way:

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
    $.getJSON('http://ws.geonames.org/countryCode', {
      lat: position.coords.latitude,
      lng: position.coords.longitude,
      type: 'JSON'
    }, function(result) {
      alert(result.countryName);
    });
  });
}

You can then translate based off of country name as you suggested. Also check out some of the posts linked in the comments.