Просмотр исходного кода

feat(module): Get translation from oasx and Translate the wrong email into Chinese

ryland 1 год назад
Родитель
Сommit
dc2d689ebd
5 измененных файлов с 52 добавлено и 10 удалено
  1. 1 0
      .gitignore
  2. 3 3
      dev_tools/assets_test.py
  3. 14 1
      module/server/home_router.py
  4. 28 0
      module/server/i18n.py
  5. 6 6
      script.py

+ 1 - 0
.gitignore

@@ -26,6 +26,7 @@ test/
 !module/gui/FluentUI/fluentuiplugin.dll
 build-fluentui-Desktop_*/
 module/config/argument/setting.json
+module/config/i18n/*.json
 !fluentui/*
 *.exe
 !dev_tools/*

+ 3 - 3
dev_tools/assets_test.py

@@ -34,10 +34,10 @@ def detect_ocr(file: str, target: RuleOcr):
 
 
 # 图片文件路径 可以是相对路径
-IMAGE_FILE = r"D:\Project\OnmyojiAutoScript\log\error\1734235242044\2024-12-15_12-00-42-005384.png"
+IMAGE_FILE = r"C:\Users\Ryland\Desktop\Weixin Screenshot_20241225220524.png"
 if __name__ == '__main__':
-    from tasks.Exploration.assets import ExplorationAssets
-    targe = ExplorationAssets.I_BATTLE_REWARD
+    from tasks.KekkaiActivation.assets import KekkaiActivationAssets
+    targe = KekkaiActivationAssets.I_CARDS_KAIKO_3
     print(detect_image(IMAGE_FILE, targe))
 
     # ocr demo

+ 14 - 1
module/server/home_router.py

@@ -1,11 +1,15 @@
 # This Python file uses the following encoding: utf-8
 # @author runhey
 # github https://github.com/runhey
-from fastapi import APIRouter
+import json
+from fastapi import APIRouter, Body
+from pathlib import Path
 
+from module.config.utils import write_file
 from module.logger import logger
 from module.server.main_manager import MainManager
 from module.server.updater import Updater
+from module.server.i18n import I18n
 
 home_app = APIRouter(
     prefix="/home",
@@ -61,6 +65,7 @@ async def update_info():
         logger.error(e)
         return None
 
+
 @home_app.get('/execute_update')
 async def execute_update():
     # 下拉仓库 -> 关闭所有脚本进程 -> 最后重启oasx
@@ -71,3 +76,11 @@ async def execute_update():
         logger.error(e)
     return '手动更新将会立即结束运行中的脚本服务, 最后你还需重启oasx'
 
+
+@home_app.put('/chinese_translate')
+async def chinese_translate(data: dict = Body(...)):
+    try:
+        I18n.save_zh_cn(data)
+    except Exception as e:
+        logger.error(e)
+    return True

+ 28 - 0
module/server/i18n.py

@@ -0,0 +1,28 @@
+import json
+from pathlib import Path
+
+
+class I18n:
+    file_zh_cn = Path.cwd() / 'module' / 'config' / 'i18n' / 'zh-CN.json'
+
+    @classmethod
+    def trans_zh_cn(cls, text) -> str:
+        cn_zh_data = cls.load_zh_cn()
+        return cn_zh_data[text] if text in cn_zh_data else text
+
+    @classmethod
+    def save_zh_cn(cls, data) -> None:
+        I18n.file_zh_cn.parent.mkdir(parents=True, exist_ok=True)
+        with open(str(I18n.file_zh_cn), 'w', encoding='utf-8') as f:
+            s = json.dumps(data, indent=2, ensure_ascii=False, sort_keys=False, default=str)
+            f.write(s)
+
+    @classmethod
+    def load_zh_cn(cls) -> dict:
+        if not I18n.file_zh_cn.exists():
+            return {}
+        with open(str(I18n.file_zh_cn), 'r', encoding='utf-8') as f:
+            return json.load(f)
+
+if __name__ == '__main__':
+    print(I18n.load_zh_cn())

+ 6 - 6
script.py

@@ -31,6 +31,7 @@ from module.base.utils import load_module
 from module.base.decorator import del_cached_property
 from module.logger import logger
 from module.exception import *
+from module.server.i18n import I18n
 
 class Script:
     def __init__(self, config_name: str ='oas') -> None:
@@ -361,7 +362,7 @@ class Script:
             self.save_error_log()
             logger.warning(f'Game stuck, {self.device.package} will be restarted in 10 seconds')
             logger.warning('If you are playing by hand, please stop Alas')
-            self.config.notifier.push(title=command, content=f"<{self.config_name}> GameStuckError or GameTooManyClickError")
+            self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> GameStuckError or GameTooManyClickError")
             self.config.task_call('Restart')
             self.device.sleep(10)
             return False
@@ -378,23 +379,22 @@ class Script:
             # 这个还不重要 留着坑填
             logger.critical('Game page unknown')
             self.save_error_log()
-            self.config.notifier.push(title=command, content=f"<{self.config_name}> GamePageUnknownError")
-            exit(1)
+            self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> GamePageUnknownError")
             return False
         except ScriptError as e:
             logger.critical(e)
             logger.critical('This is likely to be a mistake of developers, but sometimes just random issues')
-            self.config.notifier.push(title=command, content=f"<{self.config_name}> ScriptError")
+            self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> ScriptError")
             exit(1)
         except RequestHumanTakeover as e:
             logger.critical(e)
             logger.critical('Request human takeover')
-            self.config.notifier.push(title=command, content=f"<{self.config_name}> RequestHumanTakeover")
+            self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> RequestHumanTakeover")
             exit(1)
         except Exception as e:
             logger.exception(e)
             self.save_error_log()
-            self.config.notifier.push(title=command, content=f"<{self.config_name}> Exception occured")
+            self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> Exception occured")
             exit(1)
 
     def loop(self):