To create a Google Translate widget, you need to include the Google Translate API script in your website and create a container for the widget. Here are the steps:
Get a Google Translate API Key: To use the Google Translate API, you need to have a Google Cloud account and generate an API key.
Include the API script in your HTML: Add the following code to your HTML file to include the Google Translate API.
Create a container for the widget: Create a
divelement with a unique
idthat will serve as a container for the widget. You can place this element anywhere on your website where you want the widget to appear.
You can replace
'en'with the desired default language code.
- Test the widget: Load your website in a browser and verify that the widget is functioning correctly.
Note: This code assumes you have access to the Google Translate API, which is not available for free. If you are building a website for commercial purposes, you may need to pay for usage of the API.