Files
lotteryLaravel/app/Support/LotteryMessage.php

38 lines
1.2 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
namespace App\Support;
use App\Lottery\ErrorCode;
use Illuminate\Http\Request;
/**
* 【业务文案翻译辅助类】
*
* 从 lang/{locale}/sso.php 等语言包取字符串,供 JSON 里「msg」字段使用。
* `App\Lottery\ErrorCode` 中玩家 SSO 段80018004的各语言 `msg`。
*
* 依赖 NegotiateLotteryLocale 已写入 lottery_locale若未写则使用 fallback 语言包。
*/
final class LotteryMessage
{
/**
* 取 SSO 鉴权类错误的用户可见文案(与 ApiResponse 的 msg 对应)。
*
* @param int $code {@see ErrorCode} 玩家 SSO 段80018004
*/
public static function sso(Request $request, int $code): string
{
$fallback = (string) config('lottery.locales.fallback', 'en');
$locale = (string) ($request->attributes->get('lottery_locale') ?? LotteryLocale::resolve($request));
$key = 'sso.'.$code; // 对应 lang/{locale}/sso.php 内键名,如 '8001'
$msg = trans($key, [], $locale);
// Laravel 无键时返回整条 key 字符串,此时改用 fallback 语言再试一次
if ($msg !== $key) {
return $msg;
}
return trans($key, [], $fallback);
}
}