lavary/laravel-menu

Translaions locale is not working

Closed this issue · 5 comments

Hi
I defined a menu like :

           $menu->add(__('Dashboard'), [
                'route' => 'backend.dashboard',
                'class' => 'c-sidebar-nav-item',
            ])

but Dashboard word not translated in output!!!

The way the __(...) function works, is it returns the translated value. We're not doing anything with translations. I presume there's something else going on with your configuration.

My translation config is JSON and My __('Dashboard') function works fine in views ,controllers or other location.

I think in ‍Menu Middleware is not loaded my translation configs... 🤔

Does anyone have an idea?

I don't have experience using the translation tools. But we're not doing anything that should interrupt it. You could try moving your menu definition later in your boot sequence. Maybe at controller initialization instead of in a service.

I also facing the same issue. __('Dashboard') worked for everywhere but not at GenerateMenus Middleware . @ingen2 did you find any solution of this?

I also facing the same issue. __('Dashboard') worked for everywhere but not at GenerateMenus Middleware . @ingen2 did you find any solution of this?

I'm using this solution :

menu.blade.php

@foreach($items as $key => $groups)
{{ $key }}
  @if($key == "Main")
    <li class="nav">
      <a href="/admin">
      <i class="fa fa-home" aria-hidden="true"></i>
      <span>{!! __('core::menu_backend.dashboard') !!}</span>
      </a>
    </li>
  @endif
  @foreach($groups as $data)
  <li @if($data['item']->hasChildren()) class="nav-parent {{ $data['item']->active ? "nav-expanded nav-active" : "" }}" @endif>
    @if($data['item']->hasChildren())
      <a>
        <i class="{!! $data['item']->icon !!}" aria-hidden="true"></i> 
        <span>**{!! $data['item']->caption !!}**</span>
      </a>
    @else
      <li class="nav{{ $data['item']->active ? " nav-expanded nav-active" : "" }}">       
        <a href="{!! $data['item']->url() !!}"> 
          <i class="{!! $data['item']->icon !!}" aria-hidden="true"></i>
          <span>**{!! $data['item']->caption !!}**</span>
        </a>
      </li>
    @endif
    @if($data['item']->hasChildren())
      <ul aria-expanded="false" class="nav nav-children">
            @php 
            $child = $data['item']->children()->map(function ($item) {
              return ["group" => '', "item" => $item];
            })->groupBy("group");
            @endphp
            @include('theme::views.backend.menu', ['items' => $child])
      </ul>
    @endif
  </li>
  @endforeach
@endforeach

menuhandler.php

Menu::make('MenuBackend', function ($menu) use ($request){
            $menu->add('Settings', '#')->data([
                'caption' => **__('core::menu_backend.settings')**,
                'icon' => 'fa fa-cogs',
                'order' => 1,
                'group' => 'Main',
                'active_match' => 'admin/settings/*',
            ]);
})->filter(function ($item) use ($request) {

            if($item->data('permissions')!=null){
                $permissions = explode('|', $item->data('permissions'));
                if(!Auth::user()->hasAnyPermission($permissions)){
                    return false;
                }
            }


            if ($request->url() === rtrim($item->url(), '#/')) {
                $item->active();
            }
            
            if ($item->active_match) {
                $matches = is_array($item->active_match) ? $item->active_match : [$item->active_match];                
                foreach ($matches as $pattern) {
                    if (Str::is($pattern, $request->path())) {
                        $item->activate();
                    }
                }
            }
            return true;
        })->sortBy('order');
        
        return $next($request

I create a new caption item data instead using title,
because title binded to item id which is work as @dustingraham mentioned, you need to add a unbinded attribute instead