sleeping-owl / admin

Administrative interface builder for Laravel
http://sleeping-owl.github.io/
MIT License
504 stars 258 forks source link

trans() helper method not working in menu.php #226

Open acacha opened 9 years ago

acacha commented 9 years ago

If I change Laravel Locale App using this aprox:

http://mydnic.be/post/laravel-5-and-his-fcking-non-persistent-app-setlocale

and menu.php file is:

Admin::menu(PrivacyDriver\Models\Country\Country::class)->label(trans('admin.countries'))->icon('fa-briefcase'); //T
Admin::menu(PrivacyDriver\Models\Client\ClientType::class)->label(trans('admin.clientstype'))->icon('fa-briefcase'); //T

Menu labels will not translate. Any possible solution?

acacha commented 9 years ago

Note: only menu labels are not translated! Maybe menu is a singleton instance and is not possible to change labels?

EDIT: see next comment

acacha commented 9 years ago

Debugging with laravel-debug-bar I've confirmed that boot method of AdminServiceProvider (in which menu.php is readed) is called before my Language switcher Middleware. One possible solution is checking language at menu.php as following (the same code I use at middleware):

if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('laravellocalization.supportedLocales'))) {
    App::setLocale(Session::get('applocale'));
} else {
    App::setLocale(Config::get('app.fallback_locale'));
}
noxify commented 9 years ago

Hi,

we had the same problem in our fork...

We have implemented the locale change, based on http://mydnic.be/post/laravel-5-and-his-fcking-non-persistent-app-setlocale

Then, you have to do the following:

<?php
//menu.php
Admin::menu()->url('/')->label('admin::lang.admin.dashboard')->icon('fa-dashboard');
Admin::menu()->label('User Management')->icon('fa-book')->items(function ()
{
    Admin::menu('Cartalyst\Sentinel\Users\EloquentUser')->icon('fa-user');
    Admin::menu('Cartalyst\Sentinel\Roles\EloquentRole')->icon('fa-users');
    Admin::menu('SleepingOwl\Admin\Model\Permission')->icon('fa-users');

});
//menu_item.blade.php
@if( Sentinel::hasAnyAccess($permission) )
<li {!! (count($items) > 0) ? 'class="treeview"' : '' !!}>
    <a href="{{ $url }}">
        <i class="fa fa-fw {{ $icon }}"></i> <span>{!! trans($label) !!}</span>
        @if (count($items) > 0)
            <i class="fa fa-angle-left pull-right"></i>
        @endif
    </a>
    @if (count($items) > 0)
        <ul class="treeview-menu">
            @foreach ($items as $item)
                {!! $item !!}
            @endforeach
        </ul>
    @endif
</li>
@endif

As you can see, we moved the trans() helper from the menu.php to the template and then everything is working.

And if you don't want to use translations, you can type whatever you want. The trans() helper will display the given value.

Hope this helps :)

P.S. The template is from the AdminLTE template