PHP code example of simonecerruti / laravel-translation-audit

1. Go to this page and download the library: Download simonecerruti/laravel-translation-audit 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/ */

    

simonecerruti / laravel-translation-audit example snippets


'translation_calls' => [
    't',                                                // t('messages.welcome')
    'App\Support\t',                                    // a namespaced function
    App\Support\Translator::class.'::translate',        // Translator::translate('messages.welcome')
    'Illuminate\Support\Facades\Lang::customTranslate', // a macro on the Lang facade
    'trans_for' => 1,                                   // trans_for($locale, 'messages.welcome')
],

'dynamic_keys' => [
    'payments.*' => PaymentMethod::class,              // the values of the enum cases
    'status.*.label' => ['active', 'suspended'],
],

trait HasLabel
{
    public function label(): string
    {
        return __('orders.status.'.str($this->value)->replace('_', '-'));
    }
}

use TranslationAudit\Contracts\TranslationKeyResolver;

class OrderStatusLabelKeys implements TranslationKeyResolver
{
    public function resolve(): iterable
    {
        foreach (OrderStatus::cases() as $case) {
            yield 'orders.status.'.str($case->value)->replace('_', '-');
        }
    }

    public function covers(): array
    {
        return ['app/Enums/Concerns/HasLabel.php' => 'orders.status.*'];
    }
}

'resolvers' => [
    App\Translations\OrderStatusLabelKeys::class,
],

use TranslationAudit\Console\Commands\AuditCommand;
use TranslationAudit\Results\Contracts\Result;

class NotifyTeam
{
    public function before(AuditCommand $command): void
    {
        // ...
    }

    public function after(Result $result, Slack $slack): void
    {
        if (! $result->isClean()) {
            $slack->send('Some translations need attention.');
        }
    }
}

use TranslationAudit\Console\Commands\AuditTranslations;
use TranslationAudit\Console\Commands\PurgeUnusedTranslations;

'hooks' => [
    App\Translations\PullTranslations::class,                         // every command
    App\Translations\NotifyTeam::class => AuditTranslations::class,   // only translation:audit
    App\Translations\CommitLangFiles::class => [PurgeUnusedTranslations::class],
],

'additional_keys' => [
    'messages.welcome',
    'payments.*',      // audited as a dynamic key
],

'ignore_keys' => [
    'Hello',          // ignored for every locale
    'Hi' => ['en'],   // ignored only for English
    'payments.*',     // a dynamic key, like __("payments.{$method}"), ignored by its pattern
],
bash
php artisan vendor:publish --tag="laravel-translation-audit-config"
bash
php artisan translation:audit
text
  app/Http/Controllers/Auth/LoginController.php
    IT      auth.failed
    EN, IT  Welcome back! Please sign in to continue with your
            account.

  resources/views/welcome.blade.php
    IT      messages.welcome

Found 3 keys with missing translations in 2 files.
bash
php artisan translation:audit --unused
bash
php artisan translation:purge-unused --dry-run
bash
php artisan translation:purge-unused
json
{"unused":{},"not_purged":{"it":{"lang/it/legacy.php":{"legacy.old":"The file does not return a literal array."}}}}
bash
php artisan vendor:publish --tag="laravel-translation-audit-workflow"
bash
php artisan translation:audit --display-format=json
json
{"missing":{"app/Http/Controllers/HomeController.php":{"auth.failed":["it"],"messages.welcome":["en","it"]}}}
json
{"missing":{},"skipped":{"resources/views/kits/edit.blade.php":"Syntax error, unexpected '<' on line 2"}}
bash
php artisan translation:audit --for-agent
bash
php artisan translation:audit --save