Added translations for error messages returned by the REST API

This commit is contained in:
Alejandro Celaya 2016-07-21 16:41:16 +02:00
parent 06868f782b
commit e42469b090
10 changed files with 141 additions and 28 deletions

BIN
module/Rest/lang/es.mo Normal file

Binary file not shown.

60
module/Rest/lang/es.po Normal file
View File

@ -0,0 +1,60 @@
msgid ""
msgstr ""
"Project-Id-Version: Shlink 1.0\n"
"POT-Creation-Date: 2016-07-21 16:39+0200\n"
"PO-Revision-Date: 2016-07-21 16:40+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: es_ES\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.8.7.1\n"
"X-Poedit-Basepath: ..\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-KeywordsList: translate;translatePlural\n"
"X-Poedit-SearchPath-0: config\n"
"X-Poedit-SearchPath-1: src\n"
msgid "You have to provide both \"username\" and \"password\""
msgstr "Debes proporcionar tanto \"username\" como \"password\""
msgid "Invalid username and/or password"
msgstr "Usuario y/o contraseña no válidos"
msgid "A URL was not provided"
msgstr "No se ha proporcionado una URL"
#, php-format
msgid "Provided URL \"%s\" is invalid. Try with a different one."
msgstr ""
"La URL \"%s\" proporcionada es inválida. Inténtalo de nuevo con una "
"diferente."
msgid "Unexpected error occurred"
msgstr "Ocurrió un error inesperado"
#, php-format
msgid "Provided short code \"%s\" is invalid"
msgstr "El código corto \"%s\" proporcionado es inválido"
#, php-format
msgid "No URL found for shortcode \"%s\""
msgstr "No se ha encontrado una URL para el código corto \"%s\""
#, php-format
msgid "Provided short code \"%s\" has an invalid format"
msgstr "El código corto proporcionado \"%s\" tiene un formato no inválido"
#, php-format
msgid ""
"Missing or invalid auth token provided. Perform a new authentication request "
"and send provided token on every new request on the \"%s\" header"
msgstr ""
"No se ha proporcionado token de autenticación o este es inválido. Realia una "
"nueva petición de autenticación y envía el token proporcionado en cada nueva "
"petición en la cabecera \"%s\""
#~ msgid "RestToken not found for token \"%s\""
#~ msgstr "No se ha encontrado un RestToken para el token \"%s\""

View File

@ -9,6 +9,7 @@ use Shlinkio\Shlink\Rest\Service\RestTokenService;
use Shlinkio\Shlink\Rest\Service\RestTokenServiceInterface;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse;
use Zend\I18n\Translator\TranslatorInterface;
class AuthenticateMiddleware extends AbstractRestMiddleware
{
@ -16,16 +17,22 @@ class AuthenticateMiddleware extends AbstractRestMiddleware
* @var RestTokenServiceInterface
*/
private $restTokenService;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* AuthenticateMiddleware 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;
}
/**
@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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)

View File

@ -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;