Browse Source

perf: lang setting from core

pull/13540/head
ibuler 5 months ago committed by 老广
parent
commit
772c9b385c
  1. 3
      apps/authentication/views/login.py
  2. 1
      apps/common/const/choices.py
  3. 16
      apps/settings/api/i18n.py
  4. 11
      apps/settings/api/public.py
  5. 1
      apps/settings/serializers/public.py
  6. 2
      apps/users/serializers/preference/lina.py

3
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

1
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', '中文(繁體)'

16
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 {}

11
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
]
}

1
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):

2
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):

Loading…
Cancel
Save