From 2801547f9ad2d31582290b8f7ea4a672803fab71 Mon Sep 17 00:00:00 2001 From: DJERY-TOM Date: Tue, 28 Apr 2020 18:35:28 +0100 Subject: [PATCH] + Multi Language --- app/Exceptions/Handler.php | 4 +- ...roller.php => WalletServiceController.php} | 11 ++---- app/Http/Middleware/Localization.php | 37 +++++++++++++++++++ app/Services/WalletService.php | 12 +++--- app/Traits/ConsumesExternalService.php | 3 +- bootstrap/app.php | 5 ++- resources/lang/en/errors.php | 5 +++ resources/lang/fr/errors.php | 5 +++ routes/web.php | 32 ++++++++-------- 9 files changed, 81 insertions(+), 33 deletions(-) rename app/Http/Controllers/{WalletController.php => WalletServiceController.php} (83%) create mode 100644 app/Http/Middleware/Localization.php create mode 100644 resources/lang/en/errors.php create mode 100644 resources/lang/fr/errors.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 8f05ec7..de0ddad 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -69,7 +69,7 @@ class Handler extends ExceptionHandler { $model = strtolower(class_basename($exception->getModel())); - return $this->errorResponse("Does not exist any instance of {$model} with given id", + return $this->errorResponse(trans('errors.model_not_found',['model'=>$model]), Response::HTTP_NOT_FOUND); } @@ -117,7 +117,7 @@ class Handler extends ExceptionHandler return parent::render($request,$exception); } - return $this->errorResponse('Unexcepted error. Try later', + return $this->errorResponse(trans('errors.unexpected_error'), Response::HTTP_INTERNAL_SERVER_ERROR); } } diff --git a/app/Http/Controllers/WalletController.php b/app/Http/Controllers/WalletServiceController.php similarity index 83% rename from app/Http/Controllers/WalletController.php rename to app/Http/Controllers/WalletServiceController.php index ed3a1af..7fb45ac 100644 --- a/app/Http/Controllers/WalletController.php +++ b/app/Http/Controllers/WalletServiceController.php @@ -8,7 +8,7 @@ use App\Traits\ApiResponser; use Illuminate\Http\Request; use Illuminate\Http\Response; -class WalletController extends Controller +class WalletServiceController extends Controller { use ApiResponser; /** @@ -28,25 +28,22 @@ class WalletController extends Controller public function get(Request $request) { - return $this->successResponse($this->walletService->get( - substr($request->getRequestUri(),strlen(env('WALLET_SERVICE_NAME'))+1), $request->all() + substr($request->getRequestUri(),strlen(env('WALLET_SERVICE_NAME'))+1), $request->all(),$request->header() )); } public function post(Request $request) { - return $this->successResponse($this->walletService->post( - substr($request->getRequestUri(),strlen(env('WALLET_SERVICE_NAME'))+1), $request->all() + substr($request->getRequestUri(),strlen(env('WALLET_SERVICE_NAME'))+1), $request->all(),$request->header() )); } public function put(Request $request) { - return $this->successResponse($this->walletService->put( - substr($request->getRequestUri(),strlen(env('WALLET_SERVICE_NAME'))+1), $request->all() + substr($request->getRequestUri(),strlen(env('WALLET_SERVICE_NAME'))+1), $request->all(),$request->header() )); } diff --git a/app/Http/Middleware/Localization.php b/app/Http/Middleware/Localization.php new file mode 100644 index 0000000..24f09a8 --- /dev/null +++ b/app/Http/Middleware/Localization.php @@ -0,0 +1,37 @@ +hasHeader('X-localization')){ + $local = $request->header('X-localization'); + $pos=strpos($local,"-"); + if($pos!=false){ + $local=strtolower(explode("-",$local)[0]); + } + $local= in_array($local, $enLangs) ? 'en' : 'fr'; + }else{ + $local ='fr'; + } + // set laravel localization + app()->setLocale($local); + // continue request + return $next($request); + } +} diff --git a/app/Services/WalletService.php b/app/Services/WalletService.php index 9eb4d51..a99d442 100644 --- a/app/Services/WalletService.php +++ b/app/Services/WalletService.php @@ -24,18 +24,18 @@ class WalletService $this->key = config('services.wallet_service.key'); } - public function post($uri , $data) + public function post($uri , $data, $headers) { - return $this->perfomRequest('POST',$uri,$data); + return $this->perfomRequest('POST',$uri,$data,$headers); } - public function get($uri , $data) + public function get($uri , $data, $headers) { - return $this->perfomRequest('GET',$uri,$data); + return $this->perfomRequest('GET',$uri,$data,$headers); } - public function put($uri , $data) + public function put($uri , $data, $headers) { - return $this->perfomRequest('PUT',$uri,$data); + return $this->perfomRequest('PUT',$uri,$data,$headers); } } diff --git a/app/Traits/ConsumesExternalService.php b/app/Traits/ConsumesExternalService.php index 215109e..e22d79b 100644 --- a/app/Traits/ConsumesExternalService.php +++ b/app/Traits/ConsumesExternalService.php @@ -14,13 +14,14 @@ trait ConsumesExternalService * @param array $headers * @return string */ - public function perfomRequest($method, $requestUrl, $body = [], $formParams = [], $headers = []) + public function perfomRequest($method, $requestUrl, $body = [], $headers = [], $formParams = []) { $client = new Client([ 'base_uri' => $this->baseUri, ]); if(isset($this->key)){ + unset($headers['authorization']); $headers['Authorization'] = $this->key; } diff --git a/bootstrap/app.php b/bootstrap/app.php index 879e5a9..88e4d9e 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -78,9 +78,10 @@ $app->configure('app'); | */ -// $app->middleware([ + $app->middleware([ + App\Http\Middleware\Localization::class, // App\Http\Middleware\ExampleMiddleware::class -// ]); + ]); $app->routeMiddleware([ 'auth' => App\Http\Middleware\Authenticate::class, diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php new file mode 100644 index 0000000..c7e2131 --- /dev/null +++ b/resources/lang/en/errors.php @@ -0,0 +1,5 @@ + 'Does not exist any instance of :model with given id', + 'unexpected_error'=> 'Unexpected error. Try later' +]; diff --git a/resources/lang/fr/errors.php b/resources/lang/fr/errors.php new file mode 100644 index 0000000..52e681c --- /dev/null +++ b/resources/lang/fr/errors.php @@ -0,0 +1,5 @@ + 'Il n\'existe aucune instance de :model avec l\'id donné', + 'unexpected_error'=> 'Erreur inattendue. Essayer plus tard' +]; diff --git a/routes/web.php b/routes/web.php index 05b5922..c39bbb9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -34,17 +34,19 @@ /** * Routes for MobileBackendTest */ - $router->group(['prefix' => '/mobilebackendtest/interacted' , 'middleware' => 'auth:api'], function () use ($router){ + $router->group(['prefix' => '/mobilebackendtest/interacted'], function () use ($router){ $router->post('LoginAction', 'MobileBackendTestController@action'); - $router->post('BalanceAction', 'MobileBackendTestController@action'); - $router->post('ConfigAction', 'MobileBackendTestController@action'); - $router->post('DemandeAction', 'MobileBackendTestController@action'); $router->post('MembersAction', 'MobileBackendTestController@action'); - $router->post('LocationAction', 'MobileBackendTestController@action'); - $router->post('NetworkAction', 'MobileBackendTestController@action'); - $router->post('WalletAction', 'MobileBackendTestController@action'); + $router->group(['middleware' => 'auth:api'], function () use ($router){ + $router->post('BalanceAction', 'MobileBackendTestController@action'); + $router->post('ConfigAction', 'MobileBackendTestController@action'); + $router->post('DemandeAction', 'MobileBackendTestController@action'); + $router->post('LocationAction', 'MobileBackendTestController@action'); + $router->post('NetworkAction', 'MobileBackendTestController@action'); + $router->post('WalletAction', 'MobileBackendTestController@action'); + }); }); /** @@ -54,22 +56,22 @@ // Transactions routes $router->group(['prefix' => '/transactions'] , function () use ($router){ - $router->post('','WalletController@post'); - $router->get('{id_wallet}','WalletController@get'); + $router->post('','WalletServiceController@post'); + $router->get('{id_wallet}','WalletServiceController@get'); }); // Credits routes $router->group(['prefix' => '/credits'] , function () use ($router){ - $router->put('treatDemand/{id_demand}','WalletController@put'); - $router->put('cancelDemand/{id_demand}','WalletController@put'); + $router->put('treatDemand/{id_demand}','WalletServiceController@put'); + $router->put('cancelDemand/{id_demand}','WalletServiceController@put'); }); - $router->put('/virement/{id_wallet}','WalletController@put'); + $router->put('/virement/{id_wallet}','WalletServiceController@put'); // Wallets routes $router->group(['prefix' => '/wallets'] , function () use ($router){ - $router->get('{id_agent}/activated', 'WalletController@get'); - $router->get('{id_wallet}', 'WalletController@get'); - $router->post('', 'WalletController@post'); + $router->get('{id_agent}/activated', 'WalletServiceController@get'); + $router->get('{id_wallet}', 'WalletServiceController@get'); + $router->post('', 'WalletServiceController@post'); }); });