Skip to content

Commit 24ea1a4

Browse files
committed
Add feature to translate messages directly from translation files without using "__()"
1 parent 12b0e21 commit 24ea1a4

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## 1.0.2 - 2022-05-19
5+
## [1.0.2] - 2022-05-19
66

7-
## What's Changed
8-
9-
**Full Changelog**: https://github.com/kennedy-osaze/laravel-api-response/compare/v1.0.1...v1.0.2
10-
11-
## [1.0.0] - 2022-05-14
12-
13-
- First release 🚀
7+
**Full Changelog**: <https://github.com/kennedy-osaze/laravel-api-response/compare/v1.0.1...v1.0.2>
148

159
## [1.0.1] - 2022-05-19
1610

1711
- Fix and update localisation files for publishing
1812
- Update .gitattributes file
13+
14+
## [1.0.0] - 2022-05-14
15+
16+
- First release 🚀

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
},
2525
"require-dev": {
2626
"kennedy-osaze/php-cs-fixer-config": "^2.0",
27+
"nunomaduro/collision": "^6.2",
2728
"orchestra/testbench": "^7.4",
2829
"phpunit/phpunit": "^9.5"
2930
},
@@ -38,7 +39,7 @@
3839
}
3940
},
4041
"scripts": {
41-
"test": "./vendor/bin/phpunit"
42+
"test": "./vendor/bin/testbench package:test"
4243
},
4344
"config": {
4445
"sort-packages": true

src/ApiResponse.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Http\Request;
1111
use Illuminate\Http\Resources\Json\JsonResource;
1212
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Facades\Lang;
1314
use Illuminate\Support\Str;
1415
use Illuminate\Support\Traits\Conditionable;
1516
use InvalidArgumentException;
@@ -141,7 +142,7 @@ protected function getTranslatedMessageMeta(string $message, array &$data, bool
141142
return [];
142143
}
143144

144-
$translationPrefix = 'api-response::'.config("api-response.translation.{$fileKey}");
145+
$translationPrefix = Lang::has($message) ? null : 'api-response::'.config("api-response.translation.{$fileKey}");
145146

146147
$translated = $this->extractTranslationDataFromResponsePayload($data, $message, $translationPrefix);
147148

@@ -152,7 +153,7 @@ protected function getTranslatedMessageMeta(string $message, array &$data, bool
152153
return array_merge($translated, $this->pullErrorCodeFromData($data, $message, $translated['key']));
153154
}
154155

155-
protected function extractTranslationDataFromResponsePayload(array &$data, string $message, string $prefix)
156+
protected function extractTranslationDataFromResponsePayload(array &$data, string $message, ?string $prefix = null)
156157
{
157158
$parameters = $this->parseStringToTranslationParameters($message);
158159

src/Concerns/Translatable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ trait Translatable
2121
*/
2222
public function parseStringToTranslationParameters(string $string): array
2323
{
24-
$stringParts = explode(':', $string);
24+
$stringParts = explode(':', Str::after($string, '::'));
2525

26-
$name = (string) array_shift($stringParts);
26+
$prefix = Str::contains($string, '::') ? Str::before($string, '::').'::' : '';
27+
$name = $prefix.array_shift($stringParts);
2728

2829
$attributes = [];
2930

tests/ApiResponseTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,6 @@ public function testFailedNestedDataValidationReturnsAppropriateResponseData()
302302

303303
$validator = Validator::make($request->all(), $rules, ['user.names.last.min' => 'Not less than 3']);
304304

305-
$errors = $validator->errors();
306-
307305
$response = ApiResponse::fromFailedValidation($validator, $request);
308306
$responseData = $response->getData(true);
309307

@@ -367,6 +365,15 @@ public function testSuccessfulResponseMessageIsTranslatedCorrectly()
367365
$this->assertSame(__('api-response::success.Example response message'), $responseDataB['message']);
368366
}
369367

368+
public function testResponseMessageInTranslationFileIsTranslatedCorrectly()
369+
{
370+
$responseDataA = ApiResponse::create(200, 'Hello World')->getData(true);
371+
$responseDataB = ApiResponse::create(200, 'api-response::success.example_code')->getData(true);
372+
373+
$this->assertSame('Hello World', $responseDataA['message']);
374+
$this->assertSame(__('api-response::success.example_code'), $responseDataB['message']);
375+
}
376+
370377
public function testErrorResponseMessageIsTranslatedCorrectly()
371378
{
372379
app()->setLocale('en');

tests/TranslatableTraitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function getTranslatableStringProvider()
3535
['string:key1=value1', ['name' => 'string', 'attributes' => ['key1' => 'value1']]],
3636
['string:key1=value1|', ['name' => 'string', 'attributes' => ['key1' => 'value1']]],
3737
['string:key1=value1|key2=value2', ['name' => 'string', 'attributes' => ['key1' => 'value1', 'key2' => 'value2']]],
38+
['api-response::string', ['name' => 'api-response::string', 'attributes' => []]],
39+
['api-response::string:key1=value1', ['name' => 'api-response::string', 'attributes' => ['key1' => 'value1']]],
3840
];
3941
}
4042

0 commit comments

Comments
 (0)