Created
November 25, 2020 13:31
-
-
Save abhiabhi94/3eae6ccfc693d1c0b3d7c6e4175096d7 to your computer and use it in GitHub Desktop.
Generate translation into a django project for a specific language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This file can be used to generate translation into a django project for a specific language. | |
Things to keep in mind: | |
- This may not work on larger files. | |
- This script not be helpful for(it should be able to translate almost everything else): | |
- Strings that span multiple lines. | |
- Strings that contain some parameter.(e.g `msg_id "This {value} is strange"`) | |
- Install googletrans using the command `pip install googletrans`(https://github.com/ssut/py-googletrans).\ | |
Many thanks to them for this project | |
- In case, your IP get blocked by google, try to increase the sleep time. | |
- Change the parameters to the translate function on line 59 as per your requirements. | |
- This is not supposed to be a "end-all". Please verify your translations personally after running the script.\ | |
It is only meant to reduce some sort of labour not all :-p | |
- Make sure that you place this file in the same directory as the translation files(`django.po` and `djangojs.po`).\ | |
Otherwise you will have to change the path for opening the translation files. | |
- I am not sure this is completely legal. :) | |
""" | |
import math | |
import time | |
import os | |
from googletrans import Translator | |
TRANSLATION_FILES = [ | |
'django.po', | |
'djangojs.po' | |
] | |
start = time.time() | |
translator = Translator() | |
here = os.path.dirname(__file__) | |
def time_taken_in_mins(): | |
diff = (time.time() - start) / 60 | |
minutes = math.floor(diff) | |
seconds = (diff - minutes) * 60 | |
time_taken = f'{minutes} minutes' | |
if seconds: | |
time_taken += f' {seconds :.2f} seconds' | |
return time_taken | |
for file in TRANSLATION_FILES: | |
file_loc = os.path.join(here, file) | |
with open(file_loc, mode='r') as f: | |
all_contents = f.readlines() | |
starting_pattern, ending_pattern = 'msgid "', '"' | |
count = 0 | |
for (line_num, line) in enumerate(all_contents): | |
line = line.strip() | |
if line: | |
if line.startswith(starting_pattern) and line.endswith(ending_pattern): | |
msg = line.lstrip(starting_pattern).rstrip(ending_pattern) | |
if msg: | |
translated_msg = translator.translate(msg, src='en', dest='hi').text | |
all_contents[line_num + 1] = 'msgstr "' + translated_msg + '"\n' | |
count += 1 | |
print(f'{count} strings translated in {time_taken_in_mins()} minutes') | |
time.sleep(10) | |
with open(file_loc, mode='w') as f: | |
f.writelines(all_contents) | |
print(f'Process completed in {time_taken_in_mins()}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment