Skip to main content

A directory-preserving AI translator using Ollama.

Project description

OpenTrans

OpenTrans is a privacy-first, local-LLM batch translation utility using Ollama. It is designed to mirror a source directory into a target language while preserving the exact file hierarchy.

Key Features

  • Local-First (Ollama): Private, cost-free translation using models like Gemma, Llama 3, or DeepSeek.
  • Syntax Shielding: Automatically protects code blocks (```), inline code (`), LaTeX math ($), and Markdown links from being corrupted by the LLM.
  • Directory Mirroring: Recursively replicates your source folder structure in the output destination.
  • Parallel Processing: Uses ThreadPoolExecutor for high-speed batch handling of large file sets.
  • Smart Caching: Uses hashing to track file changes. Only files that have been modified since the last run are sent to the LLM, saving significant time and compute resources.
  • Resilient File Handling: Automatically pulls required models from Ollama with a real-time progress bar if they are missing.

Quick Usage

Simply provide the input directory and the target output directory.

opentrans ./docs ./translated_docs --config ./config.yaml

Example Output:

Target Language: Chinese
Input:  /home/user/Projects/OpenTrans/docs
Output: /home/user/Projects/OpenTrans/translated_docs
Using Model: translategemma:4b

Translating Files: 100%|████████████████████████████████| 12/12 [00:45<00:00, 3.7s/file]

Complete.

Running Examples

# Translating Docusaurus
opentrans ./Example/Docusaurus/docs ./Example/Docusaurus/i18n/zh-hans/docusaurus-plugin-content-blog/current ./config.yaml    

Installation

Setup

OpenTrans requires Ollama to be installed and running on your local machine.

  1. Install Ollama: Follow instructions at Ollama

  2. Install uv

curl -LsSf https://astral.sh/uv/install.sh | sh
uv tool install .
  1. Install OpenTrans
git clone https://github.com/rainstorm108/OpenTrans.git
cd OpenTrans
uv pip install .
  1. run
opentrans ./docs ./translated_docs --config ./config.yaml

For Developers

  1. Environment Setup
uv sync
source .venv/bin/activte
hatch shell
uv pip install -e .
  1. Running Tests
hatch test

Folder Structure

OpenTrans/
├── src/
│   └── opentrans/
│       ├── __init__.py        
│       ├── main.py            # CLI interface using Click
│       ├── settings.py        # Translate settings
│       ├── translator.py      # Translate logic
│       ├── cache_manager.py   # Manages translation caching using content hashing to skip unchanged files.
│       └── hasher.py          # Hash file for cache
├── tests/
│   ├── test_cache_manager.py
│   ├── test_hasher.py
│   └── test_translator.py
├── config.yaml                # Global settings (model, language, etc.)
├── pyproject.toml
└── README.md 

Workflow

graph TD
    %% Setup Phase
    Start((Start)) --> CheckRunning{Ollama Running?}
    CheckRunning -- No --> Err[Error: Start Ollama]
    CheckRunning -- Yes --> CheckModel{Model Exists?}
    
    CheckModel -- No --> Pull[Download Model with Progress Bar]
    Pull --> Init
    CheckModel -- Yes --> Init[Load Paths & Config]

    %% The Loop
    Init --> NextFile{Next File?}
    NextFile -- No --> Exit([Exit])
    NextFile -- Yes --> IsTranslatable{File Type Supported?}

    %% Branching Logic
    IsTranslatable -- No --> Mirror[Copy Original File]
    IsTranslatable -- Yes --> Shield[Apply Syntax Placeholders]
    
    Shield --> Trans[Translate via Ollama API]
    Trans --> Restore[Restore Protected Code/Math]
    Restore --> Save[Create Dir & Save File]
    Save --> NextFile

Todo

  • Click-based CLI interface
  • Placeholder-based syntax protection (Code/LaTeX)
  • Multi-threaded parallel processing
  • Implement hash caching to skip unchanged files
  • Finish the Docusaurus translate script
  • User Tree-sitter to replace the code blocks before translation instead of regex
  • Add support for more file types
  • ...

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opentrans-0.1.0.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opentrans-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file opentrans-0.1.0.tar.gz.

File metadata

  • Download URL: opentrans-0.1.0.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for opentrans-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8bff70e0e92d1cea7bd092380fbafc2b591ba317c8ed8a71aa0126829a0ce69f
MD5 bfdc0fbdb553fc4bfe1a408caa348b93
BLAKE2b-256 b6d17306b417892f6e74f335389a865809a49fc27f516c71873849b4b0a9cded

See more details on using hashes here.

File details

Details for the file opentrans-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: opentrans-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for opentrans-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba05ac304e648cb0044da8c5cd43b406142a7b680f122f6e9103a9428b8c30c9
MD5 8a4475082e781ae3dcb6fc3b3a745b61
BLAKE2b-256 54c1ee51a2c2b360725ad4fad7835dd6a4fb3f4c742766b7d05dab82d80517c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page