From 772c9b385cf47e5acfce3e1a03d07e62ec48f917 Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 28 Jun 2024 16:36:34 +0800 Subject: [PATCH] perf: lang setting from core --- apps/authentication/views/login.py | 3 ++- apps/common/const/choices.py | 1 - apps/settings/api/i18n.py | 16 ++++++++++++---- apps/settings/api/public.py | 11 +++++++++-- apps/settings/serializers/public.py | 1 + apps/users/serializers/preference/lina.py | 2 +- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/apps/authentication/views/login.py b/apps/authentication/views/login.py index 904e6cecd..99131c4fb 100644 --- a/apps/authentication/views/login.py +++ b/apps/authentication/views/login.py @@ -126,7 +126,8 @@ class UserLoginContextMixin: def get_current_lang(self): 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]) @staticmethod diff --git a/apps/common/const/choices.py b/apps/common/const/choices.py index ef898f54f..aa30b1d1d 100644 --- a/apps/common/const/choices.py +++ b/apps/common/const/choices.py @@ -22,7 +22,6 @@ class Status(models.TextChoices): class Language(models.TextChoices): - auto = 'auto', _('Auto') en = 'en', 'English' zh_hans = 'zh-hans', '中文(简体)' zh_hant = 'zh-hant', '中文(繁體)' diff --git a/apps/settings/api/i18n.py b/apps/settings/api/i18n.py index fffa4bb56..15615bee5 100644 --- a/apps/settings/api/i18n.py +++ b/apps/settings/api/i18n.py @@ -11,12 +11,13 @@ from rest_framework.response import Response class ComponentI18nApi(RetrieveAPIView): base_path = 'locale' permission_classes = [AllowAny] + lang_data = {} - def retrieve(self, request, *args, **kwargs): - name = kwargs.get('name') - component_dir = safe_join(settings.APPS_DIR, 'i18n', name) - lang = request.query_params.get('lang') + def get_component_translations(self, name): + if name in self.lang_data: + return self.lang_data[name] + component_dir = safe_join(settings.APPS_DIR, 'i18n', name) if os.path.exists(component_dir): files = os.listdir(component_dir) else: @@ -28,6 +29,13 @@ class ComponentI18nApi(RetrieveAPIView): _lang = file.split('.')[0] with open(safe_join(component_dir, file), 'r') as 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: data = data.get(lang) or {} diff --git a/apps/settings/api/public.py b/apps/settings/api/public.py index 9808ea1c6..804ac470b 100644 --- a/apps/settings/api/public.py +++ b/apps/settings/api/public.py @@ -3,7 +3,7 @@ from rest_framework import generics from rest_framework.permissions import AllowAny 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.timezone import local_now from .. import serializers @@ -26,7 +26,14 @@ class OpenPublicSettingApi(generics.RetrieveAPIView): return { "XPACK_ENABLED": settings.XPACK_ENABLED, "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 + ] } diff --git a/apps/settings/serializers/public.py b/apps/settings/serializers/public.py index b1c099aab..8c0031b29 100644 --- a/apps/settings/serializers/public.py +++ b/apps/settings/serializers/public.py @@ -12,6 +12,7 @@ class PublicSettingSerializer(serializers.Serializer): XPACK_ENABLED = serializers.BooleanField() INTERFACE = serializers.DictField() COUNTRY_CALLING_CODES = serializers.ListField() + LANGUAGES = serializers.ListField() class PrivateSettingSerializer(PublicSettingSerializer): diff --git a/apps/users/serializers/preference/lina.py b/apps/users/serializers/preference/lina.py index c318fdc2d..8c2b906df 100644 --- a/apps/users/serializers/preference/lina.py +++ b/apps/users/serializers/preference/lina.py @@ -44,7 +44,7 @@ class FileEncryptSerializer(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):