1 (edited by rafat 11/09/2024 12:28:43 pm)

Topic: Translation using googletranslate

I was always interested in online translations. So I asked my friend chatgpt to help me translate empty.po to Arabic just to see how they behave as an AI gurus. They failed saying the best for me is to use googletranslate and they offered me help with a python script. The script that uses googletranslate failed also, so they suggested deep-translate. After many tries and errors I came out with this script. I use Linux Zorin which is a flavour of Ubuntu. So for the script to run one needs to install python3 and the other modules for it to work.
So here we go.

to install python 3

 sudo apt install python3 

After python 3 installation

 pip install deep-translator 

And

 pip install polib 

And here is the script which I called translate.py

import time
import re
from deep_translator import GoogleTranslator
from polib import pofile

# Load the .po file
po_file = pofile('empty.po')

# Update the Content-Type in the header to UTF-8
po_file.metadata['Content-Type'] = 'text/plain; charset=UTF-8'

# Initialize the translator
translator = GoogleTranslator(source='auto', target='ar')

# Define a regex pattern to match `&` before or after an alphanumeric character
mnemonic_pattern = re.compile(r"(?<=\w)&|&(?=\w)")

# Counters for statistics
total_entries = len(po_file)
translated_count = 0
failed_count = 0

# Start the timer
start_time = time.time()

# Process each entry in the .po file
for entry in po_file:
    # Translate if msgstr is empty
    if not entry.msgstr:
        try:
            print(f"Translating: {entry.msgid}")  # Debug print
            translated_text = translator.translate(entry.msgid)
            
            # After translating, remove mnemonics from the translated text
            entry.msgstr = re.sub(mnemonic_pattern, "", translated_text)
            
            translated_count += 1
            print(f"Translated: {entry.msgstr}")  # Debug print
            
        except Exception as e:
            print(f"Warning: Failed to translate '{entry.msgid}' due to {e}")
            entry.msgstr = "Translation Failed"
            failed_count += 1
    else:
        # If already translated, remove mnemonics from the existing translation
        entry.msgstr = re.sub(mnemonic_pattern, "", entry.msgstr)

# Ensure all msgstr values are not None before saving
for entry in po_file:
    if entry.msgstr is None:
        entry.msgstr = ""

# Calculate the elapsed time
end_time = time.time()
elapsed_time = end_time - start_time

# Convert elapsed time to hours, minutes, and seconds
hours, rem = divmod(elapsed_time, 3600)
minutes, seconds = divmod(rem, 60)

# Save the translated .po file
output_filename = 'translated_arabic.po'
try:
    po_file.save(output_filename)
    print(f"\nTranslation complete. The file is saved as '{output_filename}'")
except Exception as e:
    print(f"Error saving the file: {e}")

# Print translation statistics
print("\n--- Translation Statistics ---")
print(f"Total entries: {total_entries}")
print(f"Successfully translated: {translated_count}")
print(f"Failed to translate: {failed_count}")
print(f"Elapsed time: {int(hours)}h {int(minutes)}m {int(seconds)}s")

In the script I had to get rid of the & keyboard mnemonic as it does not work in any translation and should be changed in the code. i.e &Items is not translatable to what it should do once one presses Alt-I.

The above script is for Arabic as target='ar'
It works for most languages just by specifying the target language. i.e I tried it for Tamil target='ta'. It works. Do not forget to change the output_filename = 'translated_arabic.po' to whatever you like.


To run the scrip

 python3 translate.py 

As of all online translations its OK at about 70%. The rest is out of context and needs to be edited manually. Thats where one uses poedit to correct the 30%.

Just note. It took 40 minutes for Arabic Translation with 6 errors and 28 minutes for Tamil Translations with no errors. Total strings translated 3498.

Re: Translation using googletranslate

This is a fantastic improvement for translators. Thanks Rafat

Joe

Re: Translation using googletranslate

The removal of & in &Items can be automated as well as long as & is not used anywhere else.
It can also be a php script as part of FA just like:
https://github.com/apmuthu/frontac24/blob/master/FA24Mods/make_coa.php

Re: Translation using googletranslate

Great work

www.boxygen.pk

Re: Translation using googletranslate

Thanks for this wonderful tool, I tied it and once it was completed it gave this error:

Error saving the file: 'latin-1' codec can't encode characters in position 657-659: ordinal not in range(256)

so I used the following for saving and it worked

# Save the translated .po file with UTF-8 encoding
output_filename = 'translated_arabic.po'
try:
    with open(output_filename, 'w', encoding='utf-8') as f:
        f.write(po_file.__unicode__())  # Ensures the file is saved in UTF-8
    print(f"\nTranslation complete. The file is saved as '{output_filename}'")
except Exception as e:
    print(f"Error saving the file: {e}")