Skip to content

Commit fb6405f

Browse files
authored
Merge pull request #2852 from nAa6666/add-translations
Add translations support for exception messages
2 parents f241ca7 + 92d0287 commit fb6405f

9 files changed

+43
-18
lines changed

src/Exceptions/GuardDoesNotMatch.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class GuardDoesNotMatch extends InvalidArgumentException
99
{
1010
public static function create(string $givenGuard, Collection $expectedGuards)
1111
{
12-
return new static("The given role or permission should use guard `{$expectedGuards->implode(', ')}` instead of `{$givenGuard}`.");
12+
return new static(__('The given role or permission should use guard `:expected` instead of `:given`.', [
13+
'expected' => $expectedGuards->implode(', '),
14+
'given' => $givenGuard,
15+
]));
1316
}
1417
}

src/Exceptions/PermissionAlreadyExists.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class PermissionAlreadyExists extends InvalidArgumentException
88
{
99
public static function create(string $permissionName, string $guardName)
1010
{
11-
return new static("A `{$permissionName}` permission already exists for guard `{$guardName}`.");
11+
return new static(__('A `:permission` permission already exists for guard `:guard`.', [
12+
'permission' => $permissionName,
13+
'guard' => $guardName,
14+
]));
1215
}
1316
}

src/Exceptions/PermissionDoesNotExist.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ class PermissionDoesNotExist extends InvalidArgumentException
88
{
99
public static function create(string $permissionName, ?string $guardName)
1010
{
11-
return new static("There is no permission named `{$permissionName}` for guard `{$guardName}`.");
11+
return new static(__('There is no permission named `:permission` for guard `:guard`.', [
12+
'permission' => $permissionName,
13+
'guard' => $guardName,
14+
]));
1215
}
1316

1417
/**
@@ -17,6 +20,9 @@ public static function create(string $permissionName, ?string $guardName)
1720
*/
1821
public static function withId($permissionId, ?string $guardName)
1922
{
20-
return new static("There is no [permission] with ID `{$permissionId}` for guard `{$guardName}`.");
23+
return new static(__('There is no [permission] with ID `:id` for guard `:guard`.', [
24+
'id' => $permissionId,
25+
'guard' => $guardName,
26+
]));
2127
}
2228
}

src/Exceptions/RoleAlreadyExists.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class RoleAlreadyExists extends InvalidArgumentException
88
{
99
public static function create(string $roleName, string $guardName)
1010
{
11-
return new static("A role `{$roleName}` already exists for guard `{$guardName}`.");
11+
return new static(__('A role `:role` already exists for guard `:guard`.', [
12+
'role' => $roleName,
13+
'guard' => $guardName,
14+
]));
1215
}
1316
}

src/Exceptions/RoleDoesNotExist.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ class RoleDoesNotExist extends InvalidArgumentException
88
{
99
public static function named(string $roleName, ?string $guardName)
1010
{
11-
return new static("There is no role named `{$roleName}` for guard `{$guardName}`.");
11+
return new static(__('There is no role named `:role` for guard `:guard`.', [
12+
'role' => $roleName,
13+
'guard' => $guardName,
14+
]));
1215
}
1316

1417
/**
@@ -17,6 +20,9 @@ public static function named(string $roleName, ?string $guardName)
1720
*/
1821
public static function withId($roleId, ?string $guardName)
1922
{
20-
return new static("There is no role with ID `{$roleId}` for guard `{$guardName}`.");
23+
return new static(__('There is no role with ID `:id` for guard `:guard`.', [
24+
'id' => $roleId,
25+
'guard' => $guardName,
26+
]));
2127
}
2228
}

src/Exceptions/UnauthorizedException.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class UnauthorizedException extends HttpException
1313

1414
public static function forRoles(array $roles): self
1515
{
16-
$message = 'User does not have the right roles.';
16+
$message = __('User does not have the right roles.');
1717

1818
if (config('permission.display_role_in_exception')) {
19-
$message .= ' Necessary roles are '.implode(', ', $roles);
19+
$message .= ' ' . __('Necessary roles are :roles', ['roles' => implode(', ', $roles)]);
2020
}
2121

2222
$exception = new static(403, $message, null, []);
@@ -27,10 +27,10 @@ public static function forRoles(array $roles): self
2727

2828
public static function forPermissions(array $permissions): self
2929
{
30-
$message = 'User does not have the right permissions.';
30+
$message = __('User does not have the right permissions.');
3131

3232
if (config('permission.display_permission_in_exception')) {
33-
$message .= ' Necessary permissions are '.implode(', ', $permissions);
33+
$message .= ' ' . __('Necessary permissions are :permissions', ['permissions' => implode(', ', $permissions)]);
3434
}
3535

3636
$exception = new static(403, $message, null, []);
@@ -41,10 +41,10 @@ public static function forPermissions(array $permissions): self
4141

4242
public static function forRolesOrPermissions(array $rolesOrPermissions): self
4343
{
44-
$message = 'User does not have any of the necessary access rights.';
44+
$message = __('User does not have any of the necessary access rights.');
4545

4646
if (config('permission.display_permission_in_exception') && config('permission.display_role_in_exception')) {
47-
$message .= ' Necessary roles or permissions are '.implode(', ', $rolesOrPermissions);
47+
$message .= ' ' . __('Necessary roles or permissions are :values', ['values' => implode(', ', $rolesOrPermissions)]);
4848
}
4949

5050
$exception = new static(403, $message, null, []);
@@ -57,12 +57,14 @@ public static function missingTraitHasRoles(Authorizable $user): self
5757
{
5858
$class = get_class($user);
5959

60-
return new static(403, "Authorizable class `{$class}` must use Spatie\Permission\Traits\HasRoles trait.", null, []);
60+
return new static(403, __('Authorizable class `:class` must use Spatie\\Permission\\Traits\\HasRoles trait.', [
61+
'class' => $class,
62+
]), null, []);
6163
}
6264

6365
public static function notLoggedIn(): self
6466
{
65-
return new static(403, 'User is not logged in.', null, []);
67+
return new static(403, __('User is not logged in.'), null, []);
6668
}
6769

6870
public function getRequiredRoles(): array

src/Exceptions/WildcardPermissionInvalidArgument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class WildcardPermissionInvalidArgument extends InvalidArgumentException
88
{
99
public static function create()
1010
{
11-
return new static('Wildcard permission must be string, permission id or permission instance');
11+
return new static(__('Wildcard permission must be string, permission id or permission instance'));
1212
}
1313
}

src/Exceptions/WildcardPermissionNotImplementsContract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class WildcardPermissionNotImplementsContract extends InvalidArgumentException
88
{
99
public static function create()
1010
{
11-
return new static('Wildcard permission class must implements Spatie\Permission\Contracts\Wildcard contract');
11+
return new static(__('Wildcard permission class must implement Spatie\\Permission\\Contracts\\Wildcard contract'));
1212
}
1313
}

src/Exceptions/WildcardPermissionNotProperlyFormatted.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class WildcardPermissionNotProperlyFormatted extends InvalidArgumentException
88
{
99
public static function create(string $permission)
1010
{
11-
return new static("Wildcard permission `{$permission}` is not properly formatted.");
11+
return new static(__('Wildcard permission `:permission` is not properly formatted.', [
12+
'permission' => $permission,
13+
]));
1214
}
1315
}

0 commit comments

Comments
 (0)