Skip to content

Commit c4e08e2

Browse files
authored
🌐 add locale for exception
1 parent 49f72fd commit c4e08e2

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

nb_cli/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class ModuleLoadFailed(RuntimeError):
22
"""Raised when a module fails to load."""
33

44

5-
class PythonVersionError(RuntimeError):
5+
class PythonInterpreterError(RuntimeError):
66
"""Raised when the Python version is not supported."""
77

88

nb_cli/handlers/meta.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
from wcwidth import wcswidth
2626
from pyfiglet import figlet_format
2727

28-
from nb_cli import cache
28+
from nb_cli import _, cache
2929
from nb_cli.config.model import NoneBotConfig
3030
from nb_cli.consts import WINDOWS, REQUIRES_PYTHON
3131
from nb_cli.config import GLOBAL_CONFIG, Driver, Plugin, Adapter, NoneBotConfig
3232
from nb_cli.exceptions import (
3333
ModuleLoadFailed,
34-
PythonVersionError,
3534
PipNotInstalledError,
35+
PythonInterpreterError,
3636
NoneBotNotInstalledError,
3737
)
3838

@@ -68,6 +68,8 @@ async def get_default_python() -> str:
6868
stdout=asyncio.subprocess.PIPE,
6969
)
7070
stdout, _ = await proc.communicate()
71+
if proc.returncode != 0:
72+
raise PythonInterpreterError(_("Cannot find a valid Python interpreter."))
7173
return json.loads(stdout.strip())
7274

7375

@@ -107,8 +109,10 @@ async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
107109
if (version["major"], version["minor"]) >= REQUIRES_PYTHON:
108110
return await func(*args, **kwargs)
109111

110-
raise PythonVersionError(
111-
f"Python {version['major']}.{version['minor']} is not supported."
112+
raise PythonInterpreterError(
113+
_("Python {major}.{minor} is not supported.").format(
114+
marjo=version["major"], minor=version["minor"]
115+
)
112116
)
113117

114118
return wrapper
@@ -148,7 +152,7 @@ async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
148152
if await get_nonebot_version(cast(Union[str, None], kwargs.get("python_path"))):
149153
return await func(*args, **kwargs)
150154

151-
raise NoneBotNotInstalledError("NoneBot is not installed.")
155+
raise NoneBotNotInstalledError(_("NoneBot is not installed."))
152156

153157
return wrapper
154158

@@ -187,7 +191,7 @@ async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
187191
if await get_pip_version(cast(Union[str, None], kwargs.get("python_path"))):
188192
return await func(*args, **kwargs)
189193

190-
raise PipNotInstalledError("pip is not installed.")
194+
raise PipNotInstalledError(_("pip is not installed."))
191195

192196
return wrapper
193197

@@ -224,7 +228,9 @@ async def load_module_data(
224228
elif module_type == "driver":
225229
module_class = Driver
226230
else:
227-
raise ValueError(f"Invalid module type: {module_type}")
231+
raise ValueError(
232+
_("Invalid module type: {module_type}").format(module_type=module_type)
233+
)
228234
module_name: str = getattr(module_class.__config__, "module_name")
229235

230236
exceptions: List[Exception] = []
@@ -244,7 +250,10 @@ async def load_module_data(
244250
except Exception as e:
245251
exceptions.append(e)
246252

247-
raise ModuleLoadFailed(f"Failed to get {module_name} list.", exceptions)
253+
raise ModuleLoadFailed(
254+
_("Failed to get {module_type} list.").format(module_type=module_type),
255+
exceptions,
256+
)
248257

249258

250259
def format_package_results(

nb_cli/locale/zh_CN/LC_MESSAGES/nb-cli.po

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: nb-cli 1.0.0\n"
99
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
10-
"POT-Creation-Date: 2023-01-21 08:49+0000\n"
10+
"POT-Creation-Date: 2023-01-21 09:01+0000\n"
1111
"PO-Revision-Date: 2023-01-11 08:56+0000\n"
1212
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1313
"Language: zh_Hans_CN\n"
@@ -398,3 +398,27 @@ msgstr "要卸载的包名?"
398398
#: nb_cli/cli/commands/self.py:110
399399
msgid "List installed packages in cli venv."
400400
msgstr "列出 cli 虚拟环境中已安装的包."
401+
402+
#: nb_cli/handlers/meta.py:72
403+
msgid "Cannot find a valid Python interpreter."
404+
msgstr "无法找到可用的 Python 解释器."
405+
406+
#: nb_cli/handlers/meta.py:113
407+
msgid "Python {major}.{minor} is not supported."
408+
msgstr "Python {major}.{minor} 不受支持."
409+
410+
#: nb_cli/handlers/meta.py:155
411+
msgid "NoneBot is not installed."
412+
msgstr "NoneBot 未安装."
413+
414+
#: nb_cli/handlers/meta.py:194
415+
msgid "pip is not installed."
416+
msgstr "pip 未安装."
417+
418+
#: nb_cli/handlers/meta.py:232
419+
msgid "Invalid module type: {module_type}"
420+
msgstr "无效的模块类型: {module_type}"
421+
422+
#: nb_cli/handlers/meta.py:254
423+
msgid "Failed to get {module_type} list."
424+
msgstr "获取 {module_type} 列表失败."

0 commit comments

Comments
 (0)