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:
2026-07-28 10:19:39 -03:00
parent 754aeebe3a
commit ff9d7728e8
34 changed files with 654 additions and 114 deletions

View File

@@ -135,7 +135,7 @@ class PurchaseController extends Controller
if ($updated === 0) {
throw ValidationException::withMessages([
'purchase' => 'The purchase is no longer available for payment.',
'purchase' => __('api.purchase.not_available_for_payment'),
]);
}
@@ -159,8 +159,14 @@ class PurchaseController extends Controller
],
]);
} catch (\Exception $e) {
Log::error('Unable to retrieve TelePagos account information.', [
'purchase_id' => $compra->id,
'exception' => $e,
]);
return response()->json([
'message' => 'Error getting account info: '.$e->getMessage(),
'code' => 'integration.account_info_failed',
'message' => __('api.integration.account_info_failed'),
], 500);
}
}
@@ -182,8 +188,14 @@ class PurchaseController extends Controller
'qr_code' => $qrResponse['qr_code'] ?? '',
]);
} catch (\Exception $e) {
Log::error('Unable to generate TelePagos QR code.', [
'purchase_id' => $compra->id,
'exception' => $e,
]);
return response()->json([
'message' => 'Error generating QR: '.$e->getMessage(),
'code' => 'integration.qr_generation_failed',
'message' => __('api.integration.qr_generation_failed'),
], 500);
}
@@ -197,7 +209,8 @@ class PurchaseController extends Controller
}
return response()->json([
'message' => 'Invalid payment method.',
'code' => 'integration.invalid_payment_method',
'message' => __('api.integration.invalid_payment_method'),
], 400);
}
@@ -222,7 +235,7 @@ class PurchaseController extends Controller
protected function resolveScopedPurchase(Tenant $tenant, int $userId, Purchase $purchase): Purchase
{
if ($purchase->tenant_codigo !== $tenant->codigo || $purchase->user_id !== $userId) {
throw new NotFoundHttpException('Purchase not found for tenant.');
throw new NotFoundHttpException(__('api.errors.not_found'));
}
return $purchase;