elegantly/laravel-translator

All on one translations management for Laravel

v0.0.13 2024-06-20 18:52 UTC

This package is auto-updated.

Last update: 2024-06-20 18:53:04 UTC


README

Laravel Translator - All in one translations file manager

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Manage all your laravel translations easily:

  • Sort tranlations array in natural order
  • Find missing translations strings
  • Translate automatically string to other languages (DeepL, OpenAI or any custom service)
  • Fix Grammar and syntax in your translations (OpenAI, or any custome service)
  • Format your translations files

Installation

You can install the package via composer:

composer require-dev elegantly/laravel-translator

You can publish the config file with:

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

This is the contents of the published config file:

return [

    'lang_path' => lang_path(),

    'translate' => [
        'service' => 'deepl',
        'services' => [
            'deepl' => [
                'key' => env('DEEPL_KEY'),
            ],
            'openai' => [
                'model' => 'gpt-4o',
                'prompt' => "Translate the following json to the locale '{targetLocale}' while preserving the keys.",
            ],
        ],
    ],

    'grammar' => [
        'service' => 'openai',
        'services' => [
            'openai' => [
                'model' => 'gpt-4o',
                'prompt' => '
                            Fix the grammar and the syntax the following json string while preserving the keys.
                            Do not change the meaning or the tone of the sentences and never change the keys.
                            ',
            ],
        ],
    ],

];

Usage

This package can be used:

  • Like a CLI tool, using commands.
  • In a programmatic way using \Elegantly\Translator\Facades\Translator::class facade class.

Sort all translations in natural order

You can format and sort all your php translations files using:

php artisan translator:sort
use Elegantly\Translator\Facades\Translator;

Translator::sortAllTranslations();

Find the missing translations

You can display all the missing translations present in a given locale but not in the other ones using:

php artisan translator:missing fr
use Elegantly\Translator\Facades\Translator;

Translator::getAllMissingTranslations('fr');

Auto translate strings

This package can automatically translate your files for you. It includes 2 services right now:

  • DeepL
  • OpenAI

You can also define your own service.

Auto-translate using DeepL

First, you need to edit the config file to add your DeepL api key and select deepl as your service:

return [
    'translate' => [
        'service' => 'deepl', // select the default service here

        'services' => [
            'deepl' => [
                'key' => env('DEEPL_KEY'), // add you api key here
            ],

        ],
    ],
]

To translate all the missing translations use:

php artisan translator:translate --from=fr --to=en

To translate all translations use:

php artisan translator:translate --from=fr --to=en --all

Ommitting the --to option will translate to every available languages in your project.

use Elegantly\Translator\Facades\Translator;

Translator::translateTranslations(
    referenceLocale: 'fr',
    targetLocale: 'en',
    namespace: 'namespace-file',
    keys: ['title', ...]
);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.