jumpserver/apps/locale/translate/manager/other.py

47 lines
1.8 KiB
Python

import json
import os
from .base import BaseTranslateManager
class OtherTranslateManager(BaseTranslateManager):
@staticmethod
def get_need_trans_dict(zh_dict, other_dict):
diff_keys = set(zh_dict.keys()) - set(other_dict.keys())
need_trans_dict = {k: zh_dict[k] for k in diff_keys if k}
return need_trans_dict
def load_json_as_dict(self, file_prefix='zh'):
file_path = os.path.join(self._dir, f'{file_prefix}.json')
if not os.path.exists(file_path):
return {}
try:
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
print(f'File: {file_path} load error: {e}')
return {}
def save_dict_as_json(self, data, file_prefix='ja'):
file_path = os.path.join(self._dir, f'{file_prefix}.json')
try:
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, sort_keys=True, indent=4)
except Exception as e:
print(f'File: {file_path} save error: {e}')
async def run(self):
zh_dict = self.load_json_as_dict()
for file_prefix, target_lang in self.LANG_MAPPER.items():
other_dict = self.load_json_as_dict(file_prefix)
need_trans_dict = self.get_need_trans_dict(zh_dict, other_dict)
print(f'File: {file_prefix}.json need to translate: {len(need_trans_dict)}')
if not need_trans_dict:
print(f'File: {file_prefix}.json is already translated.')
continue
translated_dict = await self.bulk_translate(need_trans_dict, target_lang)
other_dict.update(translated_dict)
self.save_dict_as_json(other_dict, file_prefix)