diff --git a/liteyuki/liteyuki_main/__init__.py b/liteyuki/liteyuki_main/__init__.py index a190a13..a76d106 100644 --- a/liteyuki/liteyuki_main/__init__.py +++ b/liteyuki/liteyuki_main/__init__.py @@ -28,6 +28,6 @@ $$ |/ $$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |/ $$ | $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/ """ + "\033[0m") -sys_lang = get_default_lang() +sys_lang = Language(get_default_lang_code()) nonebot.logger.info(sys_lang.get("main.current_language", LANG=sys_lang.get("language.name"))) nonebot.logger.info(sys_lang.get("main.enable_webdash", URL=f"http://127.0.0.1:{config.get('port', 20216)}")) diff --git a/liteyuki/liteyuki_main/runtime.py b/liteyuki/liteyuki_main/runtime.py index 1ba3051..16fe1d1 100644 --- a/liteyuki/liteyuki_main/runtime.py +++ b/liteyuki/liteyuki_main/runtime.py @@ -8,7 +8,7 @@ from nonebot.adapters.onebot.v11 import MessageSegment from nonebot.permission import SUPERUSER from liteyuki.utils import __NAME__, __VERSION__, load_from_yaml from liteyuki.utils.message.html_tool import template2image -from liteyuki.utils.base.language import Language, get_default_lang, get_user_lang +from liteyuki.utils.base.language import Language, get_default_lang_code, get_user_lang from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent from liteyuki.utils.base.resource import get_path from liteyuki.utils.message.tools import convert_size @@ -100,7 +100,7 @@ async def get_stats_data(self_id: str = None, lang: str = None) -> dict: if self_id is None: self_id = list(nonebot.get_bots().keys())[0] if len(nonebot.get_bots()) > 0 else "liteyuki" if lang is None: - ulang = get_default_lang() + ulang = Language(get_default_lang_code()) else: ulang = Language(lang) diff --git a/liteyuki/plugins/liteyuki_weather/qw_api.py b/liteyuki/plugins/liteyuki_weather/qw_api.py index 7147af8..1bc3383 100644 --- a/liteyuki/plugins/liteyuki_weather/qw_api.py +++ b/liteyuki/plugins/liteyuki_weather/qw_api.py @@ -1,33 +1,22 @@ from .qw_models import * import httpx -language_map = { - "zh-CN" : "zh", - "zh-HK" : "zh-hant", - "en-US" : "en", - "ja-JP" : "ja", - "ko-KR" : "ko", - "fr-FR" : "fr", - "es-ES" : "es", - "de-DE" : "de", - "it-IT" : "it", - "ru-RU" : "ru", - "ar-SA" : "ar", - "pt-BR" : "pt", - "nl-NL" : "nl", - "pl-PL" : "pl", - "tr-TR" : "tr", - "th-TH" : "th", - "vi-VN" : "vi", - "id-ID" : "id", - "ms-MY" : "ms", - "fil-PH": "fil", -} # 其他使用默认对应 dev_url = "https://devapi.qweather.com/" # 开发HBa com_url = "https://api.qweather.com/" # 正式环境 +def get_qw_lang(lang: str) -> str: + if lang in ["zh-HK", "zh-TW"]: + return "zh-hant" + elif lang.startswith("zh"): + return "zh" + elif lang.startswith("en"): + return "en" + else: + return lang + + async def city_lookup( location: str, key: str, @@ -53,7 +42,7 @@ async def city_lookup( "adm" : adm, "number" : number, "key" : key, - "lang" : language_map.get(lang, lang), + "lang" : lang, } async with httpx.AsyncClient() as client: resp = await client.get(url, params=params) @@ -72,7 +61,7 @@ async def get_weather_now( params = { "location": location, "key" : key, - "lang" : language_map.get(lang, lang), + "lang" : lang, "unit" : unit, } async with httpx.AsyncClient() as client: @@ -92,7 +81,7 @@ async def get_weather_daily( params = { "location": location, "key" : key, - "lang" : language_map.get(lang, lang), + "lang" : lang, "unit" : unit, } async with httpx.AsyncClient() as client: @@ -112,7 +101,7 @@ async def get_weather_hourly( params = { "location": location, "key" : key, - "lang" : language_map.get(lang, lang), + "lang" : lang, "unit" : unit, } async with httpx.AsyncClient() as client: @@ -132,11 +121,10 @@ async def get_airquality( url = dev_url + url_path if dev else com_url + url_path params = { "key" : key, - "lang" : language_map.get(lang, lang), + "lang" : lang, "pollutant": pollutant, "station" : station, } async with httpx.AsyncClient() as client: resp = await client.get(url, params=params) return resp.json() - diff --git a/liteyuki/plugins/liteyuki_weather/qweather.py b/liteyuki/plugins/liteyuki_weather/qweather.py index e401bb4..4afffb3 100644 --- a/liteyuki/plugins/liteyuki_weather/qweather.py +++ b/liteyuki/plugins/liteyuki_weather/qweather.py @@ -25,9 +25,10 @@ from nonebot_plugin_alconna import on_alconna, Alconna, Args, MultiVar, Arparma async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher): """await alconna.send("weather", city)""" ulang = get_user_lang(str(event.user_id)) - qw_lang = language_map.get(ulang.lang_code, ulang.lang_code) + qw_lang = get_qw_lang(ulang.lang_code) key = get_config("weather_key") is_dev = get_config("weather_dev") + user: User = user_db.first(User(), "user_id = ?", str(event.user_id), default=User()) # params @@ -38,7 +39,6 @@ async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher): await matcher.finish(ulang.get("weather.no_key")) kws = result.main_args.get("keywords") - if kws: if len(kws) >= 2: adm = kws[0] @@ -53,7 +53,6 @@ async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher): await matcher.finish(ulang.get("liteyuki.invalid_command", TEXT="location")) city_info = await city_lookup(stored_location, key, lang=qw_lang) city_name = stored_location - if city_info.code == "200": location_data = city_info.location[0] else: diff --git a/liteyuki/utils/base/language.py b/liteyuki/utils/base/language.py index a2067fa..40bc83d 100644 --- a/liteyuki/utils/base/language.py +++ b/liteyuki/utils/base/language.py @@ -102,9 +102,10 @@ def load_from_dict(data: dict, lang_code: str): class Language: def __init__(self, lang_code: str = None, fallback_lang_code: str = "zh-CN"): - if lang_code is None: - lang_code = config.get("default_language", get_default_lang()) self.lang_code = lang_code + if self.lang_code is None: + self.lang_code = get_default_lang_code() + self.fallback_lang_code = fallback_lang_code if self.fallback_lang_code is None: self.fallback_lang_code = config.get("default_language", get_system_lang_code()) @@ -159,7 +160,7 @@ def get_user_lang(user_id: str) -> Language: username="Unknown" )) - return Language(user.profile.get("lang", get_default_lang())) + return Language(user.profile.get("lang", get_default_lang_code())) def get_system_lang_code() -> str: @@ -169,11 +170,13 @@ def get_system_lang_code() -> str: return locale.getdefaultlocale()[0].replace('_', '-') -def get_default_lang() -> Language: +def get_default_lang_code() -> str: """ - 获取配置默认/系统语言 + 获取默认语言代码 + Returns: + """ - return Language(config.get("default_language", get_system_lang_code())) + return config.get("default_language", get_system_lang_code()) def get_all_lang() -> dict[str, str]: diff --git a/liteyuki/utils/base/log.py b/liteyuki/utils/base/log.py index 240ab21..3e2b09a 100644 --- a/liteyuki/utils/base/log.py +++ b/liteyuki/utils/base/log.py @@ -2,7 +2,7 @@ import sys import loguru from typing import TYPE_CHECKING from .config import load_from_yaml -from .language import get_default_lang +from .language import Language, get_default_lang_code logger = loguru.logger if TYPE_CHECKING: @@ -64,7 +64,7 @@ def init_log(): format=get_format(config.get("log_level", "INFO")), ) show_icon = config.get("log_icon", True) - lang = get_default_lang() + lang = Language(get_default_lang_code()) debug = lang.get("log.debug", default="==DEBUG") info = lang.get("log.info", default="===INFO") diff --git a/liteyuki/utils/base/resource.py b/liteyuki/utils/base/resource.py index 462af8e..21d636d 100644 --- a/liteyuki/utils/base/resource.py +++ b/liteyuki/utils/base/resource.py @@ -7,11 +7,11 @@ import nonebot import yaml from .data import LiteModel -from .language import get_default_lang +from .language import Language, get_default_lang_code _loaded_resource_packs: list["ResourceMetadata"] = [] # 按照加载顺序排序 temp_resource_root = "data/liteyuki/resources" -lang = get_default_lang() +lang = Language(get_default_lang_code()) class ResourceMetadata(LiteModel):