Files
lotteryLaravel/app/Support/AdminMessage.php

57 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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} 管理端段81108114
*/
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);
}
}