【问题标题】:Laravel frontend and backend with different multilanguageLaravel 前端和后端具有不同的多语言
【发布时间】:2017-03-14 01:27:57
【问题描述】:

同时在 Laravel 中,我需要在站点前端和后端(管理)中使用不同的语言/区域设置。前端需要 4 种语言(en,de,fr,it),后端需要 3 种语言(en,lt,es)。
示例:在浏览器中,我有两个打开的选项卡 - 1 个选项卡前端(语言:de),2 个选项卡后端(语言:en)。怎么做 ?与 setLocale?或者我需要不同的数组,例如后端?

【问题讨论】:

    标签: laravel multilingual setlocale


    【解决方案1】:

    您可以轻松处理此问题的一种方法是为您的前端和后端控制器创建两个 BaseController 类。

    然后,您可以使用 App::setLocale 方法从右侧的 BaseController 构造函数为您的前端和后端设置不同的语言。

    例子:

    <?php
    
    class FrontendBaseController extends Controller
    {
        public function __construct()
        {
            App::setLocale(Session::get('frontend-locale'));
        }
    }
    
    
    class BackendBaseController extends Controller
    {
        public function __construct()
        {
            App::setLocale(Session::get('backend-locale'));
        }
    }
    
    class HomeController extends FrontendBaseController
    {
        public function __construct()
        {
    
        }
    }
    
    class BackendDashboardController extends BackendBaseController
    {
        public function __construct()
        {
    
        }
    }
    

    在上面的示例中,我从会话中检索当前语言环境。 您可以将您的语言文件放在app/lang 文件夹中。我建议您为前端和后端语言文件设置单独的文件夹。

    示例文件夹结构:

    /app
        /lang
            /en
                backend/
                    dashboard.php
    
                frontend/
                    home.php
    
            /de
                backend/
                    dashboard.php
    
                frontend/
                    home.php
    

    app/lang/en/backend/dashboard.php的示例内容:

    <?php
    return array(
        'welcome' => 'Welcome to Backend!'
    );
    

    您可以输出 welcome 键的值,例如 echo Lang::get('backend/dashboard.welcome');.

    我希望你明白了。更多详情,请随时查看official documentation

    【讨论】:

    • 好的。但是在哪里以及如何使用语言设置前端和后端数组?
    • @Mantoze 我在回答中添加了更多细节。
    【解决方案2】:

    与其在同一个浏览器中打开两个不同的选项卡,不如考虑打开两个不同的浏览器会话,以避免具有不同语言设置的后端和前端会话相互覆盖。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 2020-06-09
      • 2012-07-30
      • 2021-06-16
      • 2016-08-17
      • 1970-01-01
      • 2023-01-30
      相关资源
      最近更新 更多