57 lines
1.6 KiB
PHP
57 lines
1.6 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Support;
|
||
|
||
use App\Lottery\ErrorCode;
|
||
use Illuminate\Http\Request;
|
||
|
||
/**
|
||
* 【管理后台业务文案翻译辅助类】
|
||
*
|
||
* 从 lang/{locale}/admin.php 等语言包取字符串,供 JSON 里「msg」字段使用。
|
||
* 与 LotteryMessage 对应,专门处理管理端错误消息。
|
||
*/
|
||
final class AdminMessage
|
||
{
|
||
/**
|
||
* 取通用管理后台错误的用户可见文案(lang/{locale}/admin.php)。
|
||
*
|
||
* @param string $key 语言包键名,如 'unauthenticated', 'permission_denied'
|
||
*/
|
||
public static function get(Request $request, string $key): string
|
||
{
|
||
$fallback = (string) config('lottery.locales.fallback', 'en');
|
||
$locale = (string) ($request->attributes->get('lottery_locale') ?? LotteryLocale::resolve($request));
|
||
|
||
$fullKey = 'admin.'.$key;
|
||
$msg = trans($fullKey, [], $locale);
|
||
|
||
if ($msg !== $fullKey) {
|
||
return $msg;
|
||
}
|
||
|
||
return trans($fullKey, [], $fallback);
|
||
}
|
||
|
||
/**
|
||
* 取错误码对应的用户可见文案。
|
||
*
|
||
* @param int $code {@see ErrorCode} 管理端段(8110–8114)
|
||
*/
|
||
public static function errorCode(Request $request, int $code): string
|
||
{
|
||
$fallback = (string) config('lottery.locales.fallback', 'en');
|
||
$locale = (string) ($request->attributes->get('lottery_locale') ?? LotteryLocale::resolve($request));
|
||
$key = 'admin.'.$code;
|
||
|
||
$msg = trans($key, [], $locale);
|
||
if ($msg !== $key) {
|
||
return $msg;
|
||
}
|
||
|
||
return trans($key, [], $fallback);
|
||
}
|
||
}
|