Vue Google Translate Integration Component

2.0.20 · maintenance · verified Wed Apr 22

The `vue-gtranslate` package provides a Vue.js component designed to integrate Google Translate's 'Website Translator' widget into a Vue application. It facilitates on-the-fly page translation, supporting approximately 40 languages, and allows for basic visual customization through CSS classes. As of version 2.0.20, the project is actively maintained but explicitly cautions users about potential instability and the expectation of bugs, suggesting a more community-driven or less rigorously tested release cycle. Its core differentiator lies in simplifying the embedding process of the Google Translate script and its associated UI within a Vue component, thereby abstracting some of the direct DOM manipulation typically required. This solution wraps Google's client-side translation widget and does not offer an offline or server-side translation mechanism.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `vue-gtranslate` into a Vue 3 application. It includes the necessary external Google Translate script setup in `index.html` and shows how to import and use the `Translator` component along with its crucial CSS for proper functionality and UI control.

<!-- public/index.html (or equivalent root HTML file) -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue GTranslate App</title>
    <script type="text/javascript">
        function googleTranslateElementInit() {
            new google.translate.TranslateElement(
                { pageLanguage: "en", autoDisplay: false }, // autoDisplay: false to manage display via Vue
                "google_translate_element"
            );
        }
    </script>
    <script
        type="text/javascript"
        src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"
    ></script>
</head>
<body>
    <div id="app"></div>
    <div id="google_translate_element" style="display: none;"></div> <!-- Hidden div for Google widget -->
    <script type="module" src="/src/main.js"></script> <!-- Assuming Vue CLI/Vite setup -->
</body>
</html>

// src/App.vue
<template>
  <div id="app">
    <h1>Welcome to My App</h1>
    <p>This content will be translated.</p>
    <Translator/>
  </div>
</template>

<script setup>
import Translator from "vue-gtranslate";
import "vue-gtranslate/translator.css"; // Important for styling and hiding pop-up
</script>

<style>
body {
  font-family: Arial, sans-serif;
}
.vg-body {
  display: flex;
  gap: 10px;
  padding: 10px;
  background: #f0f0f0;
  border-radius: 5px;
  justify-content: center;
}
.vg-container {
  padding: 5px 10px;
  font-size: 16px;
  text-align: center;
  background: white;
  border: 1px solid #ddd;
  border-radius: 3px;
}
.vg-text {
  color: #333;
  padding-left: 5px;
}
.vg-items {
  display: flex;
  align-items: center;
}
.vg-flag {
  height: 24px;
  vertical-align: middle;
}
</style>

view raw JSON →