PHP code example of elegantly / laravel-translator

1. Go to this page and download the library: Download elegantly/laravel-translator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elegantly / laravel-translator example snippets


use Elegantly\Translator\Drivers\PhpDriver;

return [
    /**
     * Possible values: 'php', 'json', or a class-string implementing Driver.
     */
    'driver' => PhpDriver::class,

    // ...
];

use Elegantly\Translator\Support\LocaleValidator;

return [
    // ...
    'locales' => LocaleValidator::class,
    // ...
];

return [
    // ...
    'locales' => ['en', 'fr', 'es'],
    // ...
];

__('messages.home.title'); // ✅ 'messages.home.title' is detected

foreach(__('messages.welcome.lines') as $line){
    // ✅ 'messages.welcome.lines' and all of its children are detected.
}

$key = 'messages.home.title';
__($key); // ❌ no key is detected

return [
    'searchcode' => [
        'paths' => [
            app_path(),
            resource_path(),
        ],
    ],
];

return [
    'searchcode' => [
        'excluded_paths' => [
            'tests'
        ],
    ],
];

return [
    'searchcode' => [
        'ignored_translations' => [
            'countries', // Ignore keys starting with 'countries'.
        ],
    ],
];

use Elegantly\Translator\Facades\Translator;

Translator::sortTranslations(locale: 'fr');

Translator::driver('json')->sortTranslations(locale: 'fr');

return [
    // ...

    'services' => [
        'openai' => [
            'key' => env('OPENAI_API_KEY'),
            'organization' => env('OPENAI_ORGANIZATION'),
            'request_timeout' => env('OPENAI_REQUEST_TIMEOUT'),
        ],
    ],

    // ...
];

return [
    // ...

    'services' => [
        // ...
        'deepl' => [
            'key' => env('DEEPL_KEY'),
        ],
    ],

    // ...
];

Translator::translateTranslations(
    source: 'en',
    target: 'fr',
    keys: ['validation.title', ...]
);

Translator::driver('json')->translateTranslations(
    source: 'en',
    target: 'fr',
    keys: ['My Title', ...]
);

Translator::proofreadTranslations(
    locale: 'fr',
    keys: ['auth.email', ...]
);

Translator::driver('json')->proofreadTranslations(
    locale: 'fr',
    keys: ['My Title', ...]
);

Translator::getUntranslatedTranslations(source: 'en', target: 'fr');

Translator::getMissingTranslations(locale: 'en');

Translator::getDeadTranslations(locale: 'fr');

$path = Translator::exportTranslations('/path/to/my/file.csv');

$translations = Translator::importTranslations('/path/to/my/file.csv');
bash
php artisan vendor:publish --tag="translator-config"
bash
php artisan translator:sort
bash
php artisan translator:sort --driver=json
bash
php artisan translator:untranslated en fr
bash
php artisan translator:untranslated en fr --translate
bash
php artisan translator:untranslated en fr --translate --driver=json
bash
php artisan translator:add-locale fr en --translate
bash
php artisan translator:proofread en
bash
php artisan translator:untranslated en fr
bash
php artisan translator:missing en
bash
php artisan translator:missing en --driver=json
bash
php artisan translator:missing en --sync
bash
php artisan translator:dead en