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:
@@ -35,7 +35,9 @@ class LoginControllerTest extends TestCase
|
||||
|
||||
$response
|
||||
->assertOk()
|
||||
->assertJsonPath('message', 'Sesion iniciada correctamente.')
|
||||
->assertHeader('Content-Language', 'es')
|
||||
->assertJsonPath('code', 'auth.login_success')
|
||||
->assertJsonPath('message', 'Sesión iniciada correctamente.')
|
||||
->assertJsonPath('token_type', 'Bearer')
|
||||
->assertJsonPath('user.id', $user->id)
|
||||
->assertJsonPath('user.nombre_apellido', 'Grace Hopper')
|
||||
@@ -85,7 +87,8 @@ class LoginControllerTest extends TestCase
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson('/api/logout')
|
||||
->assertOk()
|
||||
->assertJsonPath('message', 'Sesion cerrada correctamente.');
|
||||
->assertJsonPath('code', 'auth.logout_success')
|
||||
->assertJsonPath('message', 'Sesión cerrada correctamente.');
|
||||
|
||||
$this->assertDatabaseCount('personal_access_tokens', 0);
|
||||
}
|
||||
@@ -118,6 +121,34 @@ class LoginControllerTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_localizes_success_and_validation_responses_from_accept_language(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
User::query()->create([
|
||||
'nombre_apellido' => 'Grace Hopper',
|
||||
'email' => 'grace@example.com',
|
||||
'password' => Hash::make('secret123'),
|
||||
]);
|
||||
|
||||
$this->withHeader('Accept-Language', 'en-US,en;q=0.9')
|
||||
->postJson('/api/login', [
|
||||
'email' => 'grace@example.com',
|
||||
'password' => 'secret123',
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
])
|
||||
->assertOk()
|
||||
->assertHeader('Content-Language', 'en')
|
||||
->assertJsonPath('code', 'auth.login_success')
|
||||
->assertJsonPath('message', 'Signed in successfully.');
|
||||
|
||||
$this->withHeader('Accept-Language', 'en')
|
||||
->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_replaces_the_active_user_cart_with_the_guest_cart_on_login(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
|
||||
Reference in New Issue
Block a user