perf: lang setting from core

pull/13540/head
ibuler 5 months ago committed by 老广
parent f5053728e7
commit 772c9b385c

@ -126,7 +126,8 @@ class UserLoginContextMixin:
def get_current_lang(self): def get_current_lang(self):
langs = self.get_support_langs() langs = self.get_support_langs()
matched_lang = filter(lambda x: x['code'] == get_language(), langs) lang = get_language()
matched_lang = filter(lambda x: x['code'] == lang, langs)
return next(matched_lang, langs[0]) return next(matched_lang, langs[0])
@staticmethod @staticmethod

@ -22,7 +22,6 @@ class Status(models.TextChoices):
class Language(models.TextChoices): class Language(models.TextChoices):
auto = 'auto', _('Auto')
en = 'en', 'English' en = 'en', 'English'
zh_hans = 'zh-hans', '中文(简体)' zh_hans = 'zh-hans', '中文(简体)'
zh_hant = 'zh-hant', '中文(繁體)' zh_hant = 'zh-hant', '中文(繁體)'

@ -11,12 +11,13 @@ from rest_framework.response import Response
class ComponentI18nApi(RetrieveAPIView): class ComponentI18nApi(RetrieveAPIView):
base_path = 'locale' base_path = 'locale'
permission_classes = [AllowAny] permission_classes = [AllowAny]
lang_data = {}
def retrieve(self, request, *args, **kwargs): def get_component_translations(self, name):
name = kwargs.get('name') if name in self.lang_data:
component_dir = safe_join(settings.APPS_DIR, 'i18n', name) return self.lang_data[name]
lang = request.query_params.get('lang')
component_dir = safe_join(settings.APPS_DIR, 'i18n', name)
if os.path.exists(component_dir): if os.path.exists(component_dir):
files = os.listdir(component_dir) files = os.listdir(component_dir)
else: else:
@ -28,6 +29,13 @@ class ComponentI18nApi(RetrieveAPIView):
_lang = file.split('.')[0] _lang = file.split('.')[0]
with open(safe_join(component_dir, file), 'r') as f: with open(safe_join(component_dir, file), 'r') as f:
data[_lang] = json.load(f) data[_lang] = json.load(f)
self.lang_data[name] = data
return data
def retrieve(self, request, *args, **kwargs):
name = kwargs.get('name')
lang = request.query_params.get('lang')
data = self.get_component_translations(name)
if lang: if lang:
data = data.get(lang) or {} data = data.get(lang) or {}

@ -3,7 +3,7 @@ from rest_framework import generics
from rest_framework.permissions import AllowAny from rest_framework.permissions import AllowAny
from authentication.permissions import IsValidUserOrConnectionToken from authentication.permissions import IsValidUserOrConnectionToken
from common.const.choices import COUNTRY_CALLING_CODES from common.const.choices import COUNTRY_CALLING_CODES, Language
from common.utils import get_logger, lazyproperty from common.utils import get_logger, lazyproperty
from common.utils.timezone import local_now from common.utils.timezone import local_now
from .. import serializers from .. import serializers
@ -26,7 +26,14 @@ class OpenPublicSettingApi(generics.RetrieveAPIView):
return { return {
"XPACK_ENABLED": settings.XPACK_ENABLED, "XPACK_ENABLED": settings.XPACK_ENABLED,
"INTERFACE": self.interface_setting, "INTERFACE": self.interface_setting,
"COUNTRY_CALLING_CODES": COUNTRY_CALLING_CODES "COUNTRY_CALLING_CODES": COUNTRY_CALLING_CODES,
"LANGUAGES": [
{
'name': title,
'code': code
}
for code, title in Language.choices
]
} }

@ -12,6 +12,7 @@ class PublicSettingSerializer(serializers.Serializer):
XPACK_ENABLED = serializers.BooleanField() XPACK_ENABLED = serializers.BooleanField()
INTERFACE = serializers.DictField() INTERFACE = serializers.DictField()
COUNTRY_CALLING_CODES = serializers.ListField() COUNTRY_CALLING_CODES = serializers.ListField()
LANGUAGES = serializers.ListField()
class PrivateSettingSerializer(PublicSettingSerializer): class PrivateSettingSerializer(PublicSettingSerializer):

@ -44,7 +44,7 @@ class FileEncryptSerializer(serializers.Serializer):
class BasicSerializer(serializers.Serializer): class BasicSerializer(serializers.Serializer):
lang = serializers.ChoiceField(required=False, choices=Language.choices, label=_('Language'), default=Language.auto) lang = serializers.ChoiceField(required=False, choices=Language.choices, label=_('Language'), default=Language.en)
class LinaSerializer(serializers.Serializer): class LinaSerializer(serializers.Serializer):

Loading…
Cancel
Save