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

Ron*_*ers 0 laravel laravel-routing laravel-5 laravel-5.1 laravel-5.2

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

res*_*all 7

Laravel提供了一种简单的方法来检查用户是否使用外观登录Auth::check().

if (Auth::check()) {
    // The user is logged in...
}
Run Code Online (Sandbox Code Playgroud)

关于翻译,您可以在这里查看:本地化

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

/resources
    /lang
        /en
            messages.php
        /es
            messages.php
Run Code Online (Sandbox Code Playgroud)

Laravel还提供了一种使用the翻译短语的简便方法trans('string.to.translate'),可以在这里看到trans().

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

    return [
        'welcome' => 'Welcome'
    ];
Run Code Online (Sandbox Code Playgroud)

es/messages.php:

    return [
        'welcome' => 'Bienvenido'
    ];
Run Code Online (Sandbox Code Playgroud)

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

    // 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'); 
Run Code Online (Sandbox Code Playgroud)

你的内心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
Run Code Online (Sandbox Code Playgroud)