fix: The applet list is not translated.

This commit is contained in:
wangruidong
2025-07-24 17:28:19 +08:00
committed by 老广
parent 36ae076cb0
commit 26cea550c4
2 changed files with 33 additions and 40 deletions

View File

@@ -1,8 +1,11 @@
from django.utils.translation import gettext_lazy as _
import os
from django.utils.translation import gettext_lazy as _, get_language
from rest_framework import serializers
from common.const.choices import Status
from common.serializers.fields import ObjectRelatedField, LabeledChoiceField
from common.utils.yml import yaml_load_with_i18n
from terminal.const import PublishStatus
from ..models import Applet, AppletPublication, AppletHost
@@ -42,3 +45,31 @@ class AppletSerializer(serializers.ModelSerializer):
'version', 'author', 'type', 'edition',
'can_concurrent', 'protocols', 'tags', 'comment',
] + read_only_fields
@staticmethod
def read_manifest_with_i18n(obj, lang='zh'):
path = os.path.join(obj.path, 'manifest.yml')
if os.path.exists(path):
with open(path, encoding='utf8') as f:
manifest = yaml_load_with_i18n(f, lang)
else:
manifest = {}
return manifest
@staticmethod
def readme(obj, lang=''):
lang = lang[:2]
readme_file = os.path.join(obj.path, f'README_{lang.upper()}.md')
if os.path.isfile(readme_file):
with open(readme_file, 'r') as f:
return f.read()
return ''
def to_representation(self, instance):
data = super().to_representation(instance)
lang = get_language()
manifest = self.read_manifest_with_i18n(instance, lang)
data['display_name'] = manifest.get('display_name', instance.display_name)
data['comment'] = manifest.get('comment', instance.comment)
data['readme'] = self.readme(instance, lang)
return data