samuelgjekic/laravel-ai-translator

AI-powered translation package for Laravel applications using Prism AI

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/samuelgjekic/laravel-ai-translator

v1.0.5 2025-12-20 19:17 UTC

This package is auto-updated.

Last update: 2026-01-20 19:30:47 UTC


README

Laravel AI Translator is a developer tool for scanning and generating translations in Laravel using AI.

Right now it focuses on JSON translations (e.g. lang/en.json) and exports/imports that workflow.

image

What it does

1) Translate a JSON language file with AI

If you have a lot of keys (e.g. 3000+), you can translate them into a new language using a single Artisan command.

Example: translate en.json → Swedish:

php artisan translate:language swedish

Under the hood the package:

  • reads your source JSON (default is configurable)
  • splits it into chunks
  • securely translates a few keys at a time
  • writes the final translated JSON file (e.g. sv.json)

2) Scan your app/views and generate a JSON file of translation keys

If you don’t already have a JSON file with all your keys, you can generate one by scanning your codebase:

php artisan translations:export

You can also override the output location:

php artisan translations:export --output=path/to/export.json

The scanner looks through your configured paths (app + views by default) and extracts translation keys from:

  • __('...'), @lang('...'), trans('...'), Lang::get('...')
  • keys with parameters like __('Items total: :count', ['count' => ...])
  • placeholders/variables like :count, {count}, {{name}}, etc.

Variables/placeholders are preserved by the AI translator and should not be translated.

Installation

composer require samuelgjekic/laravel-ai-translator

Publish config (recommended):

php artisan vendor:publish --tag=ai-translator-config

Configuration

AI credentials (Prism)

This package uses Prism under the hood. Set your provider keys the way Prism expects.

Example for OpenAI:

OPENAI_API_KEY=your-api-key

Provider + model

You can switch provider/model via env:

AI_TRANSLATOR_PROVIDER=openai
AI_TRANSLATOR_MODEL=gpt-5-mini

Package config

After publishing config/ai-translator.php, you can configure:

  • default source_language (e.g. en)
  • chunk size
  • queue/job settings (tries, timeout, delays)
  • scanning paths and export path
  • regex patterns for key detection

The most important config block is:

'paths' => [
    // Directory for translation JSON files. null = use Laravel's default lang_path()
    'translations' => null,

    // Default export file for the scanner (relative to base_path())
    'export' => 'lang/export.json',

    // What to scan when running translations:export
    'scan' => [
        'app',
        'resources/views',
    ],
],

Usage

Translate language files

Translate using queue jobs (recommended):

php artisan translate:language swedish

Specify a custom source file (without .json):

php artisan translate:language swedish --source=en

Run without jobs (not recommended, but useful for quick testing):

php artisan translate:language swedish --sync

Translate only a few chunks (useful for testing prompts / rate limiting):

php artisan translate:language swedish --chunks=3

Scan & export keys

Scan your app + views (configured in paths.scan) and export a key JSON:

php artisan translations:export

Override output file:

php artisan translations:export --output=myfolder/keys.json

Notes / current limitations

  • Currently JSON translations only (lang/*.json).
  • The translator has been tested on 5000+ keys, including keys with variables/placeholders, across multiple languages.
  • Translating large files can take time depending on queue throughput, rate limits, and key count.
  • More support for other Laravel translation styles (arrays, vendor files, etc.) is coming.

Contributing

PRs are welcome.

If you’d like to add more scanning patterns, improve prompts, add support for array-based translations, or add tests—jump in.

License

MIT. See LICENSE.md.