diff --git a/README.md b/README.md index 9f5c8811fa..d533a62cf9 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,10 @@ Domain Admin是一个轻量级监控方案,占用系统资源较少。同时 - 辅助功能:Let’s Encrypt SSL证书申请 - 项目地址: - - github: [https://github.com/mouday/domain-admin](https://github.com/mouday/domain-admin) - - 国内镜像:[https://gitee.com/mouday/domain-admin](https://gitee.com/mouday/domain-admin) + - 后端代码(github): [https://github.com/mouday/domain-admin](https://github.com/mouday/domain-admin) + - 后端代码(国内镜像):[https://gitee.com/mouday/domain-admin](https://gitee.com/mouday/domain-admin) + - 前端代码:[https://github.com/mouday/domain-admin-web](https://github.com/mouday/domain-admin-web) + - H5端代码:[https://github.com/mouday/domain-admin-mini](https://github.com/mouday/domain-admin-mini) - 发布渠道: - pypi:[https://pypi.org/project/domain-admin](https://pypi.org/project/domain-admin) diff --git a/domain_admin/config/default_config.py b/domain_admin/config/default_config.py index a1af1fc3d9..d0284c1093 100644 --- a/domain_admin/config/default_config.py +++ b/domain_admin/config/default_config.py @@ -14,6 +14,9 @@ # header请求头中携带 token 参数名称 TOKEN_KEY = 'X-Token' +# header请求头中携带 language 参数名称 +LANGUAGE_KEY = 'X-Language' + # 默认的token有效时长 单位:天 DEFAULT_TOKEN_EXPIRE_DAYS = 7 diff --git a/domain_admin/i18n/__init__.py b/domain_admin/i18n/__init__.py new file mode 100644 index 0000000000..3936c7ff35 --- /dev/null +++ b/domain_admin/i18n/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +""" +@File : __init__.py +@Date : 2023-08-04 +""" +from .main import translate diff --git a/domain_admin/i18n/config.py b/domain_admin/i18n/config.py new file mode 100644 index 0000000000..de90bd99f9 --- /dev/null +++ b/domain_admin/i18n/config.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +""" +@File : config.py +@Date : 2023-08-04 +""" +translate_config = [ + { + 'zh-CN': '简体中文', + 'en-US': 'English', + }, + { + 'zh-CN': '刚刚', + 'en-US': 'now', + }, + { + 'zh-CN': '分钟前', + 'en-US': ' minutes ago', + }, + { + 'zh-CN': '小时前', + 'en-US': ' hours ago', + }, + { + 'zh-CN': '天前', + 'en-US': ' days ago', + }, + +] diff --git a/domain_admin/i18n/main.py b/domain_admin/i18n/main.py new file mode 100644 index 0000000000..5d85da7408 --- /dev/null +++ b/domain_admin/i18n/main.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +""" +@File : main.py +@Date : 2023-08-04 +""" +from flask import request + +from domain_admin.config import LANGUAGE_KEY +from domain_admin.i18n.config import translate_config + +translate_map = {} + +for item in translate_config: + translate_map[item['zh-CN']] = item + + +def translate(text): + language = 'zh-CN' + + try: + language = request.headers.get(LANGUAGE_KEY) + except Exception as e: + pass + + if text in translate_map: + return translate_map[text].get(language) or text + else: + return text diff --git a/domain_admin/utils/datetime_util.py b/domain_admin/utils/datetime_util.py index df92cfd8c9..45b7bfb445 100644 --- a/domain_admin/utils/datetime_util.py +++ b/domain_admin/utils/datetime_util.py @@ -7,6 +7,7 @@ from datetime import datetime import math import six +from domain_admin import i18n DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" @@ -135,13 +136,13 @@ def time_for_human(time_value): duration = now_time - time_value if duration < minute: - return '刚刚' + return i18n.translate('刚刚') elif duration < hour: - return six.text_type(int(duration / minute)) + '分钟前' + return six.text_type(int(duration / minute)) + i18n.translate('分钟前') elif duration < day: - return six.text_type(int(duration / hour)) + '小时前' + return six.text_type(int(duration / hour)) + i18n.translate('小时前') elif duration < day_8: - return six.text_type(int(duration / day)) + '天前' + return six.text_type(int(duration / day)) + i18n.translate('天前') else: return time.strftime(DATE_FORMAT, time.localtime(time_value))