|
|
@@ -1,14 +1,14 @@
|
|
|
import requests
|
|
|
import json
|
|
|
from homepagebuilder.interfaces import script, Logger
|
|
|
+from homepagebuilder.core.config import config
|
|
|
+from typing import TYPE_CHECKING
|
|
|
|
|
|
+if TYPE_CHECKING:
|
|
|
+ from homepagebuilder.core.types import Context
|
|
|
+
|
|
|
logger = Logger("TranslationScript")
|
|
|
-
|
|
|
-response = requests.get('https://raw.githubusercontent.com/Light-Beacon/Minecraft-ZH-Translation-Sheet/refs/heads/main/data/translations.json',
|
|
|
- timeout=10)
|
|
|
-response_content = response.content
|
|
|
-translations = json.loads(response_content)
|
|
|
-logger.info("加载中文翻译数据成功, 共计 %d 条翻译", len(translations))
|
|
|
+TRANSLATIONS = {}
|
|
|
|
|
|
@script('name')
|
|
|
def translate(*args, **_):
|
|
|
@@ -19,4 +19,26 @@ def translate(*args, **_):
|
|
|
fallback = args[1]
|
|
|
else:
|
|
|
fallback = name
|
|
|
- return translations.get(name.lower(), fallback)
|
|
|
+ return TRANSLATIONS.get(name.lower(), fallback)
|
|
|
+
|
|
|
+def fetch_transltions():
|
|
|
+ logger.info("正在加载中文翻译")
|
|
|
+ try:
|
|
|
+ response = requests.get('https://raw.githubusercontent.com/Light-Beacon/Minecraft-ZH-Translation-Sheet/refs/heads/main/data/translations.json',
|
|
|
+ timeout=10)
|
|
|
+ except ConnectionError:
|
|
|
+ logger.warning("无法加载中文翻译")
|
|
|
+ return {}
|
|
|
+ response_content = response.content
|
|
|
+ return json.loads(response_content)
|
|
|
+
|
|
|
+def init(context:'Context', *args, **kwargs):
|
|
|
+ global TRANSLATIONS
|
|
|
+ if config('NewsHomepage.Translation.UseReloadCache', config('NewsHomepage.Debug')):
|
|
|
+ if translations_manifset_cache := context.builder.get_data('translation_manifset_cache'):
|
|
|
+ TRANSLATIONS = translations_manifset_cache
|
|
|
+ logger.info("从缓存中加载中文翻译数据成功, 共计 %d 条翻译", len(TRANSLATIONS))
|
|
|
+ return
|
|
|
+ TRANSLATIONS = fetch_transltions()
|
|
|
+ logger.info("加载中文翻译数据成功, 共计 %d 条翻译", len(TRANSLATIONS))
|
|
|
+ context.builder.set_data('translation_manifset_cache', TRANSLATIONS)
|