From 6b3665e8d01fc4efd26c55c9e4efd1cc88e4c131 Mon Sep 17 00:00:00 2001 From: ibuler Date: Thu, 20 Apr 2023 11:13:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?perf:=20yaml=20=E6=96=87=E4=BB=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20i18n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../database/mysql/manifest.yml | 10 +++++- apps/assets/automations/methods.py | 5 +-- .../automations/ping/host/posix/manifest.yml | 10 +++++- apps/common/utils/yml.py | 34 +++++++++++++++++++ apps/terminal/applets/chrome/i18n.yml | 4 --- apps/terminal/applets/chrome/manifest.yml | 15 ++++++-- apps/terminal/applets/dbeaver/manifest.yml | 15 ++++++-- apps/terminal/models/applet/applet.py | 5 +-- 8 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 apps/common/utils/yml.py delete mode 100644 apps/terminal/applets/chrome/i18n.yml diff --git a/apps/accounts/automations/gather_accounts/database/mysql/manifest.yml b/apps/accounts/automations/gather_accounts/database/mysql/manifest.yml index be104b783..22f57156c 100644 --- a/apps/accounts/automations/gather_accounts/database/mysql/manifest.yml +++ b/apps/accounts/automations/gather_accounts/database/mysql/manifest.yml @@ -1,7 +1,15 @@ id: gather_accounts_mysql -name: Gather account from MySQL +name: "{{ 'Gather account from MySQL' | trans }}" category: database type: - mysql - mariadb method: gather_accounts + +i18n: + en: + Gather account from MySQL: Gather account from MySQL + zh: + Gather account from MySQL: 从MySQL获取账号 + ja: + Gather account from MySQL: MySQLからアカウントを取得する diff --git a/apps/assets/automations/methods.py b/apps/assets/automations/methods.py index cad2b1b3c..5b80bb547 100644 --- a/apps/assets/automations/methods.py +++ b/apps/assets/automations/methods.py @@ -2,7 +2,7 @@ import json import os from functools import partial -import yaml +from common.utils.yml import yaml_load_with_i18n def check_platform_method(manifest, manifest_path): @@ -40,7 +40,8 @@ def get_platform_automation_methods(path): continue with open(path, 'r') as f: - manifest = yaml.safe_load(f) + print("path: ", path) + manifest = yaml_load_with_i18n(f) check_platform_method(manifest, path) manifest['dir'] = os.path.dirname(path) manifest['params_serializer'] = generate_serializer(manifest) diff --git a/apps/assets/automations/ping/host/posix/manifest.yml b/apps/assets/automations/ping/host/posix/manifest.yml index 4b9afde37..7e00c99f9 100644 --- a/apps/assets/automations/ping/host/posix/manifest.yml +++ b/apps/assets/automations/ping/host/posix/manifest.yml @@ -1,7 +1,15 @@ id: posix_ping -name: Posix ping +name: "{{ 'Posix ping' | trans }}" category: host type: - linux - unix method: ping + +i18n: + zh: + Posix ping: Posix 测试 + ja: + Posix ping: Posix ピング + en: + Posix ping: Posix ping diff --git a/apps/common/utils/yml.py b/apps/common/utils/yml.py new file mode 100644 index 000000000..2a2954514 --- /dev/null +++ b/apps/common/utils/yml.py @@ -0,0 +1,34 @@ +import io + +import yaml +from django.utils.translation import get_language +from jinja2 import Environment + + +def translate(i18n, key): + lang = get_language()[:2] + lang_data = i18n.get(lang, {}) + return lang_data.get(key, key) + + +def yaml_load_with_i18n(stream): + ori_text = stream.read() + stream = io.StringIO(ori_text) + yaml_data = yaml.safe_load(stream) + i18n = yaml_data.get('i18n', {}) + + env = Environment() + env.filters['trans'] = lambda key: translate(i18n, key) + template = env.from_string(ori_text) + yaml_data = template.render() + yaml_f = io.StringIO(yaml_data) + d = yaml.safe_load(yaml_f) + if isinstance(d, dict): + d.pop('i18n', None) + return d + + +if __name__ == '__main__': + with open('manifest.yml') as f: + data = yaml_load_with_i18n(f) + print(data) diff --git a/apps/terminal/applets/chrome/i18n.yml b/apps/terminal/applets/chrome/i18n.yml deleted file mode 100644 index d91977ba6..000000000 --- a/apps/terminal/applets/chrome/i18n.yml +++ /dev/null @@ -1,4 +0,0 @@ -- zh: - display_name: Chrome 浏览器 - comment: 浏览器打开 URL 页面地址 - diff --git a/apps/terminal/applets/chrome/manifest.yml b/apps/terminal/applets/chrome/manifest.yml index 850ec3f08..3b192ffee 100644 --- a/apps/terminal/applets/chrome/manifest.yml +++ b/apps/terminal/applets/chrome/manifest.yml @@ -1,7 +1,7 @@ name: chrome -display_name: Chrome Browser +display_name: "{{ 'Chrome Browser' | trans }}" version: 0.2 -comment: Chrome Browser Open URL Page Address +comment: "{{ 'Chrome Browser Open URL Page Address' | trans }}" author: JumpServer Team exec_type: python update_policy: always @@ -10,3 +10,14 @@ tags: - web protocols: - http + +i18n: + en: + Chrome Browser: Chrome Browser + Chrome Browser Open URL Page Address: Chrome Browser Open URL Page Address + zh: + Chrome Browser: Chrome 浏览器 + Chrome Browser Open URL Page Address: Chrome 浏览器打开网页地址 + ja: + Chrome Browser: Chrome ブラウザ + Chrome Browser Open URL Page Address: Chrome ブラウザでウェブページを開く diff --git a/apps/terminal/applets/dbeaver/manifest.yml b/apps/terminal/applets/dbeaver/manifest.yml index c8d44af64..dbf3c6dc3 100644 --- a/apps/terminal/applets/dbeaver/manifest.yml +++ b/apps/terminal/applets/dbeaver/manifest.yml @@ -1,6 +1,6 @@ name: dbeaver -display_name: DBeaver Community -comment: Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases. +display_name: "{{ 'DBeaver Community' | trans }}" +comment: "{{ 'Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases.' | trans }}" version: 0.1 exec_type: python author: JumpServer Team @@ -14,3 +14,14 @@ protocols: - postgresql - sqlserver - oracle + +i18n: + en: + DBeaver Community: DBeaver Community + Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases.: Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases. + zh: + DBeaver Community: DBeaver 社区版 + Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases.: 免费的多平台数据库工具,供开发人员、数据库管理员、分析师和所有需要使用数据库的人使用。 + ja: + DBeaver Community: DBeaver コミュニティ + Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases.: 開発者、データベース管理者、分析家、およびデータベースを使用する必要があるすべての人のための無料のマルチプラットフォームデータベースツール。 diff --git a/apps/terminal/models/applet/applet.py b/apps/terminal/models/applet/applet.py index c6e938028..786bc35de 100644 --- a/apps/terminal/models/applet/applet.py +++ b/apps/terminal/models/applet/applet.py @@ -12,6 +12,7 @@ from rest_framework.serializers import ValidationError from common.db.models import JMSBaseModel from common.utils import lazyproperty, get_logger +from common.utils.yml import yaml_load_with_i18n logger = get_logger(__name__) @@ -76,14 +77,14 @@ class Applet(JMSBaseModel): @staticmethod def validate_pkg(d): - files = ['manifest.yml', 'icon.png', 'i18n.yml', 'setup.yml'] + files = ['manifest.yml', 'icon.png', 'setup.yml'] for name in files: path = os.path.join(d, name) if not os.path.exists(path): raise ValidationError({'error': _('Applet pkg not valid, Missing file {}').format(name)}) with open(os.path.join(d, 'manifest.yml')) as f: - manifest = yaml.safe_load(f) + manifest = yaml_load_with_i18n(f) if not manifest.get('name', ''): raise ValidationError({'error': 'Missing name in manifest.yml'}) From f2d34de161756639bd0babc29f105973f9d802b2 Mon Sep 17 00:00:00 2001 From: ibuler Date: Thu, 20 Apr 2023 11:27:10 +0800 Subject: [PATCH 2/2] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../automations/ping/host/posix/manifest.yml | 10 ++++------ apps/common/utils/yml.py | 4 ++-- apps/terminal/applets/chrome/manifest.yml | 17 ++++++++--------- apps/terminal/applets/dbeaver/i18n.yml | 3 --- apps/terminal/applets/dbeaver/manifest.yml | 18 +++++++++--------- 5 files changed, 23 insertions(+), 29 deletions(-) delete mode 100644 apps/terminal/applets/dbeaver/i18n.yml diff --git a/apps/assets/automations/ping/host/posix/manifest.yml b/apps/assets/automations/ping/host/posix/manifest.yml index 7e00c99f9..1e6143480 100644 --- a/apps/assets/automations/ping/host/posix/manifest.yml +++ b/apps/assets/automations/ping/host/posix/manifest.yml @@ -7,9 +7,7 @@ type: method: ping i18n: - zh: - Posix ping: Posix 测试 - ja: - Posix ping: Posix ピング - en: - Posix ping: Posix ping + Posix ping: + zh: Posix ping + en: Posix ping + ja: Posix ピング diff --git a/apps/common/utils/yml.py b/apps/common/utils/yml.py index 2a2954514..540f1a31f 100644 --- a/apps/common/utils/yml.py +++ b/apps/common/utils/yml.py @@ -7,8 +7,8 @@ from jinja2 import Environment def translate(i18n, key): lang = get_language()[:2] - lang_data = i18n.get(lang, {}) - return lang_data.get(key, key) + lang_data = i18n.get(key, {}) + return lang_data.get(lang, key) def yaml_load_with_i18n(stream): diff --git a/apps/terminal/applets/chrome/manifest.yml b/apps/terminal/applets/chrome/manifest.yml index 3b192ffee..1662a0422 100644 --- a/apps/terminal/applets/chrome/manifest.yml +++ b/apps/terminal/applets/chrome/manifest.yml @@ -12,12 +12,11 @@ protocols: - http i18n: - en: - Chrome Browser: Chrome Browser - Chrome Browser Open URL Page Address: Chrome Browser Open URL Page Address - zh: - Chrome Browser: Chrome 浏览器 - Chrome Browser Open URL Page Address: Chrome 浏览器打开网页地址 - ja: - Chrome Browser: Chrome ブラウザ - Chrome Browser Open URL Page Address: Chrome ブラウザでウェブページを開く + Chrome Browser: + en: Chrome Browser + zh: Chrome 浏览器 + ja: Chrome ブラウザ + Chrome Browser Open URL Page Address: + en: Chrome Browser Open URL Page Address + zh: Chrome 浏览器打开网页地址 + ja: Chrome ブラウザでウェブページを開く diff --git a/apps/terminal/applets/dbeaver/i18n.yml b/apps/terminal/applets/dbeaver/i18n.yml deleted file mode 100644 index fec926496..000000000 --- a/apps/terminal/applets/dbeaver/i18n.yml +++ /dev/null @@ -1,3 +0,0 @@ -- zh: - display_name: DBeaver Community - comment: 免费的多平台数据库工具,供开发人员、数据库管理员、分析师和所有需要使用数据库的人使用。 diff --git a/apps/terminal/applets/dbeaver/manifest.yml b/apps/terminal/applets/dbeaver/manifest.yml index dbf3c6dc3..5b834cf99 100644 --- a/apps/terminal/applets/dbeaver/manifest.yml +++ b/apps/terminal/applets/dbeaver/manifest.yml @@ -16,12 +16,12 @@ protocols: - oracle i18n: - en: - DBeaver Community: DBeaver Community - Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases.: Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases. - zh: - DBeaver Community: DBeaver 社区版 - Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases.: 免费的多平台数据库工具,供开发人员、数据库管理员、分析师和所有需要使用数据库的人使用。 - ja: - DBeaver Community: DBeaver コミュニティ - Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases.: 開発者、データベース管理者、分析家、およびデータベースを使用する必要があるすべての人のための無料のマルチプラットフォームデータベースツール。 + DBeaver Community: + en: DBeaver Community + zh: DBeaver 社区版 + ja: DBeaver コミュニティ + + Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases.: + en: Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases. + zh: 免费的多平台数据库工具,供开发人员、数据库管理员、分析师和所有需要使用数据库的人使用。 + ja: 開発者、データベース管理者、分析家、およびデータベースを使用する必要があるすべての人のための無料のマルチプラットフォームデータベースツール。