0

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

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
McRui
  • 1,879
  • 3
  • 20
  • 31
  • It seems you are using an external package for the locale. Please edit your question an add the name of the package. – Camilo Mar 10 '18 at 02:44
  • Thanks Camilo, forgot that detail. Edited. It;s Mcamara\LaravelLocalization\Facades\LaravelLocalization; – McRui Mar 10 '18 at 02:50
  • what about the `fallback_locale` is it en or pt as well? – Hamza Mohamed Mar 10 '18 at 03:16
  • The fallback_locale is 'pt' – McRui Mar 10 '18 at 03:18
  • It seems that calling the `Mcamara\LaravelLocalization\Facades\LaravelLocalization` in the `boot()` method is what's I'm doing wrong. If I force either 'pt' or 'en' it returns the correct results at the DB call in the where clause. – McRui Mar 10 '18 at 03:22
  • could be .. what did dd($language) output? .. check this [part](https://github.com/mcamara/laravel-localization#set-locale) >> This function will change the application's current locale. If the locale is not passed, the locale will be determined via a cookie (if stored previously), the session (if stored previously), browser Accept-Language header or the default application locale (depending on your config file). – Hamza Mohamed Mar 10 '18 at 03:27
  • It always gives me 'en' when I `dd($language)` at the `AppServiceProvider ` – McRui Mar 10 '18 at 03:29
  • if you dd(getLocale()) after the $language .. is it giving you en as well? – Hamza Mohamed Mar 10 '18 at 03:35
  • When I `dd(App::getLocale());` it gives me 'pt'. If I change the language in the FE switcher it keeps giving me 'pt' – McRui Mar 10 '18 at 03:48
  • I think your problem lies in the package ability to get the local language correctly .. this is from the [config.php](https://raw.githubusercontent.com/mcamara/laravel-localization/master/src/config/config.php) >>>> // Negotiate for the user locale using the Accept-Language header if it's not defined in the URL? // If false, system will take app.php locale attribute // If LaravelLocalizationRedirectFilter is active and hideDefaultLocaleInURL // is true, the url would not have the default application language – Hamza Mohamed Mar 10 '18 at 03:59
  • >>>> // IMPORTANT - When hideDefaultLocaleInURL is set to true, the unlocalized root is treated as the applications default locale "app.locale". // Because of this language negotiation using the Accept-Language header will NEVER occur when hideDefaultLocaleInURL is true. // If you want to display the locales in particular order in the language selector you should write the order here. //CAUTION: Please consider using the appropriate locale code otherwise it will not work – Hamza Mohamed Mar 10 '18 at 03:59
  • this config.php is the laravelLocalization.php which has been published – Hamza Mohamed Mar 10 '18 at 04:01
  • In the laravelLocalization.php I have, besides the active languages, the following: `'useAcceptLanguageHeader' => false, 'hideDefaultLocaleInURL' => false, 'localesOrder' => ['pt', 'en'],'` – McRui Mar 10 '18 at 04:10

3 Answers3

1

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

  • 1
    Thanks Marco, it works like charm but only after one missing part in your solution, which is to register the `LocalizationServiceProvider::class` in the `AppServiceProvider` `register()` method like `$this->app->register(LocalizationServiceProvider::class); . Thanks!` – McRui Dec 30 '18 at 15:57
0

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');
    });
});
  • Hi, I'm already doing that in my route group: `Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath', ], 'before' => 'csrf', ], function() { // }` – McRui Mar 10 '18 at 03:02
  • What happens when you change `'locale' => 'en',` in the `config/app.php` file? –  Mar 10 '18 at 03:08
  • Still gives me the 'en' – McRui Mar 10 '18 at 03:12
  • Have you published the package's config file? –  Mar 10 '18 at 03:14
  • Yes I did. The thing seems to be related to the boot() method. Probably I should make the `view()->share('practice_areas', \App\Models\PracticeArea::with('children')->orderBy('area_name')->where(['parent_id' => 0, 'language' => 'pt'])->get());` elsewhere. I'm calling this view share to build a menu item. – McRui Mar 10 '18 at 03:17
  • Have a look at [View Composers](https://laravel.com/docs/5.6/views#view-composers), that might be better suited for your case. –  Mar 10 '18 at 03:26
  • It's most likely calling the `boot` method before the route gets resolved, therefore it's not able to set the locale properly. –  Mar 10 '18 at 03:27
0

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!

McRui
  • 1,879
  • 3
  • 20
  • 31