如何在 Laravel 5 中创建多语言菜单

How to create multi language menu in Laravel 5

在 Laravel 5 中创建主菜单的最佳方法是什么?以及如何仅在用户登录时显示菜单项?制作这种多语言的最佳方法是什么?

试试 https://laravel.com/docs/5.1/authentication

例如:

if (Auth::check()) {
    return something to view
}

Laravel 提供了一种使用外观 Auth::check().

检查用户是否登录的简单方法
if (Auth::check()) {
    // The user is logged in...
}

关于翻译,你可以在这里查看:Localization

根据文档,结构定义如下:

/resources
    /lang
        /en
            messages.php
        /es
            messages.php

Laravel 还提供了一种使用 trans('string.to.translate') 翻译短语的简单方法,可在此处查看 trans().

在 messages.php 中(在两个 lang 目录中),您必须设置翻译字符串。在 en/messages.php:

    return [
        'welcome' => 'Welcome'
    ];

es/messages.php中:

    return [
        'welcome' => 'Bienvenido'
    ];

有了这两个,您可以在您的应用程序中执行以下操作:

    // Get the user locale, for the sake of clarity, I'll use a fixed string.
    // Make sure is the same as the directory under lang.
    App::setLocale('en'); 

在你的 view 里面:

    // Using blade, we check if the user is logged in.
    // If he is, we show 'Welcome" in the menu. If the lang is set to
    // 'es', then it will show "Bienvenido".
    @if (Auth::check()) 
        <ul>
            <li> {{ trans('messages.welcome') }} </li>
        </ul>
    @endif