Skip to navigation
Update my po translation files automatically with a python script
22.05.24
using GoogleTranslator via the pyton deep_translator ```python #! env/bin/python import os,time, random import polib from deep_translator import GoogleTranslator import string import re stt = random.randint(1,10) def get_pofiles(target_lang='',path_dir = './'): if target_lang != '': print("...get_pofiles def") po_files = [] for root, dirs, files in os.walk(path_dir): for file in files: if file.endswith('.po'): po_path= "{}/{}".format(root,file) po = polib.pofile(po_path) #print(po.metadata) if po.metadata["Language"] == target_lang and len(po.untranslated_entries())> 0 : po_files.append(po_path) print(po_files) else: print("...to translate language is missing") print("...return {0}x po files for translate".format(len(po_files)) ) return po_files def trans_msgids(target_lang,files=[]): if target_lang != '': print("...def translate po to {0}".format(target_lang)) for i in files: po = polib.pofile(i) total = len(po.untranslated_entries()) for k,v in enumerate(po.untranslated_entries()): if v.msgid_plural != "": if isinstance(v.msgstr_plural, dict): if len(v.msgstr_plural) == 2: if v.msgstr_plural[0] == '' or v.msgstr_plural[1] == '': time.sleep(stt) print("...msgid_plural ", v.msgid_plural) print("...msgid ", v.msgid) print("...{0}/{1} translate".format(k+1, total)) #print("...msgstr 0", v.msgstr_plural[0]) #print("...msgstr 1", v.msgstr_plural[1]) v.msgstr_plural[0] = GoogleTranslator(source='auto', target=target_lang).translate(v.msgid) v.msgstr_plural[1] = GoogleTranslator(source='auto', target=target_lang).translate(v.msgid_plural) print('\t\t\t {}'.format(v.msgstr_plural[0])) print('\t\t\t {}'.format(v.msgstr_plural[1])) po.save(i) #print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") #break elif v.msgstr == "": if is_english(v.msgid): time.sleep(stt) print("...{0}/{1} translate \t {2}".format(k+1, total, v.msgid)) v.msgstr = GoogleTranslator(source='auto', target=target_lang).translate(v.msgid) print('\t\t\t {}'.format(v.msgstr)) po.save(i) #break else: print("...{0}/{1} just copy to trans \t {2}".format(k+1, total, v.msgid)) v.msgstr = v.msgid po.save(i) else: print("...missing lang parameters") def is_english(s): for i in s: if i.upper() in string.ascii_uppercase: return True return False if __name__ == "__main__": target_lang = 'th' path_dir='/home/veto/webs/merkurod/locale/th/LC_MESSAGES/' po_files = get_pofiles(target_lang,path_dir) msgids = trans_msgids(target_lang, po_files) #print(translated) ```
Reply
Anonymous
Information Epoch 1738775384
Effectiveness beats efficiency.
Home
Notebook
Contact us