Skip to content

Instantly share code, notes, and snippets.

@Yangwendaxia
Last active November 20, 2016 07:43
Show Gist options
  • Save Yangwendaxia/dca9a47eab059bb56ea2b1a9a547fe2d to your computer and use it in GitHub Desktop.
Save Yangwendaxia/dca9a47eab059bb56ea2b1a9a547fe2d to your computer and use it in GitHub Desktop.

1. Laravel 5.1 上实现 Carbon 类本地化

在 app/Providers/AppServiceProvider.php 的boot()方法加上:

\Carbon\Carbon::setLocale('zh');

参考http://carbon.nesbot.com/docs/#api-localization

#2. Model Factory 生成本地化数据 在 AppServiceProvider.php boot() 方法内加入如下内容:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Faker\Generator as FakerGenrator;
use Faker\Factory as FakerFactory;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->app->singleton(FakerGenrator::class,function(){
            return FakerFactory::create('zh_CN');
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

注意类开头的 use Faker\Generator as FakerGenrator; ** use Faker\Factory as FakerFactory;** Faker 支持的其他语言,参见 https://github.com/fzaninotto/Faker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment