优化切换语言时菜单不翻译的问题
This commit is contained in:
@@ -278,25 +278,29 @@ class Auth extends \ba\Auth
|
|||||||
public function getMenus(int $uid = 0): array
|
public function getMenus(int $uid = 0): array
|
||||||
{
|
{
|
||||||
$menus = parent::getMenus($uid ?: $this->id);
|
$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 一致。
|
* 将 admin_rule.title 在英文界面译为英文;中文界面保持库内原文。
|
||||||
* 英文词条见 app/common/lang/en/admin_rule_title.php
|
* 英文映射见 app/common/lang/en/admin_rule_title.php
|
||||||
*
|
*
|
||||||
* @param array<int, array<string, mixed>> $menus
|
* @param array<int, array<string, mixed>> $menus
|
||||||
* @return array<int, array<string, mixed>>
|
* @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) {
|
foreach ($menus as $k => $item) {
|
||||||
if (isset($item['title']) && is_string($item['title']) && $item['title'] !== '') {
|
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'])) {
|
if (!empty($item['children']) && is_array($item['children'])) {
|
||||||
$menus[$k]['children'] = $this->translateMenuRuleTitles($item['children']);
|
$menus[$k]['children'] = $this->translateMenuRuleTitles($item['children'], $toEnglish);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user