User Tools

Site Tools


en:arkhalia-translate-script

Arkhalia translation Python script

This script is used for translating wiki:

# Jurass17 2025

# Input parameters


CHATGPT_API_KEY = ""
INPUT_DIRECTORY_ABSOLUTE = "/Users/jiri/projekty/my_python_scripts/arkhalia_translate/en"
OUTPUT_DIRECTORY_ABSOLUTE = "/Users/jiri/projekty/my_python_scripts/arkhalia_translate/cs"
INPUT_LANGUAGE_AND_FOLDER_NAME = "en"
OUTPUT_LANGUAGE_AND_FOLDER_NAME = "cs"

CHATGPT_COMMAND = f"""
You are a helpful translator from {INPUT_LANGUAGE_AND_FOLDER_NAME} to {OUTPUT_LANGUAGE_AND_FOLDER_NAME}. 

The text I will give you to translate is the DokuWiki Article text. It contains some DokuWiki special formatting syntax, like caption "===== Some caption =====",
or link to other pages "[[en:ruleset:touch_attack|Dotykový útok]]", or "<WRAP group>". Is is important that during your translation
you will not malform the original syntax of the DokuWiki elements. You are smart enough to do it, I know that.

Maybe it will be handy for you that the DokuWiki Articles you are going to translate is the community driven DokuWiki related 
to the game Neverwinter Nights 1 online persistent world. Maybe, if you will be unsure how to translate something,
like names of game feats, or spells, it will help you to know it.

Also, this is the set of rules I want you to comprehend during the translation:
1. When you encounter a link like "[[en:craftingguide]]", then I want your output to be the exact original version: "[[en:craftingguide]]"
2. When you encounter a link containing custom text like  "[[en:craftingguide|This is a custom caption]]", then I want your output to translate the custom caption like this: "[[en:craftingguide|Tohle je custom popisek]]"
3. The word "Feats" shall be translated as "Odbornosti"
3. The word "Classes" shall be translated as "Povolání"
4. Consider not to translate names of some Classes and keep the name original, like "Acolyte of Skin" sounds weird in Czech to be translated.
5. The word "Skills" shall be translated as "Dovednosti"

That's all.
"""


# Import


import os
from openai import OpenAI
from pathlib import Path


# Configuration


client = OpenAI(api_key=CHATGPT_API_KEY)


# Function definitions


def translate_text(text):
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": f"{CHATGPT_COMMAND}"},
            {"role": "user", "content": f"Translate this:\n\n{text}"}
        ],
        temperature=0.5,
    )
    return response.choices[0].message.content


def find_all_txt_files_in_directory(folder_path):
    txt_file_paths = []
    for subdir, dirs, files in os.walk(folder_path):
        for file in files:
            path = os.path.join(subdir, file)
            if not path.endswith(".txt"):
                print(f"Skipping file: {path}")
                continue
            txt_file_paths.append(path)
    txt_file_paths = sorted(txt_file_paths)
    return txt_file_paths


def load_file_content(path):
    with open(path, 'r') as f:
        return f.read()


def save_file_content(path, content):
    file_path = Path(path)
    file_path.parent.mkdir(parents=True, exist_ok=True)
    with open(file_path, 'w') as f:
        return f.write(content)


def modify_links_in_dokuwiki_article(content: str):
    return content.replace(f"[[{INPUT_LANGUAGE_AND_FOLDER_NAME}:", f"[[{OUTPUT_LANGUAGE_AND_FOLDER_NAME}:")


# Execute


if __name__ == "__main__":
    txt_file_paths = find_all_txt_files_in_directory(INPUT_DIRECTORY_ABSOLUTE)

    try:
        for file_path in txt_file_paths:
            # Translate

            file_content = load_file_content(file_path)
            translated = translate_text(file_content)

            # Modify links

            translated = modify_links_in_dokuwiki_article(translated)

            # Convert input path to output path

            relative_file_path = file_path.replace(INPUT_DIRECTORY_ABSOLUTE, '')
            output_path = OUTPUT_DIRECTORY_ABSOLUTE + relative_file_path

            # Save

            save_file_content(output_path, translated)
            print(f'Stored ({txt_file_paths.index(file_path)}/{len(txt_file_paths)}): {output_path}')
    except BaseException as e:
        print(f"An error occured. Input file path: {file_path}, exception: {e}")

en/arkhalia-translate-script.txt · Last modified: 2025/06/06 08:03 by juras17