项目下输入: composer require mcamara/laravel-localization
在 config/app.php

        'providers' => [
        // [...]
                Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider::class,
        ],  
        'aliases' => [
        // [...]
                'LaravelLocalization' => Mcamara\LaravelLocalization\Facades\LaravelLocalization::class,
        ],
然后输入命令:
~ php artisan vendor:publish –provider="Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider"
然后 在 config/laravellocalization.php 配置你的语言配置,把zh 注释打开。

你也可以使用另一种方式,单独配置语言文件conf
新建 app/providers/ConfigServicePovider.php 文件

输入如下内容,并添加到config/app.php ( App\Providers\ConfigServiceProvider::class )

<?php 
namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ConfigServiceProvider extends ServiceProvider {
    public function register()
    {
        config([
            'laravellocalization.supportedLocales' => [
                'en'  => array( 'name' => 'English', 'script' => 'Latn', 'native' => 'English' ),
                'zh'  => ['name' => 'Chinese (Simplified)',   'script' => 'Hans', 'native' => '简体中文', 'regional' => 'zh'],
            ],

            'laravellocalization.useAcceptLanguageHeader' => true,

            'laravellocalization.hideDefaultLocaleInURL' => true
        ]);
    }
然后在配置路由,在routes/web.php增加:

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
    /** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
    Route::get('/', function()
    {
        return trans('welcome.name');
    });

});

在resources增加对应zh目录,建立welcome.php
内容为:
return [‘name’=>’你好’];
在resources增加对应en目录,建立welcome.php
内容为:
return [‘name’=>’hello’];

然后访问
域名.com/en
域名.com/cn
域名.com
就可以看到翻译文件生效了

中间件形式
在 app/Http/Kernel.php 添加如下内容

<?php namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {
    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        /**** OTHER MIDDLEWARE ****/
        'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
        'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
        'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
                'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class
        // REDIRECTION MIDDLEWARE
    ];
}

路由文件这样写:

// app/Http/routes.php

Route::group(
[
    'prefix' => LaravelLocalization::setLocale(),
    'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
],
function()
{
    /** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
    Route::get('/', function()
    {
        return View::make('hello');
    });

    Route::get('test',function(){
        return View::make('test');
    });
});

更多内容请参考: https://github.com/mcamara/laravel-localization[/danger]

PHP 如何使用 Service 模式?

若将商业逻辑都写在 controller,会造成 controller 肥大而难以维护,基于SOLID原则,我们应该使用 Service 模式辅助 controller,将相关的商业逻辑封装在不...

阅读全文

php 抓取自如友家首页的区域和地铁数据

步骤使用composer安装 php抓取扩展包 composer require jaeger/querylist分析自如首页html结构经过分析发现区域和地铁数据在类名.clearfix.filterLi...

阅读全文

QueryList 一套简洁、优雅、可扩展的PHP采集工具(爬虫),基于phpQuery。

QueryList 简介 QueryList是一套简洁、优雅、可扩展的PHP采集工具(爬虫),基于phpQuery。 Github: https://github.com/jae-jae/QueryList packagist https:/...

阅读全文

欢迎留言