import os
import time
from googletrans import Translator

def read_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        return file.read()

def write_file(file_path, content):
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(content)

def append_to_file(file_path, content):
    with open(file_path, 'a', encoding='utf-8') as file:
        file.write(content)

def translate_text(text, src='en', dest='pl', retry=10, delay=10):
    translator = Translator()
    for attempt in range(retry):
        try:
            translation = translator.translate(text, src=src, dest=dest)
            return translation.text
        except Exception as e:
            print(f"Error: {e}. Retrying in {delay} seconds... ({attempt + 1}/{retry})")
            time.sleep(delay)
    raise Exception(f"Failed to translate text after {retry} attempts.")

def split_text(text, max_length=4000):
    """
    Dzieli tekst na fragmenty o długości maksymalnej max_length, ale
    nie dzieli wyrazów.
    """
    parts = []
    while len(text) > max_length:
        split_index = text.rfind(' ', 0, max_length)
        if split_index == -1:  # jeśli nie można znaleźć spacji, podziel po max_length
            split_index = max_length
        parts.append(text[:split_index])
        text = text[split_index:].strip()
    parts.append(text)
    return parts

def translate_large_file(input_path, output_path):
    text = read_file(input_path)
    parts = split_text(text)
    
    # Usuń plik wyjściowy, jeśli istnieje
    if os.path.exists(output_path):
        os.remove(output_path)

    for index, part in enumerate(parts):
        print(f"Translating part {index + 1}/{len(parts)}")
        translated_text = translate_text(part)
        append_to_file(output_path, translated_text)
        print(f"Part {index + 1} translated and saved.")
    
    print("Translation complete.")

if __name__ == "__main__":
    input_path = '/home/ania/buy-cheap-en.txt'  # ścieżka do pliku wejściowego
    output_path = '/home/ania/buy-cheap-pl.txt'  # ścieżka do pliku wyjściowego
    translate_large_file(input_path, output_path)
