From 24ea1a4ffef64faecb49c9828b86a18cc9627230 Mon Sep 17 00:00:00 2001 From: Kennedy Osaze Date: Sat, 21 May 2022 19:52:20 +0100 Subject: [PATCH] Add feature to translate messages directly from translation files without using "__()" --- CHANGELOG.md | 14 ++++++-------- composer.json | 3 ++- src/ApiResponse.php | 5 +++-- src/Concerns/Translatable.php | 5 +++-- tests/ApiResponseTest.php | 11 +++++++++-- tests/TranslatableTraitTest.php | 2 ++ 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1828f4..bbb3a28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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**: ## [1.0.1] - 2022-05-19 - Fix and update localisation files for publishing - Update .gitattributes file + +## [1.0.0] - 2022-05-14 + +- First release 🚀 diff --git a/composer.json b/composer.json index 1881768..1dab9e5 100644 --- a/composer.json +++ b/composer.json @@ -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" }, @@ -38,7 +39,7 @@ } }, "scripts": { - "test": "./vendor/bin/phpunit" + "test": "./vendor/bin/testbench package:test" }, "config": { "sort-packages": true diff --git a/src/ApiResponse.php b/src/ApiResponse.php index e56b230..7ef74f8 100644 --- a/src/ApiResponse.php +++ b/src/ApiResponse.php @@ -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; @@ -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); @@ -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); diff --git a/src/Concerns/Translatable.php b/src/Concerns/Translatable.php index c0c37eb..98de6d3 100644 --- a/src/Concerns/Translatable.php +++ b/src/Concerns/Translatable.php @@ -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 = []; diff --git a/tests/ApiResponseTest.php b/tests/ApiResponseTest.php index 0fd6fc9..7b6fcd1 100644 --- a/tests/ApiResponseTest.php +++ b/tests/ApiResponseTest.php @@ -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); @@ -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'); diff --git a/tests/TranslatableTraitTest.php b/tests/TranslatableTraitTest.php index 4ecdad7..235a0ba 100644 --- a/tests/TranslatableTraitTest.php +++ b/tests/TranslatableTraitTest.php @@ -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']]], ]; }