A CLI Tool for Local File Translation
Project description
ailingo: A CLI Tool for Local File Translation Using Generative AI
ailingo is a command-line interface (CLI) tool that utilizes the power of generative AI to translate local files into various languages.
Overview
This tool is designed to help developers, translators, and content creators efficiently localize their files into multiple languages.
Key Features:
- Flexible file handling: Translate multiple files at once.
- Wide language support: Freely specify the source and target languages for translation.
- Generative AI model selection: Choose from various generative AI models available through litellm, such as ChatGPT, Gemini, and Anthropic.
- Customizable output: Control the name and location of the translated files.
- Additional translation requests: You can add requests for nuances in translation, such as a casual tone.
- Rewrite mode: You can rewrite the text in the same language with spell/grammar correction or adjust the writing style with a request.
- Editor mode: You can translate text directly in an editor.
Installation
Prerequisites:
- Python 3.11
Steps:
- Setting up litellm:
This program utilizes LiteLLM to access generative AI. LiteLLM is designed to work with various providers. Create an account with your preferred generative AI model provider and obtain an API key.
For detailed setup instructions, please refer to the LiteLLM documentation. Below are some common API key settings:
# Default: OpenAI (e.g., gpt-4)
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
# VertexAI (e.g., Gemini)
# Run "gcloud auth application-default login" or set GOOGLE_APPLICATION_CREDENTIALS
export VERTEXAI_PROJECT="your-google-project-id"
export VERTEXAI_LOCATION="us-central1"
# Anthropic (e.g., haiku, opus, sonnet)
export ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY"
- Installing ailingo:
pip install ailingo
# To use VertexAI (e.g., Gemini)
pip install 'ailingo[google]'
# To use AWS (Bedrock)
pip install 'ailingo[aws]'
# Or install all dependencies
pip install 'ailingo[all]'
Usage
Basic translation:
ailingo <file_path> --target <target_language>
Example:
ailingo my_document.txt --target ja
This translates my_document.txt
into Japanese and saves it as my_document.ja.txt
. By default, it saves the translated file in the same folder in the format {stem}.{target}{suffix}
.
Filename estimation by specifying the source language:
ailingo /path/to/en/my_document.txt --source en --target ja
This translates my_document.txt
into Japanese and saves it as /path/to/ja/my_document.txt
. This feature replaces the source language code with the target language code when the file name or directory name contains the source language code.
- Example:
document.en.txt
→document.ja.txt
- Example:
locales/en/LC_MESSAGES/message.po
→locales/ja/LC_MESSAGES/message.po
Note: If you specify the output file name pattern with the --output
option, this automatic estimation will not be applied.
Multiple files and target languages:
ailingo file1.txt file2.html --target ja,es,fr
This translates file1.txt
and file2.html
into Japanese, Spanish, and French.
Adding a translation request:
ailingo my_document.txt --target de --request "Please translate with a casual tone, including jokes."
This translates my_document.txt
into German with a casual tone, including jokes.
Rewrite mode: Correcting spelling/grammar or adjusting writing style
ailingo my_document.txt
If you don't specify the target language, it will rewrite the existing my_document.txt
in the same language. You can specify other options in the same way as for translation.
By default, it will correct spelling and grammar errors, but you can add more specific requests using the --request
option.
Editor mode: Translate without specifying a file
ailingo -e
In editor mode, a temporary file is opened in an editor (default: vi), allowing you to manually edit the text before translation. After saving, the content will be translated.
You can combine other options with editor mode.
- Specify the target language with
--target
. - Add a request for adjusting writing style with
--request
. - By default, the translation result is printed to standard output, but you can specify an output file with
--output
.
Specifying the generative AI model:
ailingo my_document.txt --target de --model gemini-1.5-pro
This translates my_document.txt
into German using Google Gemini Pro.
Customizing the output file name:
ailingo my_document.txt --target es --output "{parent}/{stem}_translated.{target}{suffix}"
This translates my_document.txt
into Spanish and saves it as my_document_translated.es.txt
.
ailingo /path/to/en/my_document.txt --target ja --output "{parents[1]}/{target}/{name}"
This translates path/to/en/my_document.txt
into Japanese and saves it as path/to/ja/my_document.txt
.
The string is interpreted by the format function. The following variables are available:
Variable name | Value | Type | Example |
---|---|---|---|
{stem} |
Part of the input file name excluding the extension | str | my_document |
{suffix} |
Extension of the input file (including the dot) | str | .txt |
{suffixes} |
List of extensions of the input file (including the dot) | list[str] | [ '.ja', '.txt' ] |
{name} |
File name of the input file including the extension | str | my_document.txt |
{parent} |
Parent directory of the input file | str | /path/to/en |
{parents} |
List of parent directories of the input file | list[str] | ['/path/to', '/path'] |
{target} |
Target language | str | ja |
{source} |
Source language (only if specified) | Optional[str] | en |
For other properties, please refer to the Pathlib documentation.
Detailed options:
For more advanced usage, please use the help command:
ailingo --help
License
This project is distributed under the MIT License.
Disclaimer
This tool leverages the power of generative AI, but the quality of the translation may vary depending on the selected AI model and the input text. It is recommended to review and revise the translation results as needed.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.