Overriding module translations

keevitaja - 5 months ago

Hello!

I can set new translation items from service provider or from route middleware. I just use Illuminate\Translation\Translator and addLines method. How ever i cannot override any module translations set in the language files. Here's a not working example:

<?php namespace Keevitaja\TestModule;

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

class LoadTranslations
{
    protected $trans;

    public function __construct(Translator $trans)
    {
        $this->trans = $trans;
    }

    public function handle(Request $request, Closure $next)
    {
        $this->trans->addLines([
            'message.logged_out' => 'testing'
        ], 'en', 'anomaly.module.users');

        dd($this->trans->trans('anomaly.module.users::message.logged_out'));

        return $next($request);
    }
}

If i change the namespace to * i can get the message with trans('message.logged_out'). Is it possible to override module translations at all?

ryanthompson - 5 months ago

Use the addon:publish command and modify the translation files directly thereafter.

keevitaja - 5 months ago

@ryanthompson sorry, i need to do this programmatically. how could i achieve it?

it is for my translations module: https://github.com/keevitaja/translations-module

ryanthompson - 5 months ago

You would probably have to use the loader - I'd checkout how I do it - cause it merges not replaces namespaces. And adapt that to your needs.