修复翻译BUG

This commit is contained in:
2026-04-23 17:44:10 +08:00
parent f2b4dab54f
commit de3b9ab6bf
6 changed files with 109 additions and 11 deletions

View File

@@ -278,24 +278,27 @@ class Auth extends \ba\Auth
public function getMenus(int $uid = 0): array
{
$menus = parent::getMenus($uid ?: $this->id);
return $this->translateMenuRuleTitles($menus);
return $this->translateMenuRuleTitles($menus, $this->shouldTranslateMenuToEnglish());
}
/**
* 菜单标题统一按中文显示(不随语言切换)。
* 若 title 为英文动作名/英文菜单名,按中文映射表转换。
* 菜单标题按当前语言展示:
* - 英文环境:中文标题映射为英文
* - 其他环境:英文标题映射为中文
*
* @param array<int, array<string, mixed>> $menus
* @return array<int, array<string, mixed>>
*/
private function translateMenuRuleTitles(array $menus): array
private function translateMenuRuleTitles(array $menus, bool $toEnglish): array
{
foreach ($menus as $k => $item) {
if (isset($item['title']) && is_string($item['title']) && $item['title'] !== '') {
$menus[$k]['title'] = $this->menuTitleToZh($item['title']);
$menus[$k]['title'] = $toEnglish
? $this->menuTitleToEn($item['title'])
: $this->menuTitleToZh($item['title']);
}
if (!empty($item['children']) && is_array($item['children'])) {
$menus[$k]['children'] = $this->translateMenuRuleTitles($item['children']);
$menus[$k]['children'] = $this->translateMenuRuleTitles($item['children'], $toEnglish);
}
}
@@ -313,6 +316,24 @@ class Auth extends \ba\Auth
return isset($zhMap[$title]) && is_string($zhMap[$title]) ? $zhMap[$title] : $title;
}
private function menuTitleToEn(string $title): string
{
static $enMap = null;
if (!is_array($enMap)) {
$mapFile = app_path() . '/common/lang/en/admin_rule_title.php';
$loaded = is_file($mapFile) ? include $mapFile : [];
$enMap = is_array($loaded) ? $loaded : [];
}
return isset($enMap[$title]) && is_string($enMap[$title]) ? $enMap[$title] : $title;
}
private function shouldTranslateMenuToEnglish(): bool
{
$lang = function_exists('locale') ? locale() : '';
$normalized = is_string($lang) ? strtolower(str_replace('_', '-', trim($lang))) : '';
return str_starts_with($normalized, 'en');
}
public function isSuperAdmin(): bool
{
return in_array('*', $this->getRuleIds());