iperamuna/laravel-auto-translations

Auto-generate JSON language files, AI translations, and a local-only language browser.

v0.0.1 2025-07-16 10:00 UTC

This package is auto-updated.

Last update: 2025-07-16 10:03:20 UTC


README

Latest Version on Packagist Total Downloads License

✨ Automatically scan your Laravel app for translation keys, export them to JSON, generate translations via OpenAI, and preview them in a simple local browser UI.

🚀 Features

  • 🔍 Scan your entire Laravel app for __('...') usage
  • 🧠 Generate translated JSON files via OpenAI (e.g., en.jsones.json)
  • 🌐 Local-only language preview UI (/langs)
  • 🔄 Export to nested i18n format (lang:export-i18n) for Vue or frontend use
  • 🗃️ Offline caching of AI translations to avoid repeated costs
  • ✅ Fully extendable, no frontend JS frameworks required

📦 Installation

composer require iperamuna/laravel-auto-translations
php artisan vendor:publish --tag=auto-translations-config

Make sure you have OPENAI_API_KEY in your .env:

AUTO_TRANSLATE_REMOTE_VIEW_ENABLED=false
AUTO_TRANSLATE_REMOTE_VIEW_TOKEN=/*any*/
AUTO_TRANSLATE_OPENAI_API_BASE=https://api.groq.com/openai/v1
AUTO_TRANSLATE_OPENAI_MODEL=meta-llama/llama-4-scout-17b-16e-instruct
AUTO_TRANSLATE_OPENAI_API_KEY=gsk_..

🛠️ Artisan Commands

# 🔍 Scan your app and generate en.json
php artisan lang:generate-en-json

# 🌍 Create a new translation using AI
php artisan lang:new es --preview

# 🧱 Export to nested format (e.g., for Vue i18n)
php artisan lang:export-i18n en

🌐 Local Language Browser

Access this route in APP_ENV=local:

http://localhost:8000/langs

Note: This route is only registered in local environment.

  • View translations from any existing .json language file
  • Switch between languages with a dropdown
  • Built using Tailwind CSS (via CDN)
  • Can do remote view enabling AUTO_TRANSLATE_REMOTE_VIEW_ENABLED=true and setting up token by AUTO_TRANSLATE_REMOTE_VIEW_TOKEN
http://{remote.host.url}/langs?_token=***

📁 Output Examples

en.json (flat)

{
  "auth.login": "Login",
  "profile.edit": "Edit Profile"
}

en.i18n.json (nested)

{
  "auth": {
    "login": "Login"
  },
  "profile": {
    "edit": "Edit Profile"
  }
}

📚 Configuration

Publish the config:

php artisan vendor:publish --tag=auto-translations-config

You can configure:

<?php
return [
    'remote' => [
        'enabled' => env('AUTO_TRANSLATE_REMOTE_VIEW_ENABLED', false),
        'token' => env('AUTO_TRANSLATE_REMOTE_VIEW_TOKEN', ''),
    ],
    'paths' => [
        base_path('resources/js'),
    ],
    
        'ai' => [
            'enabled' => env('AUTO_TRANSLATE_ENABLED', true),
            'api_key' => env('AUTO_TRANSLATE_OPENAI_API_KEY', ''),
            'api_base' => env('AUTO_TRANSLATE_OPENAI_API_BASE', 'https://api.groq.com/openai/v1'),
            'model'   => env('AUTO_TRANSLATE_OPENAI_MODEL', 'gpt-4o-mini'),
            'temp'    => env('AUTO_TRANSLATE_TEMPERATURE', 0.3),
            'chunk'   => env('AUTO_TRANSLATE_CHUNK_SIZE', 50),
        ],
    ];
];

🤖 AI Translation Strategy

  • Uses OpenAI Chat API (gpt-4o, gpt-3.5, etc.)
  • Can Use Groak Api too
  • Breaks translations into chunks
  • Prompts ChatGPT to preserve keys, variables (:name, :count, etc.)
  • Saves translated output as es.json, fr.json, etc.

📜 License

This package is open-source software licensed under the MIT license.

🙋‍♀️ Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.

📣 Credits

Made with ❤️ by Indunil Peramuna
Built for Laravel developers who love clean translation management.