Skip to content

Commit

Permalink
本地化
Browse files Browse the repository at this point in the history
  • Loading branch information
liyu001989 committed Aug 7, 2020
1 parent c23d74a commit 3078af3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
18 changes: 18 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Arr;
use Throwable;

class Handler extends ExceptionHandler
Expand Down Expand Up @@ -52,4 +53,21 @@ public function render($request, Throwable $exception)
{
return parent::render($request, $exception);
}

protected function convertExceptionToArray(Throwable $e)
{
return config('app.debug') ? [
'message' => $e->getMessage(),
'code' => $e->getCode(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => collect($e->getTrace())->map(function ($trace) {
return Arr::except($trace, ['args']);
})->all(),
] : [
'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error',
'code' => $e->getCode(),
];
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/AuthorizationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function store(AuthorizationRequest $request)
$credentials['password'] = $request->password;

if (!$token = \Auth::guard('api')->attempt($credentials)) {
throw new AuthenticationException('用户名或密码错误');
throw new AuthenticationException(trans('auth.failed'));
}

return $this->respondWithToken($token)->setStatusCode(201);
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/Api/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;
use Symfony\Component\HttpKernel\Exception\HttpException;

class Controller extends BaseController
{
//
public function errorResponse($statusCode, $message=null, $code=0)
{
throw new HttpException($statusCode, $message, null, [], $code);
}
}
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
// 接口语言设置
'change-locale' => \App\Http\Middleware\ChangeLocale::class,
];
}
18 changes: 18 additions & 0 deletions app/Http/Middleware/ChangeLocale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Middleware;

use Closure;

class ChangeLocale
{
public function handle($request, Closure $next)
{
$language = $request->header('accept-language');
if ($language) {
\App::setLocale($language);
}

return $next($request);
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
Route::prefix('v1')
->namespace('Api')
->middleware('change-locale')
->name('api.v1.')
->group(function () {

Expand Down

0 comments on commit 3078af3

Please sign in to comment.