Skip to content

Commit 556806f

Browse files
author
Samuel
committed
Added support translation to models on model not found exception
1 parent 0f80b00 commit 556806f

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ Set your exception codes on `config/json-exception-handler.php` on codes array.
102102

103103
You can add more fields and codes to `validation_fields` array.
104104

105+
You can add too your models on lang packages to return the Not Found response translated correctly.
106+
107+
In `resources/lang/vendor/exception/lang/$locale` in `exceptions` file you can set on `models` array. Example:
108+
109+
```php
110+
'models' => [
111+
'User' => 'Usuário',
112+
'Article' => 'Artigo',
113+
]
114+
```
115+
105116
## Using
106117

107118
Use the trait on your `App\Exception\Handler` and add method `jsonResponse()`

src/JsonHandlerServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public function boot()
1313
]);
1414

1515
$this->loadTranslationsFrom(__DIR__.'/resources/lang', 'exception');
16+
17+
$this->publishes([
18+
__DIR__.'/resources/lang' => resource_path('lang/vendor/exception'),
19+
]);
1620
}
1721

1822
public function register()

src/ModelNotFoundHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function modelNotFoundException(ModelNotFoundException $exception)
2020
'status' => 404,
2121
'code' => $this->getCode('model_not_found'),
2222
'source' => ['pointer' => 'data/id'],
23-
'title' => __('exception::exceptions.model_not_found.title', ['model' => $entitie]),
24-
'detail' => $exception->getMessage(),
23+
'title' => $exception->getMessage(),
24+
'detail' => __('exception::exceptions.model_not_found.title', ['model' => $entitie]),
2525
]];
2626

2727
$this->jsonApiResponse->setStatus(404);
@@ -38,6 +38,6 @@ public function extractEntitieName($model)
3838
{
3939
$entitieName = explode('\\', $model);
4040

41-
return end($entitieName);
41+
return __('exception::exceptions.models.'.end($entitieName));
4242
}
4343
}

src/resources/lang/en/exceptions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,13 @@
2828
],
2929
'client' => [
3030
'unavailable' => 'Service unavailable now.',
31+
],
32+
33+
/*
34+
| Can set your model here to translate the models names on ModelNotFoundException
35+
|
36+
*/
37+
'models' => [
38+
'User' => 'Usuário',
3139
]
3240
];

src/resources/lang/pt-br/exceptions.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,29 @@
1212
*/
1313

1414
'authentication' => [
15-
'detail' => 'A requisição foi feita por um usuário não autenticado.',
15+
'detail' => 'The request was made by an unauthenticated user.',
1616
],
1717
'authorization' => [
18-
'title' => 'Ação não permitida.',
18+
'title' => 'Action not allowed.',
1919
],
2020
'model_not_found' => [
21-
'title' => ':Model não encontrado(a)',
21+
'title' => ':Model not found',
2222
],
2323
'not_found_http' => [
24-
'message' => 'Rota não encontrada.',
24+
'message' => 'Route not found.',
2525
],
2626
'validation' => [
27-
'title' => 'Validação falhou no campo :field',
27+
'title' => ':Fails validation failed on field :field',
2828
],
2929
'client' => [
30-
'unavailable' => 'Serviço indisponível no momento.',
30+
'unavailable' => 'Service unavailable now.',
31+
],
32+
33+
/*
34+
| Can set your model here to translate the models names on ModelNotFoundException
35+
|
36+
*/
37+
'models' => [
38+
'User' => 'Usuário',
3139
]
3240
];

0 commit comments

Comments
 (0)