You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jumpserver/apps/i18n/translate.py

61 lines
1.8 KiB

11 months ago
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
11 months ago
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):
_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):
print(f'{RED}File: {zh_file} not exists.{RED}')
11 months ago
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):
print(f'{RED}File: {zh_file} not exists.{RED}\n')
11 months ago
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
11 months ago
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())