admin icon indicating copy to clipboard operation
admin copied to clipboard

trans() helper method not working in menu.php

Open acacha opened this issue 10 years ago • 3 comments

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 avatar Sep 03 '15 20:09 acacha

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 avatar Sep 03 '15 20:09 acacha

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'));
}

acacha avatar Sep 05 '15 10:09 acacha

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

noxify avatar Oct 26 '15 21:10 noxify