Translation of Exceptions

about a month ago

tomcat4x
Member | 22
+
0
-

Hi,

has anyone a solution for translate the thrown exeptions from Nette without changing the main classes.

For examle the SimpleAuthenticator Class is throwing a

AuthenticationException(‘Invalid password.’, self::INVALID_CREDENTIAL);

or

AuthenticationException(“User ‘$username’ not found.”, self::IDENTITY_NOT_FOUND);

I use Kdyby/Translation, but i find no solution für translate this Exception text to german with this extension.

regards Frank

about a month ago

jiri.pudil
Member | 786
+
+5
-

Hello,

it's not a good practice to display exception messages to users; exceptions are part of a method's application interface, not user interface. You should catch them and display your own message based on the exception's code. Translating it will be easy this way.

about a month ago

matopeto
Member | 311
+
+4
-

Hmm.. Something like that? (writen by hand, bud i think idea is clear)

try {
 // authentication
 // $userName, $password...
} catch (AuthenticationException $ex) {
 if ($ex->getCode() === IAuthenticator::IDENTITY_NOT_FOUND) {
    $messaget = $translator->translate("user.not.found", $userName);
 }
}