Skip to content

Commit

Permalink
Add feature to translate messages directly from translation files wit…
Browse files Browse the repository at this point in the history
…hout using "__()"
  • Loading branch information
kennedy-osaze committed May 21, 2022
1 parent 12b0e21 commit 24ea1a4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
14 changes: 6 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

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

## 1.0.2 - 2022-05-19
## [1.0.2] - 2022-05-19

## What's Changed

**Full Changelog**: https://github.com/kennedy-osaze/laravel-api-response/compare/v1.0.1...v1.0.2

## [1.0.0] - 2022-05-14

- First release 🚀
**Full Changelog**: <https://github.com/kennedy-osaze/laravel-api-response/compare/v1.0.1...v1.0.2>

## [1.0.1] - 2022-05-19

- Fix and update localisation files for publishing
- Update .gitattributes file

## [1.0.0] - 2022-05-14

- First release 🚀
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"require-dev": {
"kennedy-osaze/php-cs-fixer-config": "^2.0",
"nunomaduro/collision": "^6.2",
"orchestra/testbench": "^7.4",
"phpunit/phpunit": "^9.5"
},
Expand All @@ -38,7 +39,7 @@
}
},
"scripts": {
"test": "./vendor/bin/phpunit"
"test": "./vendor/bin/testbench package:test"
},
"config": {
"sort-packages": true
Expand Down
5 changes: 3 additions & 2 deletions src/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use InvalidArgumentException;
Expand Down Expand Up @@ -141,7 +142,7 @@ protected function getTranslatedMessageMeta(string $message, array &$data, bool
return [];
}

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

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

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

protected function extractTranslationDataFromResponsePayload(array &$data, string $message, string $prefix)
protected function extractTranslationDataFromResponsePayload(array &$data, string $message, ?string $prefix = null)
{
$parameters = $this->parseStringToTranslationParameters($message);

Expand Down
5 changes: 3 additions & 2 deletions src/Concerns/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ trait Translatable
*/
public function parseStringToTranslationParameters(string $string): array
{
$stringParts = explode(':', $string);
$stringParts = explode(':', Str::after($string, '::'));

$name = (string) array_shift($stringParts);
$prefix = Str::contains($string, '::') ? Str::before($string, '::').'::' : '';
$name = $prefix.array_shift($stringParts);

$attributes = [];

Expand Down
11 changes: 9 additions & 2 deletions tests/ApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ public function testFailedNestedDataValidationReturnsAppropriateResponseData()

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

$errors = $validator->errors();

$response = ApiResponse::fromFailedValidation($validator, $request);
$responseData = $response->getData(true);

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

public function testResponseMessageInTranslationFileIsTranslatedCorrectly()
{
$responseDataA = ApiResponse::create(200, 'Hello World')->getData(true);
$responseDataB = ApiResponse::create(200, 'api-response::success.example_code')->getData(true);

$this->assertSame('Hello World', $responseDataA['message']);
$this->assertSame(__('api-response::success.example_code'), $responseDataB['message']);
}

public function testErrorResponseMessageIsTranslatedCorrectly()
{
app()->setLocale('en');
Expand Down
2 changes: 2 additions & 0 deletions tests/TranslatableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function getTranslatableStringProvider()
['string:key1=value1', ['name' => 'string', 'attributes' => ['key1' => 'value1']]],
['string:key1=value1|', ['name' => 'string', 'attributes' => ['key1' => 'value1']]],
['string:key1=value1|key2=value2', ['name' => 'string', 'attributes' => ['key1' => 'value1', 'key2' => 'value2']]],
['api-response::string', ['name' => 'api-response::string', 'attributes' => []]],
['api-response::string:key1=value1', ['name' => 'api-response::string', 'attributes' => ['key1' => 'value1']]],
];
}

Expand Down

0 comments on commit 24ea1a4

Please sign in to comment.