jumpserver/apps/i18n/translate.py

62 lines
1.9 KiB
Python
Raw Normal View History

2024-01-15 09:14:38 +00:00
import asyncio
import os
from _translator.const import LOCALE_DIR, RED
from _translator.core import CoreTranslateManager
from _translator.other import OtherTranslateManager
from _translator.utils import OpenAITranslate
2024-01-15 09:14:38 +00:00
class Translate:
IGNORE_TRANSLATE_DIRS = ('translate',)
def __init__(self, oai_trans_instance):
self.oai_trans = oai_trans_instance
def get_dir_names(self):
dir_names = []
for name in os.listdir(LOCALE_DIR):
_path = os.path.join(LOCALE_DIR, name)
if not os.path.isdir(_path) or name in self.IGNORE_TRANSLATE_DIRS:
continue
dir_names.append(name)
return dir_names
async def core_trans(self, dir_name):
return
2024-01-15 09:14:38 +00:00
_dir = os.path.join(LOCALE_DIR, dir_name)
zh_file = os.path.join(_dir, 'zh', 'LC_MESSAGES', 'django.po')
if not os.path.exists(zh_file):
2024-01-16 13:15:39 +00:00
print(f'{RED}File: {zh_file} not exists.{RED}')
2024-01-15 09:14:38 +00:00
return
await CoreTranslateManager(_dir, self.oai_trans).run()
async def other_trans(self, dir_name):
_dir = os.path.join(LOCALE_DIR, dir_name)
zh_file = os.path.join(_dir, 'zh.json')
if not os.path.exists(zh_file):
2024-01-16 13:15:39 +00:00
print(f'{RED}File: {zh_file} not exists.{RED}\n')
2024-01-15 09:14:38 +00:00
return
await OtherTranslateManager(_dir, self.oai_trans).run()
async def run(self):
dir_names = self.get_dir_names()
if not dir_names:
return
for dir_name in dir_names:
if dir_name.startswith('_'):
continue
2024-01-15 09:14:38 +00:00
if hasattr(self, f'{dir_name}_trans'):
await getattr(self, f'{dir_name}_trans')(dir_name)
else:
await self.other_trans(dir_name)
if __name__ == '__main__':
oai_trans = OpenAITranslate()
manager = Translate(oai_trans)
asyncio.run(manager.run())