a tool that helps you mass translate text that's bounded by either brackets or commas using Google's API. This Python script is designed to leverage the Google Cloud Translation API for translating text enclosed within specific brackets in a designated text file. The translated text is then stored in a new file.
Before diving into the translation magic, ensure you've got the essentials set up:
- Python: If you don't have it, grab it here.
- Google Cloud Translation API Key: Follow the steps here to create a Google Cloud project and get your API key.
- Required Python Packages: Install them using the command:
pip install google-cloud-translate tqdm
-
Configure Your API Key:
Before anything else, set your Google Cloud Translation API key in the script:
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'C:\\YOUR_PATH_HERE\\key.json'
-
Specify the File Path:
Point the script to your text file:
file_path = 'D:\\YOUR_PATH_HERE\\words.txt'
-
Adjust Translation Settings:
Tweak the following parameters according to your needs:
batch_size
: The number of text entries processed in each batch.target_language
: The language you want the text translated into.translation_method
: The type of brackets marking the text ('[]', '""', '{}').
-
Run the Magic:
Execute the translation with:
translate_file(file_path, batch_size=100, target_language='bs', translation_method='""')
-
translate_text(text, target_language)
: This function translates a single text entry. -
translate_file(file_path, batch_size=100, target_language='', translation_method='')
: This function translates text within a file.file_path
: The path to your input text file.batch_size
: Number of entries processed in each batch.target_language
: Language code for translation (e.g., 'bs' for Bosnian).translation_method
: The type of brackets used to identify text for translation ('[]', '""', '{}').
- Ensure your API key has the necessary permissions for the Google Cloud Translation API.
- Feel free to play around with
file_path
,batch_size
,target_language
, andtranslation_method
to suit your preferences.
Happy translating!