1

I am using Google Translator code to translate website in languages.Following is my code...

 <div id="google_translate_element" style="padding-left:347px;"></div><script>
                        function googleTranslateElementInit() {
                            new google.translate.TranslateElement({
                                pageLanguage: 'fr'
                            }, 'google_translate_element');
                        }
                    </script><script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

It is working fine.But I want to use it according to the country.When website is opened in France it will be set to french , in India it will be Hindi and so on...

How to use this??? Or is there any other way to achieve this kind of task in php.

I thought if I get country language in some way then I will pass it in pageLanguage .But when I pass "fr" it does not changed to French.Also how to get country code,language using hostname.

Bhuvnesh Gupta
  • 107
  • 3
  • 15

2 Answers2

0

This should help you out , maxmind geolocation API

  <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
    <script type="text/javascript">var code = geoip_country_code();</script>
    <div id="google_translate_element" style="padding-left:347px;">
    </div>
                            <script type="text/javascript">
                            function googleTranslateElementInit() {
                                new google.translate.TranslateElement({
                                    pageLanguage: code
                                }, 'google_translate_element');

                            }
                        </script><script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit">

Madra David
  • 151
  • 1
  • 8
0

You could also query any geoip service that supports json & then set the translate language according to the country_code -- You may need to code in some exceptions, for example "au" for Australia, would need to default to en - or you could default any unknown translate languages to "en" etc...

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div id="google_translate_element" style="padding-left:347px;"></div><script>
    function googleTranslateElementInit() {

        $.getJSON("http://justmyip.org/api",function(result){

            console.dir(result);
            country_code = result.geo.country_code.toLowerCase();

            new google.translate.TranslateElement({
                pageLanguage: country_code
            }, 'google_translate_element');

        });
    }

</script><script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
David
  • 21
  • 3