nuxt-google-translate
Version:
A simple Nuxt module that integrates the Google Translate widget into your Nuxt.js application, allowing seamless multilingual support without requiring an API key.
39 lines (38 loc) • 1.17 kB
JavaScript
import { computed } from "vue";
import { useNuxtApp } from "#app";
export function useGoogleTranslate() {
const { $googleTranslate } = useNuxtApp();
if (!$googleTranslate) {
return {
activeLanguage: computed(() => "en"),
// Default to English
supportedLanguages: computed(() => ["en"]),
// Default fallback
setLanguage: () => {
},
isLoaded: computed(() => false)
};
}
return {
/**
* Ref containing the currently active language
* @type {ComputedRef<string>}
*/
activeLanguage: computed(() => $googleTranslate.activeLanguage.value),
/**
* Array of supported language codes
* @type {ComputedRef<readonly string[]>}
*/
supportedLanguages: computed(() => $googleTranslate.supportedLanguages),
/**
* Function to set the active language
* @param {string} lang - The language code to set
*/
setLanguage: (lang) => $googleTranslate.setLanguage(lang),
/**
* Ref indicating whether the Google Translate script has been loaded
* @type {ComputedRef<boolean>}
*/
isLoaded: computed(() => $googleTranslate.isLoaded.value)
};
}