Implement localization for API responses and error messages
- Added localization support for API responses in English and Spanish. - Introduced a middleware to set the API locale based on the Accept-Language header. - Updated various exception messages and validation responses to use localized strings. - Created new language files for English and Spanish translations. - Refactored existing code to replace hardcoded messages with localized strings. - Added tests to verify localization functionality and response correctness.
This commit is contained in:
40
tests/Feature/Localization/ApiLocalizationTest.php
Normal file
40
tests/Feature/Localization/ApiLocalizationTest.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Localization;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class ApiLocalizationTest extends TestCase
|
||||
{
|
||||
public function test_it_negotiates_spanish_from_accept_language(): void
|
||||
{
|
||||
$this->withHeader('Accept-Language', 'es-AR,es;q=0.9')
|
||||
->postJson('/api/login')
|
||||
->assertUnprocessable()
|
||||
->assertHeader('Content-Language', 'es')
|
||||
->assertJsonPath('errors.email.0', 'El campo email es obligatorio.')
|
||||
->assertJsonPath('errors.password.0', 'El campo contraseña es obligatorio.');
|
||||
}
|
||||
|
||||
public function test_it_negotiates_english_from_accept_language(): void
|
||||
{
|
||||
$this->withHeader('Accept-Language', 'en-US,en;q=0.9')
|
||||
->postJson('/api/login')
|
||||
->assertUnprocessable()
|
||||
->assertHeader('Content-Language', 'en')
|
||||
->assertJsonPath('errors.email.0', 'The email field is required.')
|
||||
->assertJsonPath('errors.password.0', 'The password field is required.');
|
||||
}
|
||||
|
||||
public function test_it_localizes_framework_error_responses(): void
|
||||
{
|
||||
$this->withHeader('Accept-Language', 'en')
|
||||
->getJson('/api/route-that-does-not-exist')
|
||||
->assertNotFound()
|
||||
->assertHeader('Content-Language', 'en')
|
||||
->assertJson([
|
||||
'code' => 'errors.not_found',
|
||||
'message' => 'The requested resource was not found.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user