From e42469b0908c9650f02988a5bb7a33e46ddb3fa3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 21 Jul 2016 16:41:16 +0200 Subject: [PATCH] Added translations for error messages returned by the REST API --- module/Rest/lang/es.mo | Bin 0 -> 1754 bytes module/Rest/lang/es.po | 60 ++++++++++++++++++ .../src/Action/AuthenticateMiddleware.php | 15 +++-- .../src/Action/CreateShortcodeMiddleware.php | 25 ++++++-- .../Rest/src/Action/GetVisitsMiddleware.php | 15 +++-- .../src/Action/ListShortcodesMiddleware.php | 13 +++- .../Rest/src/Action/ResolveUrlMiddleware.php | 20 ++++-- .../CheckAuthenticationMiddleware.php | 17 +++-- module/Rest/src/Service/RestTokenService.php | 2 +- module/Rest/src/Util/RestUtils.php | 2 +- 10 files changed, 141 insertions(+), 28 deletions(-) create mode 100644 module/Rest/lang/es.mo create mode 100644 module/Rest/lang/es.po diff --git a/module/Rest/lang/es.mo b/module/Rest/lang/es.mo new file mode 100644 index 0000000000000000000000000000000000000000..2a8d4431a081c2eee3abe6973ebe7cb2bac0ce20 GIT binary patch literal 1754 zcmZ`(L5m|r6fSkuHJ}F%3xen~BDhHRBw3jmd&og&XJs(68)h=Ho}{MxB`LR4)mBw! zHuwt!4_^EMUIY&+vf#;E4*meaqu@W#f8bZ$Njl8#HoSc4>Q~?U-m7}=?^myVC-Agj zufuZK%dp>J;rSPK1NI+`VAr1!;v3*i;G4kTfP28dfHCluXN7nh*ayA_l)xVFE8suy z`5E{I-oL$8`~3n8vHk?^hV|!#xC{IP_#yDd3qo{&_kls**T7rAAAq0}&%v(4u)TUf zFR-=B=f!$SytG1W0V$oI4(L)k(#BI^&0J-iiDNyNxyqtA zNtqgt*+L;1&2h7@;-d{AO;InY*AyY-O!}w0H_^no=7$BRo-<-=A=RX5X)Q|rnJK|L zXXL)F16>%%YOfEWT@|XmK7)1$_yxG4<1G4=twTu@I-BN7KcH^hX$|jc9PWHNDlR?#+XS;LMaZ<-=N8LbbFF{U^K#&EN*gdE~Mx3?FR%)gia& zSlVULNYT{Y8mdbICrJA%FtrR1UAHV zy!;itW@x_r9t~{V(y}K_@{uNklk<$9aJy@cm`)kfKvrRO$#ckIG;!ts zIKzS%JAabIc{2{S#A80BGK0t~Ojyq)K3#B%bFCV@5zjdNkYAHLW)Zv)BV9hq)Fc$Z zqIE)hi$PVMdPZzl4*pGEGNxngmp^JRb3@1@2&(E>C5l;4jRMSpqpLx-IqQ(*6|N&T zu7-gLXRxW@;|xLOl|$WrestTokenService = $restTokenService; + $this->translator = $translator; } /** @@ -40,7 +47,7 @@ class AuthenticateMiddleware extends AbstractRestMiddleware if (! isset($authData['username'], $authData['password'])) { return new JsonResponse([ 'error' => RestUtils::INVALID_ARGUMENT_ERROR, - 'message' => 'You have to provide both "username" and "password"' + 'message' => $this->translator->translate('You have to provide both "username" and "password"'), ], 400); } @@ -50,7 +57,7 @@ class AuthenticateMiddleware extends AbstractRestMiddleware } catch (AuthenticationException $e) { return new JsonResponse([ 'error' => RestUtils::getRestErrorCodeFromException($e), - 'message' => 'Invalid username and/or password', + 'message' => $this->translator->translate('Invalid username and/or password'), ], 401); } } diff --git a/module/Rest/src/Action/CreateShortcodeMiddleware.php b/module/Rest/src/Action/CreateShortcodeMiddleware.php index fdb0ade3..fe3074fa 100644 --- a/module/Rest/src/Action/CreateShortcodeMiddleware.php +++ b/module/Rest/src/Action/CreateShortcodeMiddleware.php @@ -10,6 +10,7 @@ use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; use Shlinkio\Shlink\Rest\Util\RestUtils; use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Uri; +use Zend\I18n\Translator\TranslatorInterface; class CreateShortcodeMiddleware extends AbstractRestMiddleware { @@ -21,18 +22,27 @@ class CreateShortcodeMiddleware extends AbstractRestMiddleware * @var array */ private $domainConfig; + /** + * @var TranslatorInterface + */ + private $translator; /** * GenerateShortcodeMiddleware constructor. * * @param UrlShortenerInterface|UrlShortener $urlShortener + * @param TranslatorInterface $translator * @param array $domainConfig * - * @Inject({UrlShortener::class, "config.url_shortener.domain"}) + * @Inject({UrlShortener::class, "translator", "config.url_shortener.domain"}) */ - public function __construct(UrlShortenerInterface $urlShortener, array $domainConfig) - { + public function __construct( + UrlShortenerInterface $urlShortener, + TranslatorInterface $translator, + array $domainConfig + ) { $this->urlShortener = $urlShortener; + $this->translator = $translator; $this->domainConfig = $domainConfig; } @@ -48,7 +58,7 @@ class CreateShortcodeMiddleware extends AbstractRestMiddleware if (! isset($postData['longUrl'])) { return new JsonResponse([ 'error' => RestUtils::INVALID_ARGUMENT_ERROR, - 'message' => 'A URL was not provided', + 'message' => $this->translator->translate('A URL was not provided'), ], 400); } $longUrl = $postData['longUrl']; @@ -67,12 +77,15 @@ class CreateShortcodeMiddleware extends AbstractRestMiddleware } catch (InvalidUrlException $e) { return new JsonResponse([ 'error' => RestUtils::getRestErrorCodeFromException($e), - 'message' => sprintf('Provided URL "%s" is invalid. Try with a different one.', $longUrl), + 'message' => sprintf( + $this->translator->translate('Provided URL "%s" is invalid. Try with a different one.'), + $longUrl + ), ], 400); } catch (\Exception $e) { return new JsonResponse([ 'error' => RestUtils::UNKNOWN_ERROR, - 'message' => 'Unexpected error occured', + 'message' => $this->translator->translate('Unexpected error occurred'), ], 500); } } diff --git a/module/Rest/src/Action/GetVisitsMiddleware.php b/module/Rest/src/Action/GetVisitsMiddleware.php index 3e5bfbaf..75765389 100644 --- a/module/Rest/src/Action/GetVisitsMiddleware.php +++ b/module/Rest/src/Action/GetVisitsMiddleware.php @@ -10,6 +10,7 @@ use Shlinkio\Shlink\Core\Service\VisitsTracker; use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface; use Shlinkio\Shlink\Rest\Util\RestUtils; use Zend\Diactoros\Response\JsonResponse; +use Zend\I18n\Translator\TranslatorInterface; class GetVisitsMiddleware extends AbstractRestMiddleware { @@ -17,16 +18,22 @@ class GetVisitsMiddleware extends AbstractRestMiddleware * @var VisitsTrackerInterface */ private $visitsTracker; + /** + * @var TranslatorInterface + */ + private $translator; /** * GetVisitsMiddleware constructor. * @param VisitsTrackerInterface|VisitsTracker $visitsTracker + * @param TranslatorInterface $translator * - * @Inject({VisitsTracker::class}) + * @Inject({VisitsTracker::class, "translator"}) */ - public function __construct(VisitsTrackerInterface $visitsTracker) + public function __construct(VisitsTrackerInterface $visitsTracker, TranslatorInterface $translator) { $this->visitsTracker = $visitsTracker; + $this->translator = $translator; } /** @@ -52,12 +59,12 @@ class GetVisitsMiddleware extends AbstractRestMiddleware } catch (InvalidArgumentException $e) { return new JsonResponse([ 'error' => RestUtils::getRestErrorCodeFromException($e), - 'message' => sprintf('Provided short code "%s" is invalid', $shortCode), + 'message' => sprintf($this->translator->translate('Provided short code "%s" is invalid'), $shortCode), ], 400); } catch (\Exception $e) { return new JsonResponse([ 'error' => RestUtils::UNKNOWN_ERROR, - 'message' => 'Unexpected error occured', + 'message' => $this->translator->translate('Unexpected error occurred'), ], 500); } } diff --git a/module/Rest/src/Action/ListShortcodesMiddleware.php b/module/Rest/src/Action/ListShortcodesMiddleware.php index 4361ab89..99b1c718 100644 --- a/module/Rest/src/Action/ListShortcodesMiddleware.php +++ b/module/Rest/src/Action/ListShortcodesMiddleware.php @@ -9,6 +9,7 @@ use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; use Shlinkio\Shlink\Rest\Util\RestUtils; use Zend\Diactoros\Response\JsonResponse; +use Zend\I18n\Translator\TranslatorInterface; class ListShortcodesMiddleware extends AbstractRestMiddleware { @@ -18,16 +19,22 @@ class ListShortcodesMiddleware extends AbstractRestMiddleware * @var ShortUrlServiceInterface */ private $shortUrlService; + /** + * @var TranslatorInterface + */ + private $translator; /** * ListShortcodesMiddleware constructor. * @param ShortUrlServiceInterface|ShortUrlService $shortUrlService + * @param TranslatorInterface $translator * - * @Inject({ShortUrlService::class}) + * @Inject({ShortUrlService::class, "translator"}) */ - public function __construct(ShortUrlServiceInterface $shortUrlService) + public function __construct(ShortUrlServiceInterface $shortUrlService, TranslatorInterface $translator) { $this->shortUrlService = $shortUrlService; + $this->translator = $translator; } /** @@ -45,7 +52,7 @@ class ListShortcodesMiddleware extends AbstractRestMiddleware } catch (\Exception $e) { return new JsonResponse([ 'error' => RestUtils::UNKNOWN_ERROR, - 'message' => 'Unexpected error occured', + 'message' => $this->translator->translate('Unexpected error occurred'), ], 500); } } diff --git a/module/Rest/src/Action/ResolveUrlMiddleware.php b/module/Rest/src/Action/ResolveUrlMiddleware.php index 92a1e5c2..db7e5827 100644 --- a/module/Rest/src/Action/ResolveUrlMiddleware.php +++ b/module/Rest/src/Action/ResolveUrlMiddleware.php @@ -9,6 +9,7 @@ use Shlinkio\Shlink\Core\Service\UrlShortener; use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; use Shlinkio\Shlink\Rest\Util\RestUtils; use Zend\Diactoros\Response\JsonResponse; +use Zend\I18n\Translator\TranslatorInterface; class ResolveUrlMiddleware extends AbstractRestMiddleware { @@ -16,16 +17,22 @@ class ResolveUrlMiddleware extends AbstractRestMiddleware * @var UrlShortenerInterface */ private $urlShortener; + /** + * @var TranslatorInterface + */ + private $translator; /** * ResolveUrlMiddleware constructor. * @param UrlShortenerInterface|UrlShortener $urlShortener + * @param TranslatorInterface $translator * - * @Inject({UrlShortener::class}) + * @Inject({UrlShortener::class, "translator"}) */ - public function __construct(UrlShortenerInterface $urlShortener) + public function __construct(UrlShortenerInterface $urlShortener, TranslatorInterface $translator) { $this->urlShortener = $urlShortener; + $this->translator = $translator; } /** @@ -43,7 +50,7 @@ class ResolveUrlMiddleware extends AbstractRestMiddleware if (! isset($longUrl)) { return new JsonResponse([ 'error' => RestUtils::INVALID_ARGUMENT_ERROR, - 'message' => sprintf('No URL found for shortcode "%s"', $shortCode), + 'message' => sprintf($this->translator->translate('No URL found for shortcode "%s"'), $shortCode), ], 400); } @@ -53,12 +60,15 @@ class ResolveUrlMiddleware extends AbstractRestMiddleware } catch (InvalidShortCodeException $e) { return new JsonResponse([ 'error' => RestUtils::getRestErrorCodeFromException($e), - 'message' => sprintf('Provided short code "%s" has an invalid format', $shortCode), + 'message' => sprintf( + $this->translator->translate('Provided short code "%s" has an invalid format'), + $shortCode + ), ], 400); } catch (\Exception $e) { return new JsonResponse([ 'error' => RestUtils::UNKNOWN_ERROR, - 'message' => 'Unexpected error occured', + 'message' => $this->translator->translate('Unexpected error occurred'), ], 500); } } diff --git a/module/Rest/src/Middleware/CheckAuthenticationMiddleware.php b/module/Rest/src/Middleware/CheckAuthenticationMiddleware.php index 9120e2c6..f05c1b78 100644 --- a/module/Rest/src/Middleware/CheckAuthenticationMiddleware.php +++ b/module/Rest/src/Middleware/CheckAuthenticationMiddleware.php @@ -10,6 +10,7 @@ use Shlinkio\Shlink\Rest\Service\RestTokenServiceInterface; use Shlinkio\Shlink\Rest\Util\RestUtils; use Zend\Diactoros\Response\JsonResponse; use Zend\Expressive\Router\RouteResult; +use Zend\I18n\Translator\TranslatorInterface; use Zend\Stratigility\MiddlewareInterface; class CheckAuthenticationMiddleware implements MiddlewareInterface @@ -20,16 +21,22 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface * @var RestTokenServiceInterface */ private $restTokenService; + /** + * @var TranslatorInterface + */ + private $translator; /** * CheckAuthenticationMiddleware constructor. * @param RestTokenServiceInterface|RestTokenService $restTokenService + * @param TranslatorInterface $translator * - * @Inject({RestTokenService::class}) + * @Inject({RestTokenService::class, "translator"}) */ - public function __construct(RestTokenServiceInterface $restTokenService) + public function __construct(RestTokenServiceInterface $restTokenService, TranslatorInterface $translator) { $this->restTokenService = $restTokenService; + $this->translator = $translator; } /** @@ -93,8 +100,10 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface return new JsonResponse([ 'error' => RestUtils::INVALID_AUTH_TOKEN_ERROR, 'message' => sprintf( - 'Missing or invalid auth token provided. Perform a new authentication request and send provided token ' - . 'on every new request on the "%s" header', + $this->translator->translate( + 'Missing or invalid auth token provided. Perform a new authentication request and send provided ' + . 'token on every new request on the "%s" header' + ), self::AUTH_TOKEN_HEADER ), ], 401); diff --git a/module/Rest/src/Service/RestTokenService.php b/module/Rest/src/Service/RestTokenService.php index c452af2a..b9dd4a9d 100644 --- a/module/Rest/src/Service/RestTokenService.php +++ b/module/Rest/src/Service/RestTokenService.php @@ -21,8 +21,8 @@ class RestTokenService implements RestTokenServiceInterface /** * ShortUrlService constructor. * @param EntityManagerInterface $em - * * @param array $restConfig + * * @Inject({"em", "config.rest"}) */ public function __construct(EntityManagerInterface $em, array $restConfig) diff --git a/module/Rest/src/Util/RestUtils.php b/module/Rest/src/Util/RestUtils.php index 82082dcd..8e50a1cf 100644 --- a/module/Rest/src/Util/RestUtils.php +++ b/module/Rest/src/Util/RestUtils.php @@ -21,7 +21,7 @@ class RestUtils return self::INVALID_SHORTCODE_ERROR; case $e instanceof Core\InvalidUrlException: return self::INVALID_URL_ERROR; - case $e instanceof Core\InvalidArgumentException: + case $e instanceof Common\InvalidArgumentException: return self::INVALID_ARGUMENT_ERROR; case $e instanceof Rest\AuthenticationException: return self::INVALID_CREDENTIALS_ERROR;