Skip to content

Commit a28de6f

Browse files
authored
feat: i18n (#2011)
1 parent 41c7ed9 commit a28de6f

File tree

284 files changed

+5440
-4049
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

284 files changed

+5440
-4049
lines changed

apps/application/serializers/application_serializers.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import uuid
1616
from functools import reduce
1717
from typing import Dict, List
18-
1918
from django.contrib.postgres.fields import ArrayField
2019
from django.core import cache, validators
2120
from django.core import signing
@@ -54,8 +53,7 @@
5453
from setting.serializers.provider_serializers import ModelSerializer
5554
from smartdoc.conf import PROJECT_DIR
5655
from users.models import User
57-
from django.db.models import Value
58-
from django.db.models.fields.json import KeyTextTransform
56+
from django.utils.translation import gettext_lazy as _
5957

6058
chat_cache = cache.caches['chat_cache']
6159

@@ -194,10 +192,11 @@ def get_base_node_work_flow(work_flow):
194192

195193

196194
class ApplicationSerializer(serializers.Serializer):
197-
name = serializers.CharField(required=True, max_length=64, min_length=1, error_messages=ErrMessage.char("应用名称"))
195+
name = serializers.CharField(required=True, max_length=64, min_length=1,
196+
error_messages=ErrMessage.char(_("application name")))
198197
desc = serializers.CharField(required=False, allow_null=True, allow_blank=True,
199198
max_length=256, min_length=1,
200-
error_messages=ErrMessage.char("应用描述"))
199+
error_messages=ErrMessage.char(_("application describe")))
201200
model_id = serializers.CharField(required=False, allow_null=True, allow_blank=True,
202201
error_messages=ErrMessage.char("模型"))
203202
dialogue_number = serializers.IntegerField(required=True,

apps/common/auth/authenticate.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from common.exception.app_exception import AppAuthenticationFailed, AppEmbedIdentityFailed, AppChatNumOutOfBoundsFailed, \
1818
ChatException, AppApiException
19-
19+
from django.utils.translation import gettext_lazy as _
2020
token_cache = cache.caches['token_cache']
2121

2222

@@ -59,19 +59,19 @@ def authenticate(self, request):
5959
auth = auth.replace('Bearer ', '')
6060
# 未认证
6161
if auth is None:
62-
raise AppAuthenticationFailed(1003, '未登录,请先登录')
62+
raise AppAuthenticationFailed(1003, _('Not logged in, please log in first'))
6363
try:
6464
token_details = TokenDetails(auth)
6565
for handle in handles:
6666
if handle.support(request, auth, token_details.get_token_details):
6767
return handle.handle(request, auth, token_details.get_token_details)
68-
raise AppAuthenticationFailed(1002, "身份验证信息不正确!非法用户")
68+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
6969
except Exception as e:
7070
traceback.format_exc()
7171
if isinstance(e, AppEmbedIdentityFailed) or isinstance(e, AppChatNumOutOfBoundsFailed) or isinstance(e,
7272
AppApiException):
7373
raise e
74-
raise AppAuthenticationFailed(1002, "身份验证信息不正确!非法用户")
74+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
7575

7676

7777
class TokenAuth(TokenAuthentication):
@@ -80,16 +80,16 @@ def authenticate(self, request):
8080
auth = request.META.get('HTTP_AUTHORIZATION')
8181
# 未认证
8282
if auth is None:
83-
raise AppAuthenticationFailed(1003, '未登录,请先登录')
83+
raise AppAuthenticationFailed(1003, _('Not logged in, please log in first'))
8484
try:
8585
token_details = TokenDetails(auth)
8686
for handle in handles:
8787
if handle.support(request, auth, token_details.get_token_details):
8888
return handle.handle(request, auth, token_details.get_token_details)
89-
raise AppAuthenticationFailed(1002, "身份验证信息不正确!非法用户")
89+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
9090
except Exception as e:
9191
traceback.format_exc()
9292
if isinstance(e, AppEmbedIdentityFailed) or isinstance(e, AppChatNumOutOfBoundsFailed) or isinstance(e,
9393
AppApiException):
9494
raise e
95-
raise AppAuthenticationFailed(1002, "身份验证信息不正确!非法用户")
95+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))

apps/common/auth/authentication.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from common.constants.permission_constants import ViewPermission, CompareConstants, RoleConstants, PermissionConstants, \
1212
Permission
1313
from common.exception.app_exception import AppUnauthorizedFailed
14-
14+
from django.utils.translation import gettext_lazy as _
1515

1616
def exist_permissions_by_permission_constants(user_permission: List[PermissionConstants],
1717
permission_list: List[PermissionConstants]):
@@ -91,7 +91,7 @@ def run(view, request, **kwargs):
9191
# 判断是否有权限
9292
if any(exit_list) if compare == CompareConstants.OR else all(exit_list):
9393
return func(view, request, **kwargs)
94-
raise AppUnauthorizedFailed(403, "没有权限访问")
94+
raise AppUnauthorizedFailed(403, _('No permission to access'))
9595

9696
return run
9797

apps/common/auth/handle/impl/application_key.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
from common.constants.authentication_type import AuthenticationType
1414
from common.constants.permission_constants import Permission, Group, Operate, RoleConstants, Auth
1515
from common.exception.app_exception import AppAuthenticationFailed
16+
from django.utils.translation import gettext_lazy as _
1617

1718

1819
class ApplicationKey(AuthBaseHandle):
1920
def handle(self, request, token: str, get_token_details):
2021
application_api_key = QuerySet(ApplicationApiKey).filter(secret_key=token).first()
2122
if application_api_key is None:
22-
raise AppAuthenticationFailed(500, "secret_key 无效")
23+
raise AppAuthenticationFailed(500, _('Secret key is invalid'))
2324
if not application_api_key.is_active:
24-
raise AppAuthenticationFailed(500, "secret_key 无效")
25+
raise AppAuthenticationFailed(500, _('Secret key is invalid'))
2526
permission_list = [Permission(group=Group.APPLICATION,
2627
operate=Operate.USE,
2728
dynamic_tag=str(

apps/common/auth/handle/impl/public_access_token.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from common.exception.app_exception import AppAuthenticationFailed, ChatException
1616
from common.models.db_model_manage import DBModelManage
1717
from common.util.common import password_encrypt
18-
18+
from django.utils.translation import gettext_lazy as _
1919

2020
class PublicAccessToken(AuthBaseHandle):
2121
def support(self, request, token: str, get_token_details):
@@ -45,13 +45,13 @@ def handle(self, request, token: str, get_token_details):
4545
if application_setting.authentication_value.get('type') != authentication.get(
4646
'type') or password_encrypt(
4747
application_setting.authentication_value.get('value')) != authentication.get('value'):
48-
raise ChatException(1002, "身份验证信息不正确")
48+
raise ChatException(1002, _('Authentication information is incorrect'))
4949
if application_access_token is None:
50-
raise AppAuthenticationFailed(1002, "身份验证信息不正确")
50+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect'))
5151
if not application_access_token.is_active:
52-
raise AppAuthenticationFailed(1002, "身份验证信息不正确")
52+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect'))
5353
if not application_access_token.access_token == auth_details.get('access_token'):
54-
raise AppAuthenticationFailed(1002, "身份验证信息不正确")
54+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect'))
5555

5656
return application_access_token.application.user, Auth(
5757
role_list=[RoleConstants.APPLICATION_ACCESS_TOKEN],

apps/common/auth/handle/impl/user_token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from django.core import cache
1818

1919
from users.models.user import get_user_dynamics_permission
20-
20+
from django.utils.translation import gettext_lazy as _
2121
token_cache = cache.caches['token_cache']
2222

2323

@@ -31,7 +31,7 @@ def support(self, request, token: str, get_token_details):
3131
def handle(self, request, token: str, get_token_details):
3232
cache_token = token_cache.get(token)
3333
if cache_token is None:
34-
raise AppAuthenticationFailed(1002, "登录过期")
34+
raise AppAuthenticationFailed(1002, _('Login expired'))
3535
auth_details = get_token_details()
3636
user = QuerySet(User).get(id=auth_details['id'])
3737
# 续期

apps/common/constants/exception_code_constants.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from enum import Enum
1010

1111
from common.exception.app_exception import AppApiException
12+
from django.utils.translation import gettext_lazy as _
1213

1314

1415
class ExceptionCodeConstantsValue:
@@ -27,13 +28,16 @@ def to_app_api_exception(self):
2728

2829

2930
class ExceptionCodeConstants(Enum):
30-
INCORRECT_USERNAME_AND_PASSWORD = ExceptionCodeConstantsValue(1000, "用户名或者密码不正确")
31-
NOT_AUTHENTICATION = ExceptionCodeConstantsValue(1001, "请先登录,并携带用户Token")
32-
EMAIL_SEND_ERROR = ExceptionCodeConstantsValue(1002, "邮件发送失败")
33-
EMAIL_FORMAT_ERROR = ExceptionCodeConstantsValue(1003, "邮箱格式错误")
34-
EMAIL_IS_EXIST = ExceptionCodeConstantsValue(1004, "邮箱已经被注册,请勿重复注册")
35-
EMAIL_IS_NOT_EXIST = ExceptionCodeConstantsValue(1005, "邮箱尚未注册,请先注册")
36-
CODE_ERROR = ExceptionCodeConstantsValue(1005, "验证码不正确,或者验证码过期")
37-
USERNAME_IS_EXIST = ExceptionCodeConstantsValue(1006, "用户名已被使用,请使用其他用户名")
38-
USERNAME_ERROR = ExceptionCodeConstantsValue(1006, "用户名不能为空,并且长度在6-20")
39-
PASSWORD_NOT_EQ_RE_PASSWORD = ExceptionCodeConstantsValue(1007, "密码与确认密码不一致")
31+
INCORRECT_USERNAME_AND_PASSWORD = ExceptionCodeConstantsValue(1000, _('The username or password is incorrect'))
32+
NOT_AUTHENTICATION = ExceptionCodeConstantsValue(1001, _('Please log in first and bring the user Token'))
33+
EMAIL_SEND_ERROR = ExceptionCodeConstantsValue(1002, _('Email sending failed'))
34+
EMAIL_FORMAT_ERROR = ExceptionCodeConstantsValue(1003, _('Email format error'))
35+
EMAIL_IS_EXIST = ExceptionCodeConstantsValue(1004, _('The email has been registered, please log in directly'))
36+
EMAIL_IS_NOT_EXIST = ExceptionCodeConstantsValue(1005, _('The email is not registered, please register first'))
37+
CODE_ERROR = ExceptionCodeConstantsValue(1005,
38+
_('The verification code is incorrect or the verification code has expired'))
39+
USERNAME_IS_EXIST = ExceptionCodeConstantsValue(1006, _('The username has been registered, please log in directly'))
40+
USERNAME_ERROR = ExceptionCodeConstantsValue(1006,
41+
_('The username cannot be empty and must be between 6 and 20 characters long.'))
42+
PASSWORD_NOT_EQ_RE_PASSWORD = ExceptionCodeConstantsValue(1007,
43+
_('Password and confirmation password are inconsistent'))

apps/common/constants/permission_constants.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88
from enum import Enum
99
from typing import List
10-
10+
from django.utils.translation import gettext_lazy as _
1111

1212
class Group(Enum):
1313
"""
@@ -58,10 +58,10 @@ def __init__(self, name: str, decs: str, group: RoleGroup):
5858

5959

6060
class RoleConstants(Enum):
61-
ADMIN = Role("管理员", "管理员,预制目前不会使用", RoleGroup.USER)
62-
USER = Role("用户", "用户所有权限", RoleGroup.USER)
63-
APPLICATION_ACCESS_TOKEN = Role("会话", "只拥有应用会话框接口权限", RoleGroup.APPLICATION_ACCESS_TOKEN),
64-
APPLICATION_KEY = Role("应用私钥", "应用私钥", RoleGroup.APPLICATION_KEY)
61+
ADMIN = Role(_("ADMIN"), _('Admin, prefabs are not currently used'), RoleGroup.USER)
62+
USER = Role(_("USER"), _('All user permissions'), RoleGroup.USER)
63+
APPLICATION_ACCESS_TOKEN = Role(_('chat'), _('Only has application dialog interface permissions'), RoleGroup.APPLICATION_ACCESS_TOKEN),
64+
APPLICATION_KEY = Role(_('Apply private key'), _('Apply private key'), RoleGroup.APPLICATION_KEY)
6565

6666

6767
class Permission:

apps/common/event/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import setting.models
1010
from setting.models import Model
1111
from .listener_manage import *
12-
from common.db.sql_execute import update_execute
12+
from django.utils.translation import gettext as _
13+
14+
from ..db.sql_execute import update_execute
1315

1416
update_document_status_sql = """
1517
UPDATE "public"."document"
@@ -20,5 +22,5 @@
2022
def run():
2123
# QuerySet(Document).filter(status__in=[Status.embedding, Status.queue_up]).update(**{'status': Status.error})
2224
QuerySet(Model).filter(status=setting.models.Status.DOWNLOAD).update(status=setting.models.Status.ERROR,
23-
meta={'message': "下载程序被中断,请重试"})
25+
meta={'message': _('The download process was interrupted, please try again')})
2426
update_execute(update_document_status_sql, [])

apps/common/event/listener_manage.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from dataset.models import Paragraph, Status, Document, ProblemParagraphMapping, TaskType, State
2626
from embedding.models import SourceType, SearchMode
2727
from smartdoc.conf import PROJECT_DIR
28+
from django.utils.translation import gettext_lazy as _
2829

2930
max_kb_error = logging.getLogger(__file__)
3031
max_kb = logging.getLogger(__file__)
@@ -86,11 +87,12 @@ def embedding_by_paragraph_list(paragraph_id_list, embedding_model: Embeddings):
8687
ListenerManagement.embedding_by_paragraph_data_list(data_list, paragraph_id_list=paragraph_id_list,
8788
embedding_model=embedding_model)
8889
except Exception as e:
89-
max_kb_error.error(f'查询向量数据:{paragraph_id_list}出现错误{str(e)}{traceback.format_exc()}')
90+
max_kb_error.error(_('Query vector data: {paragraph_id_list} error {error} {traceback}').format(
91+
paragraph_id_list=paragraph_id_list, error=str(e), traceback=traceback.format_exc()))
9092

9193
@staticmethod
9294
def embedding_by_paragraph_data_list(data_list, paragraph_id_list, embedding_model: Embeddings):
93-
max_kb.info(f'开始--->向量化段落:{paragraph_id_list}')
95+
max_kb.info(_('Start--->Embedding paragraph: {paragraph_id_list}').format(paragraph_id_list=paragraph_id_list))
9496
status = Status.success
9597
try:
9698
# 删除段落
@@ -102,11 +104,13 @@ def is_save_function():
102104
# 批量向量化
103105
VectorStore.get_embedding_vector().batch_save(data_list, embedding_model, is_save_function)
104106
except Exception as e:
105-
max_kb_error.error(f'向量化段落:{paragraph_id_list}出现错误{str(e)}{traceback.format_exc()}')
107+
max_kb_error.error(_('Vectorized paragraph: {paragraph_id_list} error {error} {traceback}').format(
108+
paragraph_id_list=paragraph_id_list, error=str(e), traceback=traceback.format_exc()))
106109
status = Status.error
107110
finally:
108111
QuerySet(Paragraph).filter(id__in=paragraph_id_list).update(**{'status': status})
109-
max_kb.info(f'结束--->向量化段落:{paragraph_id_list}')
112+
max_kb.info(
113+
_('End--->Embedding paragraph: {paragraph_id_list}').format(paragraph_id_list=paragraph_id_list))
110114

111115
@staticmethod
112116
def embedding_by_paragraph(paragraph_id, embedding_model: Embeddings):
@@ -115,7 +119,7 @@ def embedding_by_paragraph(paragraph_id, embedding_model: Embeddings):
115119
@param paragraph_id: 段落id
116120
@param embedding_model: 向量模型
117121
"""
118-
max_kb.info(f"开始--->向量化段落:{paragraph_id}")
122+
max_kb.info(_('Start--->Embedding paragraph: {paragraph_id}').format(paragraph_id=paragraph_id))
119123
# 更新到开始状态
120124
ListenerManagement.update_status(QuerySet(Paragraph).filter(id=paragraph_id), TaskType.EMBEDDING, State.STARTED)
121125
try:
@@ -140,11 +144,12 @@ def is_the_task_interrupted():
140144
ListenerManagement.update_status(QuerySet(Paragraph).filter(id=paragraph_id), TaskType.EMBEDDING,
141145
State.SUCCESS)
142146
except Exception as e:
143-
max_kb_error.error(f'向量化段落:{paragraph_id}出现错误{str(e)}{traceback.format_exc()}')
147+
max_kb_error.error(_('Vectorized paragraph: {paragraph_id} error {error} {traceback}').format(
148+
paragraph_id=paragraph_id, error=str(e), traceback=traceback.format_exc()))
144149
ListenerManagement.update_status(QuerySet(Paragraph).filter(id=paragraph_id), TaskType.EMBEDDING,
145150
State.FAILURE)
146151
finally:
147-
max_kb.info(f'结束--->向量化段落:{paragraph_id}')
152+
max_kb.info(_('End--->Embedding paragraph: {paragraph_id}').format(paragraph_id=paragraph_id))
148153

149154
@staticmethod
150155
def embedding_by_data_list(data_list: List, embedding_model: Embeddings):
@@ -258,7 +263,8 @@ def is_the_task_interrupted():
258263

259264
if is_the_task_interrupted():
260265
return
261-
max_kb.info(f"开始--->向量化文档:{document_id}")
266+
max_kb.info(_('Start--->Embedding document: {document_id}').format(document_id=document_id)
267+
)
262268
# 批量修改状态为PADDING
263269
ListenerManagement.update_status(QuerySet(Document).filter(id=document_id), TaskType.EMBEDDING,
264270
State.STARTED)
@@ -279,11 +285,12 @@ def is_the_task_interrupted():
279285
document_id)),
280286
is_the_task_interrupted)
281287
except Exception as e:
282-
max_kb_error.error(f'向量化文档:{document_id}出现错误{str(e)}{traceback.format_exc()}')
288+
max_kb_error.error(_('Vectorized document: {document_id} error {error} {traceback}').format(
289+
document_id=document_id, error=str(e), traceback=traceback.format_exc()))
283290
finally:
284291
ListenerManagement.post_update_document_status(document_id, TaskType.EMBEDDING)
285292
ListenerManagement.get_aggregation_document_status(document_id)()
286-
max_kb.info(f"结束--->向量化文档:{document_id}")
293+
max_kb.info(_('End--->Embedding document: {document_id}').format(document_id=document_id))
287294
un_lock('embedding' + str(document_id))
288295

289296
@staticmethod
@@ -294,17 +301,18 @@ def embedding_by_dataset(dataset_id, embedding_model: Embeddings):
294301
@param embedding_model 向量模型
295302
:return: None
296303
"""
297-
max_kb.info(f"开始--->向量化数据集:{dataset_id}")
304+
max_kb.info(_('Start--->Embedding dataset: {dataset_id}').format(dataset_id=dataset_id))
298305
try:
299306
ListenerManagement.delete_embedding_by_dataset(dataset_id)
300307
document_list = QuerySet(Document).filter(dataset_id=dataset_id)
301-
max_kb.info(f"数据集文档:{[d.name for d in document_list]}")
308+
max_kb.info(_('Start--->Embedding document: {document_list}').format(document_list=document_list))
302309
for document in document_list:
303310
ListenerManagement.embedding_by_document(document.id, embedding_model=embedding_model)
304311
except Exception as e:
305-
max_kb_error.error(f'向量化数据集:{dataset_id}出现错误{str(e)}{traceback.format_exc()}')
312+
max_kb_error.error(_('Vectorized dataset: {dataset_id} error {error} {traceback}').format(
313+
dataset_id=dataset_id, error=str(e), traceback=traceback.format_exc()))
306314
finally:
307-
max_kb.info(f"结束--->向量化数据集:{dataset_id}")
315+
max_kb.info(_('End--->Embedding dataset: {dataset_id}').format(dataset_id=dataset_id))
308316

309317
@staticmethod
310318
def delete_embedding_by_document(document_id):

0 commit comments

Comments
 (0)