Skip to content

Instantly share code, notes, and snippets.

@hewerthomn
Created February 25, 2017 16:56
Show Gist options
  • Save hewerthomn/267f00da6b644a40bd1025a07f374d28 to your computer and use it in GitHub Desktop.
Save hewerthomn/267f00da6b644a40bd1025a07f374d28 to your computer and use it in GitHub Desktop.
Como definir o locale pt_BR no Laravel 5.4
<?php
// config/app.php
/* ... */
/**
* PHP Locale
*/
'phplocale' => ['pt_BR', 'pt_BR.UTF-8'],
/* ... */
<?php
// app/Http/Kernel.php
/* ... */
protected $middleware = [
/* ... */
\App\Http\Middleware\LocaleSetup::class,
/* ... */
];
<?php
// app/Http/Middleware/LocaleSetup.php
namespace App\Http\Middleware;
use Closure;
class LocaleSetup
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$phplocale = config('app.phplocale');
// descomente se for salvar o locale na sessão
// if(session()->has('lang')) {
// app()->setLocale(session()->get('lang'));
// $phplocale = session()->get('locale');
// }
setlocale(LC_TIME, $phplocale);
return $next($request);
}
}
<?php
// routes/web.php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
$dia = date('d');
$ano = date('Y');
$diaSemana = ucwords(Carbon\Carbon::instance(new DateTime)->formatLocalized('%A'));
$mes = ucwords(Carbon\Carbon::instance(new DateTime)->formatLocalized('%B'));
return "{$diaSemana}, {$dia} de {$mes} de {$ano}";
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment