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

如何在Laravel 5中创建多语言菜单,laravel,laravel-5,laravel-5.1,laravel-5.2,laravel-routing,Laravel,Laravel 5,Laravel 5.1,Laravel 5.2,Laravel Routing,在Laravel 5中创建主菜单的最佳方法是什么?以及如何仅在用户登录时显示菜单项?使用这种多语言的最佳方法是什么?试试看 例如: if (Auth::check()) { return something to view } // Get the user locale, for the sake of clarity, I'll use a fixed string. // Make sure is the same as the directory under l

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

试试看

例如:

if (Auth::check()) {
    return something to view
}
    // 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'); 
试一试

例如:

if (Auth::check()) {
    return something to view
}
    // 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'); 

通过使用facade
Auth::check()
,Laravel提供了一种检查用户是否登录的简单方法

关于翻译,您可以在此处查看:

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

/resources
    /lang
        /en
            messages.php
        /es
            messages.php
Laravel还提供了一种使用
trans('string.to.translate')
翻译短语的简单方法,如图所示

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

    return [
        'welcome' => 'Welcome'
    ];
    return [
        'welcome' => 'Bienvenido'
    ];
es/messages.php
中:

    return [
        'welcome' => 'Welcome'
    ];
    return [
        'welcome' => 'Bienvenido'
    ];
使用这两个选项,您可以在应用程序中执行以下操作,例如:

if (Auth::check()) {
    return something to view
}
    // 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'); 
视图中

    // 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
//使用blade,我们检查用户是否登录。
//如果他是,我们在菜单上显示“欢迎”。如果lang设置为
//“es”,然后会显示“Bienvenido”。
@if(Auth::check())
  • {{trans('messages.welcome')}
@恩迪夫
Laravel提供了一种使用facade
Auth::check()
检查用户是否登录的简单方法

关于翻译,您可以在此处查看:

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

/resources
    /lang
        /en
            messages.php
        /es
            messages.php
Laravel还提供了一种使用
trans('string.to.translate')
翻译短语的简单方法,如图所示

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

    return [
        'welcome' => 'Welcome'
    ];
    return [
        'welcome' => 'Bienvenido'
    ];
es/messages.php
中:

    return [
        'welcome' => 'Welcome'
    ];
    return [
        'welcome' => 'Bienvenido'
    ];
使用这两个选项,您可以在应用程序中执行以下操作,例如:

if (Auth::check()) {
    return something to view
}
    // 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'); 
视图中

    // 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
//使用blade,我们检查用户是否登录。
//如果他是,我们在菜单上显示“欢迎”。如果lang设置为
//“es”,然后会显示“Bienvenido”。
@if(Auth::check())
  • {{trans('messages.welcome')}
@恩迪夫