优化切换语言时菜单不翻译的问题

This commit is contained in:
2026-04-20 10:33:57 +08:00
parent 92fb40ae80
commit cf9373e5c2

View File

@@ -278,25 +278,29 @@ class Auth extends \ba\Auth
public function getMenus(int $uid = 0): array
{
$menus = parent::getMenus($uid ?: $this->id);
// 库内 title 为中文;仅英文界面走 __()。若对 zh-cn 也 __()Symfony 在找不到键时会 fallback 到 en
// 命中 admin_rule_title 后会把中文标题误译成英文。
$localeNorm = str_replace('_', '-', strtolower(locale()));
$toEnglish = ($localeNorm === 'en');
return $this->translateMenuRuleTitles($menus);
return $this->translateMenuRuleTitles($menus, $toEnglish);
}
/**
* 将 admin_rule.title(库内中文等)按当前请求语言翻译,供侧边栏/标签与 meta.title 一致
* 英文词条见 app/common/lang/en/admin_rule_title.php
* 将 admin_rule.title 在英文界面译为英文;中文界面保持库内原文
* 英文映射见 app/common/lang/en/admin_rule_title.php
*
* @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'] = __($item['title']);
$menus[$k]['title'] = $toEnglish ? __($item['title']) : $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);
}
}