PHP code example of esign / laravel-translation-loader

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

    

esign / laravel-translation-loader example snippets


return new class extends Migration
{
    public function up(): void
    {
        Schema::create('translations', function (Blueprint $table) {
            $table->id();
            $table->string('key');
            $table->string('group')->default('*');
            $table->text('value_en')->nullable();
            $table->timestamps();

            $table->unique(['key', 'group']);
        });
    }
};

return [
    /**
     * These loaders will load translations from different sources.
     * You can use any class that implements the TranslationLoaderContract.
     */
    'loaders' => [
        \Esign\TranslationLoader\Loaders\DatabaseLoader::class,
    ],

    /**
     * This is the loader that combines all of the other loaders together.
     * This class overrides Laravel's default `translation.loader`.
     */
    'aggregate_loader' => \Esign\TranslationLoader\Loaders\AggregateLoader::class,

    /**
     * This is the model that will be used by the DatabaseLoader.
     * You may provide a class that implements the UnderscoreTranslatable trait.
     */
    'model' => \Esign\TranslationLoader\Models\Translation::class,

    'cache' => [
        /**
         * The key that will be used to cache the database translations.
         */
        'key' => 'esign.laravel-translation-loader.translations',

        /**
         * The duration for which database translations will be cached.
         */
        'ttl' => \DateInterval::createFromDateString('24 hours'),

        /**
         * The cache store to be used for database translations.
         * Use null to utilize the default cache store from the cache.php config file.
         * To disable caching, you can use the 'array' store.
         */
        'store' => null,
    ],

    /**
     * Configuration for the custom translator class that handles missing translation keys.
     * This class overrides Laravel's default `translator` binding.
     */
    'translator' => \Esign\TranslationLoader\Translator::class,
];

use Esign\TranslationLoader\Models\Translation;

Translation::create([
    'group' => 'messages',
    'key' => 'welcome',
    'value_en' => 'Hello World!',
    'value_nl' => 'Hallo Wereld!',
]);

trans('messages.welcome'); // Hello World!
trans('messages.welcome', [], 'nl'); // Hallo Wereld!

use Esign\TranslationLoader\Facades\Translator;

Translator::setMissingKeyCallback(function (string $key, string $locale) {
    // Implement your custom logic here

    return "Fallback translation for '$key'";
});

use Esign\TranslationLoader\Facades\Translator;

Translator::createMissingTranslations();

use Esign\TranslationLoader\Contracts\TranslationLoaderContract;

class MyTranslationsLoader implements TranslationLoaderContract
{
    public function loadTranslations(string $locale, string $group, string $namespace = null): array
    {
        // Your implementation here
    }
}

use Illuminate\Contracts\Translation\Translator as TranslatorContract;
use Illuminate\Foundation\Application;
use Illuminate\Translation\FileLoader;
use Illuminate\Translation\Translator;
use Mcamara\LaravelLocalization\LaravelLocalization;

public function register(): void
{
    $this
        ->app
        ->when(LaravelLocalization::class)
        ->needs(TranslatorContract::class)
        ->give(function (Application $app) {
            $loader = new FileLoader($app['files'], [__DIR__.'/lang', $app['path.lang']]);

            return new Translator($loader, $app->getLocale());
        });
}
bash
php artisan vendor:publish --provider="Esign\TranslationLoader\TranslationLoaderServiceProvider" --tag="migrations"
bash
php artisan vendor:publish --provider="Esign\TranslationLoader\TranslationLoaderServiceProvider" --tag="config"
bash
php artisan translations:clear-cache