joedixon / laravel-translation

Translation management for your Laravel application.
MIT License
698 stars 231 forks source link

How to use database driver for Application Specific translations and use File Driver for Vendor Translations #251

Open yob-yob opened 1 year ago

yob-yob commented 1 year ago

https://github.com/joedixon/laravel-translation/blob/e10786b0bcde76b684ad18dd21abe3dda9887c75/src/Drivers/File.php#L45

I was wondering also thou, since this package has it's own translations... but it does not include that in the database when syncing...

please tell me if I'm missing something here...

The reason why I was having trouble was because I'm using the package filament-admin... and this package is not finding the correct translations for the filament-admin package when using the database driver...

anyways... I've created a middleware to reset the translator when I'm on a specific path... for a quick fix of my problem...

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Translation\FileLoader;
use Illuminate\Translation\Translator;

class TranslationDriverManager
{
    protected $except = [
      'admin/*', 
      'telescope', 
      'horizon'
    ];

    public function handle(Request $request, Closure $next)
    {
      foreach ($this->except as $key => $value) {
        if ($request->is($value)) {
          app()->singleton('translation.loader', function ($app) {
              return new FileLoader($app['files'], $app['path.lang']);
          });

          app()->singleton('translator', function ($app) {
              $trans = new Translator($app['translation.loader'], $app['config']['app.locale']);
              $trans->setFallback($app['config']['app.fallback_locale']);
              return $trans;
          });
        }
      }

        return $next($request);
    }
}