zerospam / laravel-gettext

Adds localization support to laravel applications using PoEdit and GNU-Gettext.
99 stars 56 forks source link

Issues setting the language #19

Closed simtrax closed 5 years ago

simtrax commented 5 years ago

Recently I upgraded an application from Laravel 5.5 to 5.6. The translations worked fine before, but I believe they somehow broke after the update.

I am using the 5.6 version: "Belphemur/laravel-gettext": "7.x"

I have a dropdown menu that lets the user select the language. A request is sent to a controller that will update the language accordingly. But unfortunately the strings on the page stays in the same language and does not get updated to the selected one. So I tried to die dump LaravelGettext::getLocale(); after the call to LaravelGettext::setLocale($locale);. And the result is the actual selected language. I can also find that the selected language is set in the PHP session. But as said no strings are translated.

I tried to set the locale in AppServiceProvider.php boot method, just as a test. And that works! The strings get translated to the other language.

Can someone help me out? I have no idea where to start looking. Somehow the strings does not get translated even though the language is set in the session. I have \Xinax\LaravelGettext\Middleware\GettextMiddleware::class registered in the middlewareGroups in Kernel.php.

Thanks.

simtrax commented 5 years ago

When testing further I noticed that this works:

    /**
     * Display the dashboard
     *
     * @return \Illuminate\Http\Response
     */
    public function dashboard()
    {
        \LaravelGettext::setLocale('sv_SE');
        \LaravelGettext::setLocale('en_US');
        return view('dashboard');
    }

But if I remove the top one it will stay in Swedish and not get updated to English.

And since I can install a new Laravel project and get the translation working there without any problems it seems like there's an issue in my main Laravel project.

dnmarques commented 5 years ago

Did you solve this problem?

How is your config file?

simtrax commented 5 years ago

@dnmarques I figured it out last weekend. The issue was that I tried to translate a string in AppServiceProvider. I guess this is to early in the boot process to do translations. The reason was that we added a custom validator there, that we wanted to have a translated string as a error message.

So I'm closing the issue.