Files
web-Iot/.workbuddy/update_router.py
2026-08-12 11:29:44 +08:00

96 lines
3.7 KiB
Python

import re
with open('src/router/index.tsx', 'r', encoding='utf-8') as f:
content = f.read()
# Replace routeMeta titles with i18n keys
replacements = {
"title: '\u603b\u89c8\u9996\u9875'": "title: 'router.overview'",
"title: '\u8bbe\u5907\u7ba1\u7406'": "title: 'router.devices'",
"title: '\u5728\u7ebf\u89c6\u9891'": "title: 'router.video'",
"title: '\u5b9e\u65f6\u76d1\u63a7'": "title: 'router.monitor'",
"title: '\u544a\u8b66\u4e2d\u5fc3'": "title: 'router.alerts'",
"title: 'AI\u8bca\u65ad\u5206\u6790'": "title: 'router.aiAnalysis'",
"title: '\u6e05\u6d17\u4f18\u5316'": "title: 'router.clean'",
"title: '\u5de5\u5355\u4efb\u52a1'": "title: 'router.task'",
"title: '\u62a5\u8868\u4e2d\u5fc3'": "title: 'router.table'",
"title: '\u7cfb\u7edf\u8bbe\u7f6e'": "title: 'router.systemSetting'",
"title: '\u65e5\u5fd7\u7ba1\u7406'": "title: 'router.log'",
"title: '\u89c6\u9891\u7ba1\u7406'": "title: 'router.videoManage'",
"title: '\u89c6\u9891\u4e0b\u8f7d'": "title: 'router.downloads'",
"title: '\u7528\u6237\u7ba1\u7406'": "title: 'router.users'",
"title: '\u89d2\u8272\u7ba1\u7406'": "title: 'router.roles'",
"title: '\u83dc\u5355\u7ba1\u7406'": "title: 'router.menuList'",
"title: '\u7ec4\u7ec7\u7ba1\u7406'": "title: 'router.organization'",
"title: '\u573a\u7ad9\u7ba1\u7406'": "title: 'router.station'",
"title: '\u4e2a\u4eba\u4e2d\u5fc3'": "title: 'router.profile'",
}
for old, new in replacements.items():
content = content.replace(old, new)
# Replace 404 page text
content = content.replace(
'<p className="text-lg font-medium">\u6a21\u5757\u5f00\u53d1\u4e2d</p>',
'<p className="text-lg font-medium">{t("router.moduleDeveloping")}</p>'
)
# Replace placeholder pages
content = content.replace(
'<h2>\u901a\u77e5\u7b56\u7565\u9875\u9762</h2>',
'<h2>{t("router.noticePolicy")}</h2>'
)
content = content.replace(
'<h2>\u6570\u636e\u4e0e\u5b89\u5168\u9875\u9762</h2>',
'<h2>{t("router.dataSafety")}</h2>'
)
# Add useTranslation import and helper components
add_after_imports = """
import { useTranslation } from 'react-i18next';
function ModuleDeveloping() {
\tconst { t } = useTranslation();
\treturn (
\t\t<div className="flex flex-col items-center justify-center h-full gap-4 text-gray-400 select-none">
\t\t\t<span className="text-6xl opacity-20">\ud83c\udde7</span>
\t\t\t<p className="text-lg font-medium">{t('router.moduleDeveloping')}</p>
\t\t</div>
\t);
}
function NoticePolicyPage() {
\tconst { t } = useTranslation();
\treturn <div style={{ padding: 20 }}><h2>{t('router.noticePolicy')}</h2></div>;
}
function DataSafetyPage() {
\tconst { t } = useTranslation();
\treturn <div style={{ padding: 20 }}><h2>{t('router.dataSafety')}</h2></div>;
}
"""
# Insert after the Profile import line
content = re.sub(
r"(import Profile from '.*?';\n)",
r"\1" + add_after_imports,
content,
count=1
)
# Replace the inline 404 element with component
old_404 = '<div className="flex flex-col items-center justify-center h-full gap-4 text-gray-400 select-none">\n\t\t\t\t\t\t\t<span className="text-6xl opacity-20">\ud83c\udde7</span>\n\t\t\t\t\t\t\t<p className="text-lg font-medium">{t("router.moduleDeveloping")}</p>\n\t\t\t\t\t\t</div>'
content = content.replace(old_404, '<ModuleDeveloping />')
# Replace the inline notice/dataSafety elements
old_notice = '<div style={{ padding: 20 }}><h2>{t("router.noticePolicy")}</h2></div>'
content = content.replace(old_notice, '<NoticePolicyPage />')
old_datasafety = '<div style={{ padding: 20 }}><h2>{t("router.dataSafety")}</h2></div>'
content = content.replace(old_datasafety, '<DataSafetyPage />')
with open('src/router/index.tsx', 'w', encoding='utf-8') as f:
f.write(content)
print("router/index.tsx updated successfully")