Passport: [Request feature] Localization

Created on 11 Feb 2017  ·  15Comments  ·  Source: laravel/passport

Most helpful comment

My quick workaround is

// resources/lang/en/oauth.php
return [
    'invalid_credentials' => [ // key from error type
        'error'   => 'Invalid credentials',
        'message' => 'The user credentials were incorrect.'
    ]
];

// app/Exceptions/Handler.php
/**
 * Report or log an exception.
 *
 * @param  \Exception  $exception
 * @return void
 */
public function report(Exception $exception)
{
    if ($exception instanceof \League\OAuth2\Server\Exception\OAuthServerException) {
        $transPayload = trans('oauth.' . $exception->getErrorType());

        if (is_array($transPayload)) { // can be remove if you translate all error types!
            $exception->setPayload($transPayload);
        }
    }

    parent::report($exception);
}

All 15 comments

All views are publishable and available for editing; passport has localization support. I didn't have any trouble using this in a multi-locale service.

Could you elaborate which part's are preventing you from doing what you want?

actually in json response messages of errors

@alphaelf Were you able to find a fix for this?

This new feature will be very useful for non-english speaking country developers.

Actually, it doesn't have localization support. As mentioned above, in API JSON responses theres no way to customize the messages. Only if we do this.

I think this is a feature regarding thephpleague\oauth2-server package, because the strings for the exception messages are contained in \vendor\league\oauth2-server\src\Exception\OAuthServerException.php file. Maybe publishing this file from the vendor folder to the app\Exceptions folder, or creating a config/oauth2.php like config file containing the strings can solve our problem.

We have an issue raised for this already in the oauth2-server. No one has picked it up yet but it is on the _todo_ list

@Sephster you seem to be in the thephpleague people, can't you accept pull requests? If you can, I can try to contribute.

hi @tpaksu - yep, I'm one of the maintainers for the package. Always happy to accept additional help!

Okay, I'll look when I get some spare time :) Thanks. :+1:

@Sephster I understood that oauth2-server package does not use any of the laravel components such as localization because of being available to any system that supports PSR-4 features. We need to have some overridable system that modifies the strings. For example:

in src/Lang/Messages.php

<?php 

namespace League\Oauth2\Server\Lang;

static class Messages {
     private static $translations = [
          "grant.not_supported.message" => "The authorization grant type is not supported by the authorization server.",
          "grant.not_supported.hint" => "Check the `grant_type` parameter"
     ];

     public static function translate($key){
           if(isset(self::$translations[$key])) return self::$translations[$key];
           return $key;
     }
}

in src/Exception/OAuthServerException.php

<?php 

..
use League\Oauth2\Server\Lang\Messages as Message;

..

public static function unsupportedGrantType()
{
    $errorMessage = Message::translate("grant.not_supported.message");
    $hint = Message::translate("grant.not_supported.hint");
    return new static($errorMessage, 2, 'unsupported_grant_type', 400, $hint);
}

And in Laravel/Passport side, create a overrider class to override \League\Oauth2\Server\Lang\Messages class.

But how can we do it?

One way which comes in my mind is making the League\Oauth2\Server\Lang\Messages::$translations member public and setting it with new values at initialization.

I'm sorry @tpaksu but I haven't had time to give it much thought at the moment due to a lack of available time. I haven't looked into the different approaches that can be used but what you are proposing doesn't seem an unreasonable approach.

I'm sorry but I have little time to fully research this at the moment but I will try and assist you when I have more time. All the best with it

@Sephster no problem, I'll look further into it. If I can find a more reliable and structured way, I'll continue to share.

Looks like this first needs to be solved in oauth2-server before we can solve it in Passport.

My quick workaround is

// resources/lang/en/oauth.php
return [
    'invalid_credentials' => [ // key from error type
        'error'   => 'Invalid credentials',
        'message' => 'The user credentials were incorrect.'
    ]
];

// app/Exceptions/Handler.php
/**
 * Report or log an exception.
 *
 * @param  \Exception  $exception
 * @return void
 */
public function report(Exception $exception)
{
    if ($exception instanceof \League\OAuth2\Server\Exception\OAuthServerException) {
        $transPayload = trans('oauth.' . $exception->getErrorType());

        if (is_array($transPayload)) { // can be remove if you translate all error types!
            $exception->setPayload($transPayload);
        }
    }

    parent::report($exception);
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

gbgelado picture gbgelado  ·  3Comments

duccanh0022 picture duccanh0022  ·  3Comments

mehrancodes picture mehrancodes  ·  3Comments

rudolfdobias picture rudolfdobias  ·  3Comments

parth-vora-7 picture parth-vora-7  ·  4Comments