AI Translator
An AI-powered translation tool that uses OpenAI's GPT models to translate text and files between different languages.
Features
- Text translation
- File translation
- Folder translation
- Support for multiple languages
- Customizable language configuration
- JSON configuration support
- Support for different GPT model settings
Installation
pip install ai-translator
Usage
Basic Text Translation
from translator import Translator
translator = Translator(api_key="your-openai-api-key")
# Translate text
text = "Hello, world!"
result = translator.translate(text, target_language="zh-tw")
print(result) # 你好,世界!
Advanced Initialization Options
# Using different models and parameters
translator = Translator(
api_key="your-openai-api-key",
model="gpt-4o-mini", # Default model
temperature=0.0, # Controls creativity, 0 for most deterministic output
max_tokens=1000 # Maximum output length
)
# View currently available languages
for code, name in translator.available_languages.items():
print(f"{code}: {name}")
File Translation
# Translate a single file
translator.translate_file(
file_path="input.txt",
target_language="es",
output_path="output.txt"
)
# Translate all files in a folder
translator.translate_folder(
folder_path="input_folder",
target_language="ja",
output_dir="output_folder",
file_extension=".txt",
recursive=True # Set to True to process subdirectories
)
Custom Language Configuration
# Using default configuration
translator = Translator(api_key="your-openai-api-key")
# Using custom configuration
custom_config = {
"zh-tw": "繁體中文",
"en": "English",
"fr": "French"
}
translator = Translator(api_key="your-openai-api-key", language_config=custom_config)
# Update configuration of an existing instance (after initialization)
translator.config(
model="gpt-4",
temperature=0.3,
max_tokens=2000,
language_config=custom_config
)
# Update language configuration from JSON file
translator.update_language_config_from_json("languages.json")
Configuration
Default Supported Languages
- zh-tw: 繁體中文
- en: English
- es: Español
- ja: 日本語
- ko: 한국어
JSON Configuration File Format
{
"zh-tw": "繁體中文",
"en": "English",
"fr": "French",
"de": "Deutsch"
}
Error Handling
The translation functions include robust error handling mechanisms for the following situations:
- Files or directories don't exist
- JSON configuration file format errors
- API call failures
- File read/write errors
Error messages are printed via standard output, and translation methods return None when errors occur.
Requirements
- Python 3.8 or higher
- OpenAI API key
- openai>=1.65.0
- python-dotenv>=1.0.0
License
Metadata
Release files for ai-translator 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ai_translator-0.1.0.tar.gz | 6.2 kB | Without provenance | Details for ai_translator-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ai_translator-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for ai_translator-0.1.0-py3-none-any.whl |
Total release size: 12.9 kB
Release files / ai_translator-0.1.0.tar.gz
| Download URL | ai_translator-0.1.0.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4b1dfe0e4555061fc283be240f94d58ceb70b1549d34458892af2c5c2568e2b2
|
|
BLAKE2b-256 checksum How to use checksums |
85c691d9881acd9642a0ef38d506ac720ddfc6d34d81366cb580e12275c87b3a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / ai_translator-0.1.0-py3-none-any.whl
| Download URL | ai_translator-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e344555aedf6a095ac491a1f1da21cc22c955b01bff6efb0f9587d3a4c8938d7
|
|
BLAKE2b-256 checksum How to use checksums |
632440ee150426c8ebdf3040b5ba04b8b5335380440c4f2544e1032282cd44ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Security for ai-translator 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.