javascripthtmlcsscdngoogle-translator-toolkit

Google translator cdn


I'm working on the HTML website I have google translator CDN and I want to change the English language to another language that is also working fine in google page insights. I got this error:

Properly defines charset Error! A character encoding declaration is required. It can be done with a <meta> tag in the first 1024 bytes of the HTML or in the Content-Type HTTP response header. Learn more about declaring the character encoding.

I have already added this charset encode meta tag:

 <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, interactive-widget=resizes-content">

   <script>
        function googleTranslateElementInit() {
            var translator = new google.translate.TranslateElement({ pageLanguage: 'en' }, 'google_translate_element');

            setTimeout(function () {
                var select = document.querySelector('.goog-te-combo');
                console.log("Dropdown element:", select); 
                select.value = 'es';
                console.log("Selected value:", select.value); // Debugging: Check if value is set to 'es'
                select.dispatchEvent(new Event('change'));
                console.log("Change event dispatched"); // Debugging: Check if change event is dispatched
            }, 10); // Increase timeout to 2 seconds for testing
        }
    </script>
    <script src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

Solution

  • <script>
            function googleTranslateElementInit() {
                var translator = new google.translate.TranslateElement({ pageLanguage: 'en' }, 'google_translate_element');
    
                setTimeout(function () {
                    var select = document.querySelector('.goog-te-combo');
                    console.log("Dropdown element:", select);
                    select.value = 'ru';
                    console.log("Selected value:", select.value);
                    select.dispatchEvent(new Event('change'));
                    console.log("Change event dispatched");
                }, 5000);
            }
    </script>
    

    I have added this code and my issue got solved i hope if someone face issue like me so this code will help them.