Download the PHP package kargnas/laravel-ai-translator without Composer
On this page you can find all versions of the php package kargnas/laravel-ai-translator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kargnas/laravel-ai-translator
More information about kargnas/laravel-ai-translator
Files in kargnas/laravel-ai-translator
Package laravel-ai-translator
Short Description AI-powered translation tool for Laravel language files
License MIT
Homepage https://github.com/kargnas/laravel-ai-translate
Informations about the package laravel-ai-translator
kargnas/laravel-ai-translator
AI-powered translation tool for Laravel language files
💡 New Feature: Custom Language Styles
We've expanded our capabilities with support for custom language styles, allowing for unique and creative translations. Learn more about Custom Language Styles
Overview
Laravel AI Translator is a powerful tool designed to streamline the localization process in Laravel projects. It automates the tedious task of translating strings across multiple languages, leveraging advanced AI models to provide high-quality, context-aware translations.
Key benefits:
- Time-saving: Translate all your language files with one simple command
- AI-powered: Utilizes state-of-the-art language models (GPT-4, GPT-4o, GPT-3.5, Claude) for superior translation quality
- Smart context understanding: Accurately captures nuances, technical terms, and Laravel-specific expressions
- Seamless integration: Works within your existing Laravel project structure, preserving complex language file structures
Whether you're working on a personal project or a large-scale application, Laravel AI Translator simplifies the internationalization process, allowing you to focus on building great features instead of wrestling with translations.
Key Features
- Automatically detects all language folders in your
lang
directory - Translates PHP language files from a source language (default: English) to all other languages
- Supports multiple AI providers for intelligent, context-aware translations
- Preserves variables, HTML tags, pluralization codes, and nested structures
- Maintains consistent tone and style across translations
- Supports custom translation rules for enhanced quality and project-specific requirements
- Efficiently processes large language files, saving time and effort
- Respects Laravel's localization system, ensuring compatibility with your existing setup
- Chunking functionality for cost-effective translations: Processes multiple strings in a single AI request, significantly reducing API costs and improving efficiency
- String validation to ensure translation accuracy: Automatically checks and validates AI translations to catch and correct any errors or mistranslations
Also, this tool is designed to translate your language files intelligently:
- Contextual Understanding: Analyzes keys to determine if they represent buttons, descriptions, or other UI elements.
- Linguistic Precision: Preserves word forms, tenses, and punctuation in translations.
- Variable Handling: Respects and maintains your language file variables during translation.
- Smart Length Adaptation: Adjusts translation length to fit UI constraints where possible.
- Tone Consistency: Maintains a consistent tone across translations, customizable via configuration.
Do you want to know how this works? See the prompt in src/AI
.
Custom Language Styles
In addition to standard language translations, this package now supports custom language styles, allowing for unique and creative localizations.
Built-in Styles
The package includes several built-in language styles:
ko_kp
: North Korean style Korean- Various regional dialects and language variants
These are automatically available and don't require additional configuration.
Custom Style Example: Reddit English
As an demonstration of custom styling capabilities, we've implemented a "Reddit style" English:
This style mimics the casual, often humorous language found on Reddit, featuring:
- Liberal use of sarcasm
- Internet slang and meme references
- Playful skepticism
Example configuration:
Creating Custom Styles
You can create your own custom language styles by adding new entries to the locale_names
and additional_rules
in the configuration. This allows you to tailor translations to specific audiences or platforms.
These custom styles offer creative ways to customize your translations, adding a unique flair to your localized content. Use responsibly to enhance user engagement while maintaining clarity and appropriateness for your audience.
Prerequisites
- PHP 8.0 or higher
- Laravel 8.0 or higher
Installation
-
Install the package via composer:
-
Add the OpenAI API key to your
.env
file:You can obtain an API key from the OpenAI website.
(If you want to use Anthropic's Claude instead, see step 4 below for configuration instructions.)
-
(Optional) Publish the configuration file:
This step is optional but recommended if you want to customize the package's behavior. It will create a
config/ai-translator.php
file where you can modify various settings. -
(Optional) If you want to use Anthropic's Claude instead of OpenAI's GPT, update the
config/ai-translator.php
file:Then, add the Anthropic API key to your
.env
file:You can obtain an Anthropic API key from the Anthropic website. For best results, we recommend using the Claude-3-5-Sonnet model for your translations rather than OpenAI GPT. This model provides more accurate and natural translations.
- You're now ready to use the Laravel AI Translator!
Usage
To translate your language files, run the following command:
This command will:
- Recognize all language folders in your
lang
directory - Use AI to translate the contents of the string files in the source language, English. (You can change the source language in the config file)
Example
Given an English language file:
The package will generate translations like these:
-
Korean (ko-kr):
-
Chinese (zh-cn):
-
Thai (th-th):
-
🤣 Korean (North Korea):
- 🤣 English (Reddit):
Configuration
If you want to customize the settings, you can publish the configuration file:
This will create a config/ai-translator.php
file where you can modify the following settings:
-
source_directory
: If you use a different directory for language files instead of the defaultlang
directory, you can specify it here. -
ai
: Configure the AI provider, model, and API key here. Here are our recommendation for the best models:Provider Model Cost (I/O per 1M tokens) Descrpition anthropic claude-3-5-sonnet-20240620 $3.00 / $15.00 The best quality, little bit slow. We recommend. anthropic claude-3-haiku-20240307 $0.25 / $1.25 Low quality, but better than gpt-3.5 openai gpt-4o $5.00 / $15.00 Balanced quality and high speed openai gpt-4o-mini $0.15 / $0.60 Balanced quality and cheap Here are not recommended models which are expensive or low quality:
Provider Model Cost (I/O per 1M tokens) anthropic claude-3-opus-20240229 $15.00 / $75.00 anthropic claude-3-sonnet-20240229 $3.00 / $15.00 openai gpt-4-turbo $10.0 / $30.0 openai gpt-3.5-turbo $0.50 / $1.50 -
locale_names
: This mapping of locale codes to language names enhances translation quality by providing context to the AI. additional_rules
: Add custom rules to the translation prompt. This is useful for customizing the style of the messages or creating entirely new language styles.
Example configuration:
Make sure to set your chosen AI provider's API key in your .env
file.
Supported File Types
Currently, this package only supports PHP language files used by Laravel. JSON language files are not supported, and there are no plans to add support for them in the future.
Why PHP files only?
We recommend using PHP files for managing translations, especially when dealing with multiple languages. Here's why:
-
Structure: PHP files allow for a more organized structure with nested arrays, making it easier to group related translations.
-
Comments: You can add comments in PHP files to provide context or instructions for translators.
-
Performance: PHP files are slightly faster to load compared to JSON files, as they don't require parsing.
-
Flexibility: PHP files allow for more complex operations, such as using variables or conditions in your translations.
- Scalability: When managing a large number of translations across multiple languages, the directory structure of PHP files makes it easier to navigate and maintain.
If you're currently using JSON files for your translations, we recommend migrating to PHP files for better compatibility with this package and improved manageability of your translations.
AI Service
This package supports both OpenAI's GPT models and Anthropic's Claude for translations, each with its own strengths:
- OpenAI
- Anthropic
TODO List
We're constantly working to improve Laravel AI Translator. Here are some features and improvements we're planning:
- [ ] Implement strict validation for translations:
- Verify that variables are correctly preserved in translated strings
- Ensure placeholders and Laravel-specific syntax are maintained
- Check for consistency in pluralization rules across translations
- [ ] Write test code to ensure reliability and catch potential issues
- [ ] Implement functionality to maintain the array structure of strings during translation
- [ ] Expand support for other LLMs (such as Gemini)
If you'd like to contribute to any of these tasks, please feel free to submit a pull request!
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
The MIT License (MIT). Please see License File for more information.
Credits
- Created by Sangrak Choi
- Inspired by Mandarin Study
All versions of laravel-ai-translator with dependencies
crowdin/crowdin-api-client Version ^1.13
guzzlehttp/guzzle Version ^7.0.1
illuminate/support Version ^8.0|^9.0|^10.0|^11.0
kargnas/instructrice Version ^1.0