topview-digital/laravel-lang-switcher

1.0 2019-03-21 08:01 UTC

This package is auto-updated.

Last update: 2024-09-28 11:31:41 UTC


README

GitHub release Scrutinizer Code Quality Build Status Code Intelligence Status License Total Downloads HitCount

Laravel 语言切换器

语言切换器和区域切换的中间件和助手。

语言切换和区域切换功能中间件的实现。

要求

  • PHP >= 7.0
  • MySQL >= 5.7
  • Laravel >= 5.5

安装

使用 Composer 安装此包

composer require topview-digital/laravel-language-switch

Laravel 会自动注册 ServiceProvider

发布包

安装后,请通过以下命令发布资源

php artisan lang-switch:publish

配置包

请在 config/lang-switch.php 文件中配置您的设置,它应该看起来像下面这样

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Laravel-Language-Switcher Database Settings
    |--------------------------------------------------------------------------
    |
    | Here are database settings for Laravel-Language-Switcher builtin tables connction.
    |
    */

    'database' => [
        // Database connection for guards tables.
        'connection' => '',

    ],

    //the field name of the storage in  cookie
    'field' => 'locale',
];

一旦您配置了设置,您可以通过运行安装命令来设置包的表格。

php artisan lang-switch:install

配置全局中间件

全局使用:为了允许中间件自动为所有路由设置区域,请将 LangSwwitcher 中间件添加到 app/Http/Kernel.php 类的 $middleware 属性中

    protected $middlewareGroups = [
        'web' => [
            // ...
            \TopviewDigital\LangSwitcher\Middleware\LangSwitcher::class,
        ],
        //...

注册您的守卫中间件

在引导文件或您的服务提供者中

//Auth::user() is the guard method to get login user model, which could access the user field for setting locale
\TopviewDigital\LangSwitcher\Model\LangSwitcher::registerGuard(['class'=>'Auth','method'=>'user','middleware'=>'web']);

或者,您可以参考以下示例语言切换器控制器来注册您的守卫

<?php

namespace App\Backend\Controllers\API;

use App\User;
use Encore\Admin\Facades\Admin;
use App\Http\Controllers\Controller;
use TopviewDigital\LangSwitcher\Model\LangSwitcher;

class LanguageSelector extends Controller
{

    public function index()
    {
        LangSwitcher::registerGuard(['class' => 'Admin', 'method' => 'user', 'middleware' => 'admin']);
        LangSwitcher::switchLocale();
        return back();
    }
}

希望您喜欢它!谢谢!

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件