
To add Google Translate to your website, you'll need to copy and paste the code provided by Google into your website's HTML. This code is called the Google Translate widget.
The Google Translate widget is a JavaScript code that needs to be inserted into your website's HTML file, usually in the footer section. This is where it will load and function properly.
You can find the Google Translate widget code on the Google Translate website, under the "Get Started" section. From there, you can select the language you want to translate your website into and copy the code provided.
Once you have the code, you can paste it into your website's HTML file, making sure to replace any placeholder text with your own website's content.
Here's an interesting read: Google Doc File Type
Configuration and Customization
To get the most out of Google Translate on your website, you need to configure it correctly. The configuration options are detailed, and they're crucial for a seamless translation experience.
You can specify the Authenticated endpoint URL, which should forward the incoming POST payload from Google Translate along with the valid OAuth 2.0 headers and API key. This is done by setting the `apiProxy` parameter to a string value, like `https://www.domain.com/google-translate-api`.
The `translationNode` parameter determines the HTML element that requires translation. It's an object that defaults to `document.body`, but you can set it to any other element, like `document.getElementById("#banner")`.
You can also observe mutations on specific HTML elements by setting the `observeMutationsOn` parameter to an array of objects. For example, you might want to translate `document.getElementById("#banner")` and `document.getElementById("#main-content")` if they mutate.
Here are the configuration options in detail:
Persisting Language Preference
Language preference is stored in localStorage with the key of gTranslate_lang, making it persistent as users navigate your website.
This allows users to keep their preferred language even as they move from page to page.
To retrieve the language preference, simply run a command to access the stored value.
You might like: Change Language of Google Assistant

It's a small detail that can make a big difference in user experience.
The key is stored in localStorage, which is a convenient way to save user preferences without affecting the website's functionality.
You can access the stored value and use it to display content in the user's preferred language.
Discover more: Nexus S
Configuration Options
Configuration options play a crucial role in determining how your translation works. You'll need to specify the Authenticated endpoint URL, which should forward the incoming POST payload from Google Translate along with the valid OAuth 2.0 headers and API key param if set to be required by Google Cloud Translation API.
This URL is required, so make sure to fill it in with a valid endpoint, such as https://www.domain.com/google-translate-api.
The translationNode parameter is used to specify the HTML element that requires translation. By default, it's set to document.body, which means the entire page will be translated. However, you can change this to a specific element, like document.getElementById("#banner"), to only translate that particular section.
Recommended read: How to Set up Google Analytics on My Website

You can also use the observeMutationsOn parameter to specify an array of HTML elements that should be translated if they mutate. This is useful if you have dynamic content that needs to be translated. For example, you could use [document.getElementById("#banner"), document.getElementById("#main-content") ] to translate both the banner and main content sections.
On the other hand, you can use the excludeElements parameter to specify an array of elements that should not be translated. This is useful if you have certain elements that shouldn't be translated, like a user profile section. For example, you could use [document.getElementById("#user-profile"), document.getElementById("#hidden-element") ] to exclude the user profile and hidden element sections.
The sourceLanguage parameter is optional but recommended. If you know the BCP-47 language code of the input text, you can specify it here. For example, you could use "en" for English or "sr-Latn" for Serbian (Latin). If you don't specify the source language, the API will attempt to identify it automatically.
The targetLanguage parameter is required and specifies the BCP-47 language code to use for translation of the input text. You should set it to one of the language codes listed in Language Support of Google Cloud Translate. For example, you could use "fr" for French.
Here's an interesting read: Google Profile No Showing up on the Sid
JavaScript Implementation
To implement Google Translate in your website using JavaScript, include the Google Translate script in your main scripts file, ideally after the Google Translate script, and add CSS to style the 'Translating...' popup.
You can then call the init method and pass the required configuration, which can be useful if you want to show a loading screen or graphic as Google Translate can take a few seconds to complete the translation process.
To change the language, call the googleTranslate.setTargetLanguage method with the target language code, for example, 'fr' for French.
This method returns a promise which can be used to detect completion of page translation action.
To create a translator, check for user activation and call the asynchronous create() function, which requires an options parameter with two fields, one for the sourceLanguage and one for the targetLanguage.
Once you have a translator, you can call the asynchronous translate() function, or use the streaming version of the API and call translateStreaming() if you need to deal with longer texts.
To add a Google Translate button on your webpage, you'll need to add a JavaScript function to your code, as shown in the example:
For another approach, see: Google Fi Call Forwarding
Translator Functionality
To create a translator, you need to check for user activation and call the asynchronous create() function, which requires an options parameter with two fields: sourceLanguage and targetLanguage.
The Translator create() function is an asynchronous operation, meaning it runs in the background without blocking your website's functionality.
Once you have a translator, you can call the asynchronous translate() function to translate text. This is the most straightforward way to use the translator API.
Alternatively, if you need to deal with longer texts, you can use the streaming version of the API and call translateStreaming() instead.
For more insights, see: Google Sheet Custom Function
Featured Images: pexels.com


