caffeinated / menus

:pushpin: Menu generator package for the Laravel framework
https://caffeinatedpackages.com
132 stars 59 forks source link

can't use trans or Lang:choice #37

Closed illuminate3 closed 9 years ago

illuminate3 commented 9 years ago
        $menu = Menu::get('navbar');
        $menu->add(trans('kotoba::general.home'), '')->data('order', 1);

I also tried with Laravel's default files.

NOTE: I've been tweeking my L5 install so that it is possible that I have a conflict with my own middleware or something else on my side.

kaidesu commented 9 years ago

I can't help much if you don't provide the outcome of your code, haha. What are the errors?

illuminate3 commented 9 years ago

lol ..

It just defaults to the default locale if the "en" directory is included. "kotoba::general.home" is just "home" in English. The Spanish should be "portada" (<-- I doubt this a correct translation)

IF I remove the "en" default locale it will just show "kotoba::general.home".

The same will occur if I use "Lang::choice('')"

kaidesu commented 9 years ago

Just tested on a clean install, and all trans(), Lang::choice() and Lang::get() methods work for me without issue.

illuminate3 commented 9 years ago

ok, will look into what I'm doing that is causing this issue. Thanks!

illuminate3 commented 9 years ago

Ok, this works when you set locale=>'es' directly but I'm trying to set it with a middleware using

App::setLocale(Session::get('locale'));

I'm gonna take a look again by reducing the number of modules I'm running.

kaidesu commented 9 years ago

I would check the loading order. I think packages are loaded and registered before middleware are. I could be wrong though.

illuminate3 commented 9 years ago

Here's what I got:

    protected $middleware = [
        'App\Http\Middleware\SetTheme',
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'App\Http\Middleware\VerifyCsrfToken',
        'App\Http\Middleware\SetLanguage',
    ];
class SetLanguage implements Middleware {

    public function __construct(Request $request)
    {
        $this->request = $request;

// fix for setting App::locale
        $lang = Session::get('locale');
        if ($lang == null) {
            Session::set('locale', Config::get('app.locale'));
        }
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

//dd(Config::get('app.locale'));
//dd(Session::get('locale'));
//dd(Session::has('locale'));

        $lang = Session::get('locale');
        if ( $lang != App::getLocale() ) {
            if ( Session::has('locale') && array_key_exists(Session::get('locale'), Config::get('languages.supportedLocales')) ) {
                App::setLocale(Session::get('locale'));
//dd('here');
            } else {
                App::setLocale(Config::get('app.fallback_locale'));
            }
        }

        return $next($request);

    }

}

hmmmm....

kaidesu commented 9 years ago

Again, service providers are loaded before middleware are.

Service providers are the key to bootstrapping a Laravel application. The application instance is created, the service providers are registered, and the request is handed to the bootstrapped application [excerpt taken from the Laravel documentation].

Read more on the lifecycle here: http://laravel.com/docs/5.0/lifecycle#lifecycle-overview

Create a new service provider to set the language, and simply make sure that it is registered before your menu service provider.