Using Google Translator api to convert text in python.
2 min readAug 7, 2023
Step 1: Sign Up for Google Cloud Translation API
- Go to Google Cloud Console.
- Create a new project or select an existing project.
- Enable the “Cloud Translation API” for your project.
- Create credentials: Choose “Service Account Key,” select “JSON” format, and download the credentials JSON file.
Step 2: Install Required Libraries
First, make sure you have Streamlit and the googletrans
library installed. You can install them using the following commands:
pip install streamlit googletrans==4.0.0-rc1
Step 3: Create the Translation App
Create a Python script named translation_app.py
and follow the steps below:
import streamlit as st
from googletrans import Translator
def translate_text(text, source_lang, target_lang):
translator = Translator()
translated_text = translator.translate(text, src=source_lang, dest=target_lang)
return translated_text.textdef main():
st.title("Multilingual Text Translation App")
# Load Google API credentials credentials_path = "YOUR_GOOGLE_CREDENTIALS_JSON_FILE.json" os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path