McRui
McRui

Reputation: 1931

Laravel get getCurrentLocale() in AppServiceProvider

I'm trying to get the LaravelLocalization::getCurrentLocale() in the boot() method of the Laravel AppServiceProvider class, and although my default locale is pt I always get the en. The package I'm using is mcamara/laravel-localization. Code I have:

public function boot()
{
    Schema::defaultStringLength(191);

    // Twitter view share
    $twitter = Twitter::getUserTimeline(['screen_name' => env('TWITTER_USER'), 'count' => 3, 'format' => 'object']);
    view()->share('twitter', $twitter);

    // Current language code view share
    $language = LaravelLocalization::getCurrentLocale();
    view()->share('lang', $language);

    // Practice Areas
    view()->share('practice_areas', \App\Models\PracticeArea::with('children')->orderBy('area_name')->where(['parent_id' => 0, 'language' => $language])->get());

}

I'm probably placing this in the wrong place because when I try to share the practice_areas variable it always sets it as en even if the language is switched.

What may I be doing wrong?

Thanks in advance for any help

Upvotes: 0

Views: 3196

Answers (3)

Marco Carrodano
Marco Carrodano

Reputation: 81

Faced the exact same problem, solved by using a dedicated Service Provider and a view composer class, like so:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class LocalizationServiceProvider extends ServiceProvider
{
    public function boot() {
        View::composer(
            '*', 'App\Http\ViewComposers\LocalizationComposer'
        );
    }
}

and then on LocalizationComposer class:

<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;
use LaravelLocalization;

class LocalizationComposer {

    public function compose(View $view)
    {
        $view->with('currentLocale', LaravelLocalization::getCurrentLocale());
        $view->with('altLocale', config('app.fallback_locale'));
    }

}

currentLocale and altLocale will be available on all views of your application

Upvotes: 1

McRui
McRui

Reputation: 1931

After several hours trying to work around the issue, I decided not to use the view()->share() with mcamara/laravel-localization package methods here. The reasons seems to be that in the AppServiceProvider::class boot() method the package isn't yet getting the requested language string.

Anyway, thank you all for your help!

Upvotes: 0

user320487
user320487

Reputation:

From the package docs Usage section:

Laravel Localization uses the URL given for the request. In order to achieve this purpose, a route group should be added into the routes.php file. It will filter all pages that must be localized.

You need to be setting the localization within your route group definitions:

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
    /** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
    Route::get('/', function()
    {
        return View::make('hello');
    });

    Route::get('test',function(){
        return View::make('test');
    });
});

Upvotes: 0

Related Questions