Skip to content

kargnas/laravel-ai-translator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Laravel AI Translator by kargnas

AI-powered translation tool for Laravel language files

Build Status Total Downloads Latest Stable Version License

Official Website

๐Ÿ”„ Recent Updates

  • ๐Ÿ” Parallel Translation: Translate multiple locales concurrently with the translate-parallel command.
  • New Provider: Added Google Gemini support (including the 2.5 models)
  • AI Enhancement: Added support for Claude 3.7's Extended Thinking capabilities
    • Extended context window up to 200K tokens, output tokens up to 64K tokens
    • Enhanced reasoning for complex translations
    • Improved context understanding with extended thinking mode
  • Visual Logging Improvements: Completely redesigned logging system
    • ๐ŸŽจ Beautiful color-coded console output
    • ๐Ÿ“Š Real-time progress indicators
    • ๐Ÿ” Detailed token usage tracking with visual stats
    • ๐Ÿ’ซ Animated status indicators for long-running processes
  • Performance Improvements: Enhanced translation processing efficiency and reduced API calls
  • Better Error Handling: Improved error handling and recovery mechanisms
  • Code Refactoring: Major code restructuring for better maintainability
    • Separated services into dedicated classes
    • Improved token usage tracking and reporting
    • Enhanced console output formatting
  • Testing Improvements: Added comprehensive test suite using Pest
    • XML parsing validation tests
    • Line break handling in CDATA
    • XML comment tag support
    • Multiple translation items processing
  • XML Processing: Enhanced XML and AI response parsing system for more reliable translations

Overview

Laravel AI Translator Example

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, Gemini) 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:

'locale_names' => [
    'en_reddit' => 'English (Reddit)',
],
'additional_rules' => [
    'en_reddit' => [
        "- Incorporate sarcasm and exaggeration",
        "- Use popular internet slang and meme references",
        "- Add humorous calls for sources on obvious statements",
    ],
],

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

  1. Install the package via composer:

    composer require kargnas/laravel-ai-translator
  2. Add the Claude API key to your .env file:

    ANTHROPIC_API_KEY=your-anthropic-api-key-here
    

    You can obtain an API key from the Anthropic Console.

(If you want to use OpenAI's GPT or Google's Gemini instead, see step 4 below for configuration instructions.)

  1. (Optional) Publish the configuration file:

    php artisan vendor:publish --provider="Kargnas\LaravelAiTranslator\ServiceProvider"

    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.

  2. (Optional) The package is configured to use Claude by default. If you want to use OpenAI's GPT or Google's Gemini instead, update the config/ai-translator.php file:

    For OpenAI GPT:

    'ai' => [
        'provider' => 'openai',
        'model' => 'gpt-4o',
        'api_key' => env('OPENAI_API_KEY'),
    ],

    Or for Gemini:

    'ai' => [
        'provider' => 'gemini',
        'model' => 'gemini-2.5-pro-preview-05-06',
        'api_key' => env('GEMINI_API_KEY'),
    ],

    Then, add the OpenAI or Gemini API key to your .env file:

    OPENAI_API_KEY=your-openai-api-key-here
    GEMINI_API_KEY=your-gemini-api-key-here
    

    You can obtain API keys from:

    We strongly recommend using Claude for the best translation quality and accuracy.

  3. You're now ready to use the Laravel AI Translator!

Usage

To translate your language files, run the following command:

php artisan ai-translator:translate

To speed up translating multiple locales, you can run them in parallel:

php artisan ai-translator:translate-parallel

Specify target locales separated by commas using the --locale option. For example:

php artisan ai-translator:translate-parallel --locale=ko,ja

If you omit the --locale option, the command automatically translates all available locales.

This command will:

  1. Recognize all language folders in your lang directory
  2. 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:

<?php
return [
    'expertise' => [
        'coding' => [
            'title' => 'Coding & Product',
            'description' => 'Coding is practically my entire life. I started creating fan sites for Harry Potter and StarCraft when I was 11 years old. Before becoming a university student, I developed various game-related web services such as Nexus TK, TalesWeaver, and MapleStory. During my university days, I earned money by doing part-time jobs creating websites. My current company, OP.GG, is also an extension of the game-related services I\'ve been developing since childhood, which gives me a great sense of pride.',
        ],
    ],
];

The package will generate translations like these:

  • Korean:
    <?php
    return [
        'expertise' => [
            'coding' => [
                'title' => '์ฝ”๋”ฉ & ์ œํ’ˆ',
                'description' => '์ฝ”๋”ฉ์€ ์‚ฌ์‹ค์ƒ ์ œ ์ธ์ƒ ์ „๋ถ€์ž…๋‹ˆ๋‹ค. 11์‚ด ๋•Œ๋ถ€ํ„ฐ ํ•ด๋ฆฌ ํฌํ„ฐ์™€ ์Šคํƒ€ํฌ๋ž˜ํ”„ํŠธ ํŒฌ ์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ•™์ƒ์ด ๋˜๊ธฐ ์ „์— ๋„ฅ์„œ์Šค TK, ํ…Œ์ผ์ฆˆ์œ„๋ฒ„, ๋ฉ”์ดํ”Œ์Šคํ† ๋ฆฌ์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๊ฒŒ์ž„ ๊ด€๋ จ ์›น ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ•™ ์‹œ์ ˆ์—๋Š” ์›น์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ์•„๋ฅด๋ฐ”์ดํŠธ๋กœ ๋ˆ์„ ๋ฒŒ์—ˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์ œ ํšŒ์‚ฌ์ธ OP.GG๋„ ์–ด๋ฆฐ ์‹œ์ ˆ๋ถ€ํ„ฐ ๊ฐœ๋ฐœํ•ด ์˜จ ๊ฒŒ์ž„ ๊ด€๋ จ ์„œ๋น„์Šค์˜ ์—ฐ์žฅ์„ ์œผ๋กœ, ์ด๋Š” ์ €์—๊ฒŒ ํฐ ์ž๋ถ€์‹ฌ์„ ์ค๋‹ˆ๋‹ค.',
            ]
        ]
    ];
  • Simplified Chinese:
    <?php
    return [
        'expertise' => [
            'coding' => [
                'title' => '็ผ–็จ‹ไธŽไบงๅ“',
              'description' => '็ผ–็จ‹ๅ‡ ไนŽๅฐฑๆ˜ฏๆˆ‘็š„ๆ•ดไธช็”Ÿๆดปใ€‚11ๅฒๆ—ถ๏ผŒๆˆ‘ๅฐฑๅผ€ๅง‹ไธบใ€Šๅ“ˆๅˆฉยทๆณข็‰นใ€‹ๅ’Œใ€Šๆ˜Ÿ้™…ไบ‰้œธใ€‹ๅˆ›ๅปบ็ฒ‰ไธ็ฝ‘็ซ™ใ€‚ๅœจไธŠๅคงๅญฆไน‹ๅ‰๏ผŒๆˆ‘ๅผ€ๅ‘ไบ†ๅ„็งๆธธๆˆ็›ธๅ…ณ็ฝ‘็ปœๆœๅŠก๏ผŒๅฆ‚Nexus TKใ€TalesWeaverๅ’Œๅ†’้™ฉๅฒ›ใ€‚ๅคงๅญฆๆœŸ้—ด๏ผŒๆˆ‘้€š่ฟ‡ๅˆ›ๅปบ็ฝ‘็ซ™็š„ๅ…ผ่Œๅทฅไฝœ่ตš้’ฑใ€‚ๆˆ‘็Žฐๅœจ็š„ๅ…ฌๅธOP.GG๏ผŒไนŸ  ๆ˜ฏๆˆ‘ไปŽๅฐๅฐฑๅœจๅผ€ๅ‘็š„ๆธธๆˆ็›ธๅ…ณๆœๅŠก็š„ๅปถไผธ๏ผŒ่ฟ™่ฎฉๆˆ‘ๆ„Ÿๅˆฐ้žๅธธ่‡ช่ฑชใ€‚'
            ]
        ]
    ];
  • Thai:
    <?php
    return [
        'expertise' => [
            'coding' => [
                'title' => 'Coding & Product',
                'description' => 'เธเธฒเธฃเน€เธ‚เธตเธขเธ™เน‚เธ„เน‰เธ”เน€เธ›เน‡เธ™เน€เธฃเธทเนˆเธญเธ‡เธ—เธตเนˆเธญเธขเธนเนˆเนƒเธ™เธŠเธตเธงเธดเธ•เธœเธกเนเธ—เธšเธ—เธฑเน‰เธ‡เธซเธกเธ” เธœเธกเน€เธฃเธดเนˆเธกเธชเธฃเน‰เธฒเธ‡เน€เธงเน‡เธšเน„เธ‹เธ•เนŒเนเธŸเธ™เธ„เธฅเธฑเธšเธชเธณเธซเธฃเธฑเธšเนเธฎเธฃเนŒเธฃเธตเนˆ เธžเธญเธ•เน€เธ•เธญเธฃเนŒเนเธฅเธฐเธชเธ•เธฒเธฃเนŒเธ„เธฃเธฒเธŸเธ—เนŒเธ•เธฑเน‰เธ‡เนเธ•เนˆเธญเธฒเธขเธธ 11 เธ›เธต เธเนˆเธญเธ™เธ—เธตเนˆเธˆเธฐเน€เธ‚เน‰เธฒเธกเธซเธฒเธงเธดเธ—เธขเธฒเธฅเธฑเธข เธœเธกเน„เธ”เน‰เธžเธฑเธ’เธ™เธฒเน€เธงเน‡เธšเธšเธฃเธดเธเธฒเธฃเน€เธเธตเนˆเธขเธงเธเธฑเธšเน€เธเธกเธ•เนˆเธฒเธ‡เน† เน€เธŠเนˆเธ™ Nexus TK, TalesWeaver เนเธฅเธฐ MapleStory เนƒเธ™เธŠเนˆเธงเธ‡เธกเธซเธฒเธงเธดเธ—เธขเธฒเธฅเธฑเธข เธœเธกเธซเธฒเน€เธ‡เธดเธ™เธ”เน‰เธงเธขเธเธฒเธฃเธ—เธณเธ‡เธฒเธ™เธžเธฒเธฃเนŒเธ—เน„เธ—เธกเนŒเธชเธฃเน‰เธฒเธ‡เน€เธงเน‡เธšเน„เธ‹เธ•เนŒ เธšเธฃเธดเธฉเธฑเธ—เธ›เธฑเธˆเธˆเธธเธšเธฑเธ™เธ‚เธญเธ‡เธœเธก OP.GG เธเน‡เน€เธ›เน‡เธ™เธชเนˆเธงเธ™เธ‚เธขเธฒเธขเธ‚เธญเธ‡เธšเธฃเธดเธเธฒเธฃเน€เธเธตเนˆเธขเธงเธเธฑเธšเน€เธเธกเธ—เธตเนˆเธœเธกเน„เธ”เน‰เธžเธฑเธ’เธ™เธฒเธกเธฒเธ•เธฑเน‰เธ‡เนเธ•เนˆเน€เธ”เน‡เธ เธ‹เธถเนˆเธ‡เธ—เธณเนƒเธซเน‰เธœเธกเธฃเธนเน‰เธชเธถเธเธ เธนเธกเธดเนƒเธˆเน€เธ›เน‡เธ™เธญเธขเนˆเธฒเธ‡เธกเธฒเธ',
            ]
        ]
    ];
  • Japanese:
    <?php
    return [
        'expertise' => [
            'coding' => [
                'title' => 'ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐ๏ผ†ใƒ—ใƒญใƒ€ใ‚ฏใƒˆ',
                'description' => 'ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใฏ็งใฎไบบ็”Ÿใใฎใ‚‚ใฎใงใ™ใ€‚11ๆญณใฎๆ™‚ใ‹ใ‚‰ใƒใƒชใƒผใƒใƒƒใ‚ฟใƒผใ‚„ใ‚นใ‚ฟใƒผใ‚ฏใƒฉใƒ•ใƒˆใฎใƒ•ใ‚กใƒณใ‚ตใ‚คใƒˆใ‚’ไฝœใ‚Šๅง‹ใ‚ใพใ—ใŸใ€‚ๅคงๅญฆ็”Ÿใซใชใ‚‹ๅ‰ใซใ€Nexus TKใ€ใƒ†ใ‚คใƒซใ‚บใ‚ฆใ‚ฃใƒผใƒใƒผใ€ใƒกใ‚คใƒ—ใƒซใ‚นใƒˆใƒผใƒชใƒผใชใฉใฎใ‚ฒใƒผใƒ ้–ข้€ฃใฎใ‚ฆใ‚งใƒ–ใ‚ตใƒผใƒ“ใ‚นใ‚’้–‹็™บใ—ใฆใ„ใพใ—ใŸใ€‚ๅคงๅญฆๆ™‚ไปฃใซใฏใ€ใ‚ฆใ‚งใƒ–ใ‚ตใ‚คใƒˆๅˆถไฝœใฎใ‚ขใƒซใƒใ‚คใƒˆใงใŠ้‡‘ใ‚’็จผใ„ใงใ„ใพใ—ใŸใ€‚็พๅœจใฎไผš็คพOP.GGใ‚‚ใ€ๅญไพ›ใฎ้ ƒใ‹ใ‚‰้–‹็™บใ—ใฆใใŸใ‚ฒใƒผใƒ ้–ข้€ฃใ‚ตใƒผใƒ“ใ‚นใฎๅปถ้•ท็ทšไธŠใซใ‚ใ‚Šใ€ใใ‚ŒใŒๅคงใใช่ช‡ใ‚Šใจใชใฃใฆใ„ใพใ™ใ€‚',
            ]
        ]
    ];
  • ๐Ÿคฃ Simplified Chinese (Dongbei Dialect):
    <?php
    return [
        'expertise' => [
            'coding' => [
                'title' => 'ๅ†™ไปฃ็ ๅ’Œๆ•ดไบงๅ“',
                'description' => 'ๅ†™ไปฃ็ ็ฎ€็›ดๅฐฑๆ˜ฏไฟบ็š„ๅ‘ฝๆ นๅญใ€‚ไฟบ11ๅฒ้‚ฃไผšๅ„ฟๅฐฑๅผ€ๅง‹ๆ•ดๅ“ˆๅˆฉๆณข็‰นๅ’Œๆ˜Ÿ้™…ไบ‰้œธ็š„็ฒ‰ไธ็ฝ‘็ซ™ไบ†ใ€‚ไธŠๅคงๅญฆๅ‰๏ผŒไฟบๆ•ดไบ†ไธๅฐ‘ๆธธๆˆ็ฝ‘็ซ™๏ผŒๅ•ฅNexus TKๅ•Š๏ผŒTalesWeaverๅ•Š๏ผŒ่ฟ˜ๆœ‰ๅ†’้™ฉๅฒ›ๅ•Šใ€‚ไธŠๅคงๅญฆ้‚ฃไผšๅ„ฟ๏ผŒไฟบ้ ็ป™ไบบๆ•ด็ฝ‘็ซ™ๆŒฃไบ†ไธๅฐ‘้›ถ่Šฑ้’ฑใ€‚็Žฐๅœจไฟบ่ฟ™ๅ…ฌๅธOP.GG๏ผŒไนŸๅฐฑๆ˜ฏไฟบไปŽๅฐๆ•ด็š„ๆธธๆˆๆœๅŠก็š„ๅปถ็ปญ๏ผŒ่ฟ™่ฎฉไฟบ่ดผๆœ‰ๆˆๅฐฑๆ„Ÿใ€‚'
            ]
        ]
    ];
  • ๐Ÿคฃ Korean (North Korea):
    <?php
    return [
        'expertise' => [
            'coding' => [
                'title' => '์ฝคํ“จํ„ฐํ”„๋กœ๊ทธ๋žŒ์ž‘์„ฑ & ์ œํ’ˆ',
                'description' => 'ํ”„๋กœ๊ทธ๋žŒ์ž‘์„ฑ์€ ํ˜๋ช…์ ์œผ๋กœ ๋‚˜์˜ ์ „์ƒ์• ์ž…๋‹ˆ๋‹ค. 11์‚ด๋•Œ๋ถ€ํ„ฐ ํ•ด๋ฆฌ ํฌํ„ฐ์™€ ์Šคํƒ€ํฌ๋ž˜ํ”„ํŠธ ์• ํ˜ธ๊ฐ€์‹ธ์ดํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์‹œ์ž‘ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋Œ€ํ•™์ƒ์ด ๋˜๊ธฐ์ „์— ๋„ฅ์„œ์Šค TK, ํ…Œ์ผ์ฆˆ์œ„๋ฒ„, ๋ฉ”์ดํ”Œ์Šคํ† ๋ฆฌ์™€ ๊ฐ™์€ ํ˜๋ช…์ ์ธ ๊ฒŒ์ž„๊ด€๋ จ ๋ง๋ด‰์‚ฌ๋“ค์„ ๊ฐœ๋ฐœํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋Œ€ํ•™์‹œ์ ˆ์—๋Š” ์›น์‹ธ์ดํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฆผ์‹œ์ผ๋กœ ๋ˆ์„ ๋ฒŒ์—ˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ๋‚˜์˜ ํšŒ์‚ฌ์ธ OP.GG๋„ ์–ด๋ฆฐ์‹œ์ ˆ๋ถ€ํ„ฐ ๊ฐœ๋ฐœํ•˜์—ฌ์˜จ ๊ฒŒ์ž„๊ด€๋ จ ๋ด‰์‚ฌ์˜ ๋ จ์žฅ์„ ์œผ๋กœ, ์ด๋Š” ๋‚˜์—๊ฒŒ ํฐ ํ˜๋ช…์ ์ž๋ถ€์‹ฌ์„ ์ค๋‹ˆ๋‹ค.',
            ]
        ]
    ];
  • ๐Ÿคฃ Korean (Busan Dialect):
    <?php
    return [
        'expertise' => [
            'coding' => [
                'title' => '์ฝ”๋”ฉ & ์ œํ’ˆ',
                'description' => '์ฝ”๋”ฉ์€ ์‚ฌ์‹ค์ƒ ์ œ ์ธ์ƒ ์ „๋ถ€๋ผ ์นด๋ชจ. 11์‚ด ๋•Œ๋ถ€ํ„ฐ ํ•ด๋ฆฌ ํฌํ„ฐ์บ‰ ์Šคํƒ€ํฌ๋ž˜ํ”„ํŠธ ํŒฌ ์‚ฌ์ดํŠธ ๋งŒ๋“ค๊ธฐ ์‹œ์ž‘ํ–ˆ๋ฐ์ด. ๋Œ€ํ•™์ƒ์ด ๋˜๊ธฐ ์ „์—๋Š” ๋„ฅ์„œ์Šค TK, ํ…Œ์ผ์ฆˆ์œ„๋ฒ„, ๋ฉ”์ดํ”Œ์Šคํ† ๋ฆฌ ๊ฐ™์€ ์—ฌ๋Ÿฌ ๊ฒŒ์ž„ ๊ด€๋ จ ์›น ์„œ๋น„์Šค ๊ฐœ๋ฐœํ–ˆ๋””. ๋Œ€ํ•™ ๋‹ค๋‹ ๋•Œ๋Š” ์›น์‚ฌ์ดํŠธ ๋งŒ๋“œ๋Š” ์•„๋ฅด๋ฐ”์ดํŠธ๋กœ ๋ˆ ๋ฒŒ์—ˆ๋ฐ์ด. ์ง€๊ธˆ ์ œ ํšŒ์‚ฌ์ธ OP.GG๋„ ์–ด๋ฆด ๋•Œ๋ถ€ํ„ฐ ๊ฐœ๋ฐœํ•ด ์˜จ ๊ฒŒ์ž„ ๊ด€๋ จ ์„œ๋น„์Šค์œผ ์—ฐ์žฅ์„ ์ธ๋ฐ, ์ด๊ธฐ ๋‚ด์ธ๋ฐ ์–ต์ˆ˜๋กœ ์ž๋ถ€์‹ฌ์ด ๋œ๋‹ค ์นด๋ชจ.',
            ]
        ]
    ];
  • ๐Ÿคฃ English (Reddit):
    <?php
    return [
        'expertise' => [
            'coding' => [
                'title' => 'Coding & Product',
                'description' => 'Coding is practically my entire life, duh. Started building Harry Potter and StarCraft fan sites at 11 (yeah, I was *that* kid). Before even hitting university, I was already knee-deep in game sites like Nexus TK, TalesWeaver, and MapleStory. Paid my way through college building websites - because who needs a social life, right? Now I run OP.GG, which is basically just the grown-up version of what little-me was doing in his bedroom. Not gonna lie, pretty damn proud of that full-circle moment.',
            ]
        ]
    ];

Configuration

If you want to customize the settings, you can publish the configuration file:

php artisan vendor:publish --provider="Kargnas\LaravelAiTranslator\ServiceProvider"

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 default lang directory, you can specify it here.

  • ai: Configure the AI provider and model:

    'ai' => [
        'provider' => 'anthropic',
        'model' => 'claude-sonnet-4-20250514',
        'api_key' => env('ANTHROPIC_API_KEY'),
    ],

    This package supports Anthropic's Claude, Google's Gemini, and OpenAI's GPT models for translations. Here are the tested and verified models:

    Provider Model Extended Thinking Context Window Max Tokens
    anthropic claude-sonnet-4-20250514 โœ… 200K 8K/64K*
    anthropic claude-3-7-sonnet-latest โœ… 200K 8K/64K*
    anthropic claude-3-7-sonnet-latest โŒ 200K 8K
    anthropic claude-3-haiku-20240307 โŒ 200K 8K
    openai gpt-4o โŒ 128K 4K
    openai gpt-4o-mini โŒ 128K 4K
    gemini gemini-2.5-pro-preview-05-06 โŒ 1000K 64K
    gemini gemini-2.5-flash-preview-04-17 โŒ 1000K 64K

    * 8K tokens for normal mode, 64K tokens when extended thinking is enabled

    For available models:

    โญ๏ธ Strong Recommendation: We highly recommend using Anthropic's Claude models, particularly claude-sonnet-4-20250514 or claude-3-7-sonnet-latest. Here's why:

    • More accurate and natural translations
    • Better understanding of context and nuances
    • More consistent output quality
    • More cost-effective for the quality provided
    • Claude 4.0 offers even better reasoning and translation quality

    While OpenAI integration is available, we strongly advise against using it for translations. Our extensive testing has shown that Claude models consistently produce superior results for localization tasks.

    Provider Setup

    1. Get your API key:

    2. Add to your .env file:

      # For Anthropic
      ANTHROPIC_API_KEY=your-api-key
      
      # For OpenAI
      OPENAI_API_KEY=your-api-key
      
      # For Gemini
      GEMINI_API_KEY=your-api-key
    3. Configure in config/ai-translator.php:

      'ai' => [
         'provider' => 'anthropic', // or 'openai' or 'gemini'
         'model' => 'claude-sonnet-4-20250514', // see model list above
         'api_key' => env('ANTHROPIC_API_KEY'), // or env('OPENAI_API_KEY') or env('GEMINI_API_KEY')
      ],
  • 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.

  • disable_plural: Disable pluralization. Use ":count apples" instead of ":count apple|:count apples"

Example configuration:

<?php

return [
    'source_directory' => 'lang',

    'ai' => [
        'provider' => 'anthropic',
        'model' => 'claude-sonnet-4-20250514',
        'api_key' => env('ANTHROPIC_API_KEY'),
    ],

    'locale_names' => [
        'en' => 'English',
        'ko' => 'Korean',
        'zh_cn' => 'Chinese (Simplified)',
        // ... other locales
    ],

    'disable_plural' => false,

    'additional_rules' => [
        'default' => [
            "Use a friendly and intuitive tone of voice, like the service tone of voice of 'Discord'.",
        ],
        'ko' => [
            "ํ•œ๊ตญ์˜ ์ธํ„ฐ๋„ท ์„œ๋น„์Šค 'ํ† ์Šค'์˜ ์„œ๋น„์Šค ๋งํˆฌ ์ฒ˜๋Ÿผ, ์œ ์ €์—๊ฒŒ ์นœ๊ทผํ•˜๊ณ  ์ง๊ด€์ ์ธ ๋งํˆฌ๋กœ ์„ค๋ช…ํ•˜๊ณ  ์กด๋Œ“๋ง๋กœ ์„ค๋ช…ํ•˜์„ธ์š”.",
        ],
    ],
];

Make sure to set your chosen AI provider's API key in your .env file.

Supported File Types

This package supports both PHP and JSON language files used by Laravel:

PHP Language Files

php artisan ai-translator:translate
php artisan ai-translator:translate-parallel

These commands translate PHP language files located in subdirectories like lang/en/, lang/ko/, etc.

JSON Language Files

php artisan ai-translator:translate-json

This command translates root-level JSON language files like lang/en.json, lang/ko.json, etc.

All translation commands support the same powerful features:

  • Interactive language selection: Choose source and target languages interactively
  • Reference language support: Use high-quality translations as reference for better results
  • Chunking: Process multiple strings in batches for cost-effective API usage
  • Progress tracking: Real-time progress indicators with colorful console output
  • Token usage monitoring: Track and display API token consumption and costs
  • Context awareness: Maintain translation consistency across files

Command Options

All translation commands support these options:

  • --source=LOCALE: Source language (e.g., --source=en)
  • --locale=LOCALE1,LOCALE2: Target locales (e.g., --locale=ko,ja)
  • --reference=LOCALE1,LOCALE2: Reference languages for guidance (e.g., --reference=fr,es)
  • --chunk=SIZE: Chunk size for batch processing (default: 100)
  • --max-context=COUNT: Maximum context items (default: 1000)
  • --force-big-files: Force translation of files with 500+ strings
  • --show-prompt: Display AI prompts during translation
  • --non-interactive: Run without interactive prompts

File Structure Examples

PHP Files:

lang/
โ”œโ”€โ”€ en/
โ”‚   โ”œโ”€โ”€ auth.php
โ”‚   โ”œโ”€โ”€ validation.php
โ”‚   โ””โ”€โ”€ messages.php
โ”œโ”€โ”€ ko/
โ”‚   โ”œโ”€โ”€ auth.php
โ”‚   โ”œโ”€โ”€ validation.php
โ”‚   โ””โ”€โ”€ messages.php

JSON Files:

lang/
โ”œโ”€โ”€ en.json
โ”œโ”€โ”€ ko.json
โ”œโ”€โ”€ ja.json
โ””โ”€โ”€ fr.json

Why Support Both Formats?

PHP Files Benefits:

  • Nested array structure for better organization
  • Support for comments and context
  • Slightly better performance
  • More flexibility for complex translations

JSON Files Benefits:

  • Simpler flat structure
  • Easier to edit manually
  • Better for frontend JavaScript integration
  • Widely supported format

Choose the format that best fits your project's needs - this package handles both seamlessly!

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
    • Check for consistency in pluralization rules across translations
  • Expand support for other LLMs (such as Gemini)
  • Replace regex-based XML parser with proper XML parsing:
    • Better handle edge cases and malformed XML

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

Read my articles about language