Skip to content

Translation into English #4

@uloup

Description

@uloup

In order to support with the translation, I can share a simple Python script using the DeepL API:

import deepl
import os
import sys

# Define some environment variables or simply add your API token here
DEEPL_API_KEY = os.getenv('deepl_api_token')
DEEPL_FORMALITY = "default" # possible values: default/more/less

def translate_markdown(text, source_lang="DE", target_lang="EN-US"):
    """
    Translates a Markdown text

    :param text: Markdown input
    :param source_lang: source language (e.g. "EN-US" for American English)
    :param target_lang: target language (e.g. "DE" for Deutsch)
    :return: translated Markdown text
    """
    try:
        translator = deepl.Translator(DEEPL_API_KEY)
        result = translator.translate_text(
            text,
            source_lang=source_lang,
            target_lang=target_lang,
            preserve_formatting=True, # keep the formatting
            formality=DEEPL_FORMALITY
        )
        return result.text
    except deepl.DeepLException as e:
        print(f"Error: {e}")
        return None


if __name__ == '__main__':
    if sys.argv.__len__() > 1:
        print()
        input_file = sys.argv[1]
        with open(input_file) as f:
            input_text = "".join(line for line in f)
            print(translate_markdown(input_text))
    else:
        print("DeepL API Translator Tool for Formatted Texts from German (DE) to American English (EN-US).")
        print("Usage: python " + sys.argv[0] + " <path to the input file to be translated> >> <path to the translated output file>")

A free DeepL license already lets you translate 500 000 characters per month.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions