Skip to content

Commit d963ed4

Browse files
committed
feat: i18n
1 parent 9d96ed3 commit d963ed4

File tree

7 files changed

+76
-80
lines changed

7 files changed

+76
-80
lines changed

apps/application/serializers/application_serializers.py

+6-6
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,
@@ -268,7 +267,8 @@ def get_embed(self, with_valid=True, params=None):
268267
float_location = application_setting.float_location
269268
if application_setting.custom_theme is not None and len(
270269
application_setting.custom_theme.get('header_font_color', 'rgb(100, 106, 115)')) > 0:
271-
header_font_color = application_setting.custom_theme.get('header_font_color', 'rgb(100, 106, 115)')
270+
header_font_color = application_setting.custom_theme.get('header_font_color',
271+
'rgb(100, 106, 115)')
272272

273273
is_auth = 'true' if application_access_token is not None and application_access_token.is_active else 'false'
274274
t = Template(content)

apps/common/util/field_message.py

+26-71
Original file line numberDiff line numberDiff line change
@@ -6,112 +6,67 @@
66
@date:2024/3/1 14:30
77
@desc:
88
"""
9-
from django.utils.translation import gettext_lazy
9+
from django.utils.functional import lazy
10+
from rest_framework import serializers
11+
12+
13+
def value_(field, value):
14+
return f"【{field}{value}"
15+
16+
17+
def reset_messages(field, messages):
18+
return {key: lazy(value_, str)(field, messages.get(key)) for key in messages}
19+
20+
21+
def reset_message_by_field(field_text, field):
22+
return reset_messages(field_text, {**field.default_error_messages, **field.__bases__[0].default_error_messages})
1023

1124

1225
class ErrMessage:
1326
@staticmethod
1427
def char(field: str):
15-
return {
16-
'invalid': gettext_lazy("【%s】不是有效的字符串。" % field),
17-
'blank': gettext_lazy("【%s】此字段不能为空字符串。" % field),
18-
'max_length': gettext_lazy("【%s】请确保此字段的字符数不超过 {max_length} 个。" % field),
19-
'min_length': gettext_lazy("【%s】请确保此字段至少包含 {min_length} 个字符。" % field),
20-
'required': gettext_lazy('【%s】此字段必填。' % field),
21-
'null': gettext_lazy('【%s】此字段不能为null。' % field)
22-
}
28+
return reset_message_by_field(field, serializers.CharField)
2329

2430
@staticmethod
2531
def uuid(field: str):
26-
return {'required': gettext_lazy('【%s】此字段必填。' % field),
27-
'null': gettext_lazy('【%s】此字段不能为null。' % field),
28-
'invalid': gettext_lazy("【%s】必须是有效的UUID。" % field),
29-
}
32+
return reset_messages(field, serializers.UUIDField.default_error_messages)
3033

3134
@staticmethod
3235
def integer(field: str):
33-
return {'invalid': gettext_lazy('【%s】必须是有效的integer。' % field),
34-
'max_value': gettext_lazy('【%s】请确保此值小于或等于 {max_value} 。' % field),
35-
'min_value': gettext_lazy('【%s】请确保此值大于或等于 {min_value} 。' % field),
36-
'max_string_length': gettext_lazy('【%s】字符串值太大。') % field,
37-
'required': gettext_lazy('【%s】此字段必填。' % field),
38-
'null': gettext_lazy('【%s】此字段不能为null。' % field),
39-
}
36+
return reset_messages(field, serializers.IntegerField.default_error_messages)
4037

4138
@staticmethod
4239
def list(field: str):
43-
return {'not_a_list': gettext_lazy('【%s】应为列表,但得到的类型为 "{input_type}".' % field),
44-
'empty': gettext_lazy('【%s】此列表不能为空。' % field),
45-
'min_length': gettext_lazy('【%s】请确保此字段至少包含 {min_length} 个元素。' % field),
46-
'max_length': gettext_lazy('【%s】请确保此字段的元素不超过 {max_length} 个。' % field),
47-
'required': gettext_lazy('【%s】此字段必填。' % field),
48-
'null': gettext_lazy('【%s】此字段不能为null。' % field),
49-
}
40+
return reset_messages(field, serializers.ListField.default_error_messages)
5041

5142
@staticmethod
5243
def boolean(field: str):
53-
return {'invalid': gettext_lazy('【%s】必须是有效的布尔值。' % field),
54-
'required': gettext_lazy('【%s】此字段必填。' % field),
55-
'null': gettext_lazy('【%s】此字段不能为null。' % field)}
44+
return reset_messages(field, serializers.BooleanField.default_error_messages)
5645

5746
@staticmethod
5847
def dict(field: str):
59-
return {'not_a_dict': gettext_lazy('【%s】应为字典,但得到的类型为 "{input_type}' % field),
60-
'empty': gettext_lazy('【%s】能是空的。' % field),
61-
'required': gettext_lazy('【%s】此字段必填。' % field),
62-
'null': gettext_lazy('【%s】此字段不能为null。' % field),
63-
}
48+
return reset_messages(field, serializers.DictField.default_error_messages)
6449

6550
@staticmethod
6651
def float(field: str):
67-
return {'invalid': gettext_lazy('【%s】需要一个有效的数字。' % field),
68-
'max_value': gettext_lazy('【%s】请确保此值小于或等于 {max_value}。' % field),
69-
'min_value': gettext_lazy('【%s】请确保此值大于或等于 {min_value}。' % field),
70-
'max_string_length': gettext_lazy('【%s】字符串值太大。' % field),
71-
'required': gettext_lazy('【%s】此字段必填。' % field),
72-
'null': gettext_lazy('【%s】此字段不能为null。' % field),
73-
}
52+
return reset_messages(field, serializers.FloatField.default_error_messages)
7453

7554
@staticmethod
7655
def json(field: str):
77-
return {
78-
'invalid': gettext_lazy('【%s】值必须是有效的JSON。' % field),
79-
'required': gettext_lazy('【%s】此字段必填。' % field),
80-
'null': gettext_lazy('【%s】此字段不能为null。' % field),
81-
}
56+
return reset_messages(field, serializers.JSONField.default_error_messages)
8257

8358
@staticmethod
8459
def base(field: str):
85-
return {
86-
'required': gettext_lazy('【%s】此字段必填。' % field),
87-
'null': gettext_lazy('【%s】此字段不能为null。' % field),
88-
}
60+
return reset_messages(field, serializers.Field.default_error_messages)
8961

9062
@staticmethod
9163
def date(field: str):
92-
return {
93-
'required': gettext_lazy('【%s】此字段必填。' % field),
94-
'null': gettext_lazy('【%s】此字段不能为null。' % field),
95-
'invalid': gettext_lazy('【%s】日期格式错误,请改用以下格式之一: {format}。'),
96-
'datetime': gettext_lazy('【%s】应为日期,但得到的是日期时间。')
97-
}
64+
return reset_messages(field, serializers.DateField.default_error_messages)
9865

9966
@staticmethod
10067
def image(field: str):
101-
return {
102-
'required': gettext_lazy('【%s】此字段必填。' % field),
103-
'null': gettext_lazy('【%s】此字段不能为null。' % field),
104-
'invalid_image': gettext_lazy('您上载的【%s】文件不是图像或图像已损坏,请上载有效的图像。' % field),
105-
'max_length': gettext_lazy('【%s】请确保此文件名最多包含 {max_length} 个字符(长度为 {length})。' % field),
106-
'invalid': gettext_lazy('【%s】提交的数据不是文件,请检查表单上的编码类型。' % field)
107-
}
68+
return reset_messages(field, serializers.ImageField.default_error_messages)
10869

10970
@staticmethod
11071
def file(field: str):
111-
return {
112-
'required': gettext_lazy('【%s】此字段必填。' % field),
113-
'empty': gettext_lazy('【%s】提交的文件为空。' % field),
114-
'invalid': gettext_lazy('【%s】提交的数据不是文件,请检查表单上的编码类型。' % field),
115-
'no_name': gettext_lazy('【%s】无法确定任何文件名。' % field),
116-
'max_length': gettext_lazy('【%s】请确保此文件名最多包含 {max_length} 个字符(长度为 {length})。' % field)
117-
}
72+
return reset_messages(field, serializers.FileField.default_error_messages)
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#: .\apps\application\serializers\application_serializers.py:196
2+
msgid "application name"
3+
msgstr ""
4+
5+
#: .\apps\application\serializers\application_serializers.py:199
6+
msgid "application describe"
7+
msgstr ""
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#: .\apps\application\serializers\application_serializers.py:196
2+
msgid "application name"
3+
msgstr "应用名称"
4+
5+
#: .\apps\application\serializers\application_serializers.py:199
6+
msgid "application describe"
7+
msgstr "应用描述"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#: .\apps\application\serializers\application_serializers.py:196
2+
msgid "application name"
3+
msgstr "應用名稱"
4+
5+
#: .\apps\application\serializers\application_serializers.py:199
6+
msgid "application describe"
7+
msgstr "應用描述"

apps/smartdoc/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class Config(dict):
8282
"DB_PASSWORD": "Password123@postgres",
8383
"DB_ENGINE": "dj_db_conn_pool.backends.postgresql",
8484
"DB_MAX_OVERFLOW": 80,
85+
'LANGUAGE_CODE': 'en',
8586
# 向量模型
8687
"EMBEDDING_MODEL_NAME": "shibing624/text2vec-base-chinese",
8788
"EMBEDDING_DEVICE": "cpu",

apps/smartdoc/settings/base.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
]
5151

5252
MIDDLEWARE = [
53+
'django.middleware.locale.LocaleMiddleware',
5354
'django.middleware.security.SecurityMiddleware',
5455
'django.contrib.sessions.middleware.SessionMiddleware',
5556
'django.middleware.common.CommonMiddleware',
@@ -172,13 +173,31 @@
172173
# Internationalization
173174
# https://docs.djangoproject.com/en/4.2/topics/i18n/
174175

175-
LANGUAGE_CODE = 'en-us'
176-
177176
TIME_ZONE = CONFIG.get_time_zone()
178177

178+
# 启用国际化
179179
USE_I18N = True
180180

181-
USE_TZ = False
181+
# 启用本地化
182+
USE_L10N = True
183+
184+
# 启用时区
185+
USE_TZ = True
186+
187+
# 默认语言
188+
LANGUAGE_CODE = CONFIG.get("LANGUAGE_CODE")
189+
190+
# 支持的语言
191+
LANGUAGES = [
192+
('en', 'English'),
193+
('zh', '中文简体'),
194+
('zh-hant', '中文繁体')
195+
]
196+
197+
# 翻译文件路径
198+
LOCALE_PATHS = [
199+
os.path.join(BASE_DIR.parent, 'locales')
200+
]
182201

183202
# Static files (CSS, JavaScript, Images)
184203
# https://docs.djangoproject.com/en/4.2/howto/static-files/

0 commit comments

Comments
 (0)