Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
How can I use google translate directly on sveltekit web pages

I've been having issues using google translator directly on my sveltekit web app using the following code

<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
    <script type="text/javascript">
        function googleTranslateElementInit() {
          new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
        }
  // googleTranslateElementInit()
     </script>

I've tried several approaches, for example putting the code in <svelte:head></svelte:head> but yet it's kept appearing and disappearing. Please how can I achieve this, Note I'm using Static adapter, thanks in advance

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I had to load the script manually after mounting of component. It works with SvelteKit too.

onMount(() => {
loadTranslate()
setTimeout(function () {
    googleTranslateInit()
}, 3000)
})
function googleTranslateInit() {
    const checkIfGoogleLoaded = setInterval(() => {
        if (google != null && google.translate != null && google.translate.TranslateElement != null) {
            clearInterval(checkIfGoogleLoaded)

            googleTranslateElementInit()
        }
    }, 1000)
}

function googleTranslateElementInit() {
    new google.translate.TranslateElement({ pageLanguage: 'en' }, 'google_translate_element')
}
function loadTranslate() {
    loading = true
    if (browser) {
        const domElement = document.createElement('script')
        domElement.setAttribute('src', '//translate.google.com/translate_a/element.js')
        domElement.onload = () => {
            loadedTranslate = true
        }
        document.body.appendChild(domElement)
    }
}
</script>

<template>
    <div id="google_translate_element" class="mb-2"></div>
</template>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!