Интернационализация для всех сервисов сервера
This commit is contained in:
parent
7491ce455d
commit
ba8cea01e7
13 changed files with 1011 additions and 206 deletions
|
|
@ -8,7 +8,9 @@ from sdbus_async.networkmanager import (
|
|||
import sdbus
|
||||
from sdbus_async.networkmanager.settings import ConnectionProfile
|
||||
from services.arp_service import NetworkScanner
|
||||
|
||||
from utils.i18n import get_translations, _
|
||||
from babel.support import Translations
|
||||
from fastapi import Depends
|
||||
|
||||
class IPService:
|
||||
"""Сервис проверки доступности IP-адреса и применения настроек сети.
|
||||
|
|
@ -17,7 +19,7 @@ class IPService:
|
|||
ARP-сканирования и применения настроек через NetworkManager.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, translations: Translations = Depends(get_translations("services"))):
|
||||
"""Инициализирует сервис IP.
|
||||
|
||||
Создаёт объект ARP-сканера и устанавливает системную шину D-Bus как
|
||||
|
|
@ -26,6 +28,7 @@ class IPService:
|
|||
self.arp_scanner = NetworkScanner()
|
||||
self.bus = sdbus.sd_bus_open_system()
|
||||
sdbus.set_default_bus(self.bus)
|
||||
self.translations = translations
|
||||
|
||||
def _reset_bus(self):
|
||||
"""Перезапускает соединение с системной шиной D-Bus.
|
||||
|
|
@ -63,11 +66,11 @@ class IPService:
|
|||
if any(client["ip"] == new_ip for client in clients):
|
||||
return {
|
||||
"available": False,
|
||||
"message": f"IP address {new_ip} уже занят."
|
||||
"message": self.translations.gettext("IP-адрес {ip} уже занят.").format(ip=new_ip)
|
||||
}
|
||||
return {
|
||||
"available": True,
|
||||
"message": "Network profile settings are available for use.",
|
||||
"message": self.translations.gettext("Настройки сетевого профиля доступны для использования.")
|
||||
}
|
||||
|
||||
async def apply_ip_config(self, args: Dict[str, Any]) -> None:
|
||||
|
|
@ -95,7 +98,7 @@ class IPService:
|
|||
args["connection_id"])
|
||||
|
||||
if not connection_paths:
|
||||
raise ValueError(f"Подключение {args['connection_id']} не найдено")
|
||||
raise ValueError(self.translations.gettext("Подключение {connection_id} не найдено").format(connection_id=args["connection_id"]))
|
||||
|
||||
connection = NetworkConnectionSettings(connection_paths[0])
|
||||
profile = await connection.get_profile()
|
||||
|
|
@ -110,7 +113,7 @@ class IPService:
|
|||
|
||||
new_profile = ConnectionProfile.from_settings_dict(settings_dict)
|
||||
await connection.update(new_profile.to_dbus())
|
||||
print(f"Обновлены настройки для {args['connection_id']}:")
|
||||
print(f"Updated settings for {args['connection_id']}")
|
||||
|
||||
interface_name = settings_dict["connection"]["interface-name"]
|
||||
devices = await nm.get_devices()
|
||||
|
|
@ -118,9 +121,9 @@ class IPService:
|
|||
device = NetworkDeviceGeneric(device_path)
|
||||
if await device.interface == interface_name:
|
||||
await device.disconnect()
|
||||
print(f"Отключён от {interface_name}")
|
||||
await nm.activate_connection(connection=connection_paths[0],device=device_path)
|
||||
print(f"Переподключён к {args['connection_id']}")
|
||||
print(f"Disconnected from {interface_name}")
|
||||
await nm.activate_connection(connection=connection_paths[0], device=device_path)
|
||||
print(f"Reconnected to {args['connection_id']}")
|
||||
return
|
||||
|
||||
raise RuntimeError(f"Устройство для интерфейса {interface_name} не найдено")
|
||||
raise RuntimeError(self.translations.gettext("Устройство для интерфейса {interface} не найдено").format(interface=interface_name))
|
||||
Loading…
Add table
Add a link
Reference in a new issue