2

I am using google translate for translating the web content.But as we know that it creates the div and classes itself, So i am unable to get selected language from drop down of google translate. I surf so many sites and so many questions but i did not find any appropriate answer.I want a solution that should not depends on cookies, And how I can call google translate every time with default language get from database for particular users.. My website is in english by default but i want that it should open in other language if other user choose their default language. I will be so thankful for a good solutions.I am using this normal code with 4 languages

<div id="google_translate_element" style="margin:10px 0 0;"> </div><div id="language"></div><script type="text/javascript">
function initializeGoogleTranslateElement() {
 new google.translate.TranslateElement({
  layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
  includedLanguages: "zh-CN,en,ja,ko"
 }, "google_translate_element");
}

</script>
shoaib
  • 129
  • 5
  • 15

3 Answers3

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

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

 <script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
Kos
  • 4,890
  • 9
  • 38
  • 42
1

Here is the solution use below code to select your default language, you can load the default language from the database:

<div id="google_translate_element" style="float:left;"></div>
    <script type="text/javascript">
    function googleTranslateElementInit() {
      new google.translate.TranslateElement({pageLanguage: 'ja', layout: google.translate.TranslateElement.InlineLayout.SIMPLE,includedLanguages: "zh-CN,en,ja,ko"
    }, 'google_translate_element');
    }
    </script>
    <script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
Akshay Jindal
  • 365
  • 2
  • 6
  • Thank you for your response, I already use it. But in chrome it showing error of file not found when it try to execute "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit" and in firefox its not showing any error but not working too. – shoaib Mar 17 '16 at 13:10
  • I find the solution to detect the selected language, but now how i can execute that language by default – shoaib Mar 17 '16 at 13:11
  • I have tried this code in jsFiddel and it is working fine at both the browsers. I think in your code some other JS issue is causing this error. – Akshay Jindal Mar 17 '16 at 13:30
  • hello bro.. sorry i lost my network yesterday.. btw can you give me that fiddle link ? for me that pageLanguage not working. and when i uploaded the complete script on the server, whole customization of dropdown box turn to default layout and it showing two dropdown for choosing language.. thank you – shoaib Mar 18 '16 at 04:52
0

Google translate adds an attribute "lang" on tag.

<html lang="en"></html>

Use the code below to get the selected language code - document.querySelector('html').getAttribute('lang')