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

@@ -11,22 +11,22 @@ class TicketGenerationException extends RuntimeException
{
public static function invalidQuantity(): self
{
return new self('La cantidad de tickets a generar debe ser mayor a cero.');
return new self(__('api.ticket.positive_quantity'));
}
public static function emptyBundle(CatalogItem $bundle): self
{
return new self("El bundle {$bundle->id} no tiene componentes.");
return new self(__('api.ticket.empty_bundle', ['bundle' => $bundle->id]));
}
public static function ticketsDisabled(CatalogItem $catalogItem): self
{
return new self("El producto {$catalogItem->id} no tiene tickets habilitados.");
return new self(__('api.ticket.disabled', ['product' => $catalogItem->id]));
}
public static function maximumUseDateReached(CatalogItem $catalogItem): self
{
return new self("El producto {$catalogItem->id} alcanzó su fecha máxima de uso.");
return new self(__('api.ticket.expired', ['product' => $catalogItem->id]));
}
public static function variantNotFound(
@@ -34,17 +34,20 @@ class TicketGenerationException extends RuntimeException
int $variantId,
): self {
return new self(
"La variante {$variantId} no pertenece al producto {$catalogItem->id}.",
__('api.ticket.invalid_variant', [
'variant' => $variantId,
'product' => $catalogItem->id,
]),
);
}
public static function purchaseWithoutUser(Purchase $purchase): self
{
return new self("La compra {$purchase->id} no tiene un usuario asociado.");
return new self(__('api.ticket.purchase_without_user', ['purchase' => $purchase->id]));
}
public static function catalogItemNotFound(PurchaseItem $purchaseItem): self
{
return new self("No se encontró el producto de la línea de compra {$purchaseItem->id}.");
return new self(__('api.ticket.product_not_found', ['purchase_item' => $purchaseItem->id]));
}
}

View File

@@ -0,0 +1,7 @@
<?php
namespace App\Domains\Ticket\Exceptions;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class TicketNotAvailableException extends NotFoundHttpException {}