In this post we will be creating a Python script that will translate English text to Japanese. To do this we will be using the Googletrans library, this library makes it easy to translate text from one language to another. It uses the Google Translate API and supports over 100 languages.
At the time of writing this there are issues affecting the library although you can use an alternative version of the library. To install it run the following in command line.
pip install googletrans==3.1.0a0
Fun Fact: Japanese has multiple words for ‘I,’ with the choice dependent on formality and relationship. The casual ‘boku’ and polite ‘watashi’ are two examples.
See the snippet of Python code below where we translate English text to Japanese. For this example we will be taking user input which will be in English and return the translation in Japanese to output.
from googletrans import Translator while True: translator = Translator() text=input("Enter text: ") translation = translator.translate(text, dest='ja') print(translation.text,"\n")
An example of the script running can be seen below.
Enter text: hello こんにちは Enter text: whats your name あなたの名前は何ですか Enter text: my name is Rahul 私の名前はラフルです >>>
Take a look at some of our other content around the Python programming language by clicking here.