Django不翻译过滤标签

时间:2018-05-28 20:49:56

标签: python html django translation

我在我的网站上使用django-filters进行过滤,并且除了两个页面之外,每个页面上的过滤标签都正确翻译。我检查了所有常见问题,例如确保将il8n加载到两个页面,测试locale文件夹的路径,重新启动服务器,检查中间件和放大器。其他设置,检查翻译文件中的模糊标签等。我可以想到的唯一相似之处是,当网站上的其他页面都没有时,它们都使用Django的内置Paginator。这足以打破翻译吗?我包含了两页中的一页的代码,希望有人可以告诉我发生了什么。如果有人想看到代码的其他部分github是here

models_FSJUser.py

pip install bs4

models_student.py

class FSJUser(models.Model):
    # List the languages a user may choose from in human readable format and also make them accessible in other files 
    # via FSJUser.LANG_CHOICES, FSJUser.ENGLISH, etc.
    FRENCH = 'fr'
    ENGLISH = 'en'
    LANG_CHOICES = (
        (FRENCH, "Fran"+u"\u00E7"+"ais"),
        (ENGLISH, 'English'),
    )   
    # Link FSJUser with a User model for authentication related business
    user = models.OneToOneField(User, on_delete = models.CASCADE, blank = True, null = True)    

    # All FSJ Users have these attributes in common
    ccid = models.CharField(max_length = 255, unique = True, verbose_name = _("CCID"))
    first_name = models.CharField(max_length = 255, verbose_name = _("First Name"))
    last_name = models.CharField(max_length = 255, verbose_name = _("Last Name"))
    email = models.EmailField(max_length = 254, verbose_name= _("Email"))
    lang_pref = models.CharField(max_length = 2, blank = False, choices = LANG_CHOICES, default = FRENCH, verbose_name = _("Language Preference"))

filters.py

# This class inherits from a standard FSJ User and extends for Student specific attributes and methods
class Student(FSJUser):
    program = models.ForeignKey(Program, on_delete = models.SET_NULL, null = True, blank = True, verbose_name = _("Program"))
    year = models.ForeignKey(YearOfStudy, on_delete=models.PROTECT, verbose_name = _("Year"))
    gpa = models.CharField(max_length = 10, null = True, blank = True, verbose_name = _("GPA"))
    middle_name = models.CharField(max_length = 50, blank = True, verbose_name = _("Middle Name"))
    student_id = models.CharField(max_length = 10, unique = True, verbose_name = _("U of A Student ID"), validators=[validate_student_id])    

views.py

from .models import *
import django_filters
from django.forms import CheckboxSelectMultiple, DateInput
from django.utils.translation import gettext_lazy as _

LOOKUP_TYPES = [
        ('icontains', _("contains"))
]

class StudentFilter(django_filters.FilterSet):
    ccid = django_filters.CharFilter(lookup_expr='icontains')
    first_name = django_filters.CharFilter(lookup_expr='icontains')
    middle_name = django_filters.CharFilter(lookup_expr='icontains')
    last_name = django_filters.CharFilter(lookup_expr='icontains')
    student_id = django_filters.CharFilter(lookup_expr='icontains')
    class Meta:
        model = Student
        fields = ['ccid','first_name','middle_name','last_name','student_id','year','program']

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,但我会在这里发布答案,以防其他人遇到同样的问题。

我没有找出问题的根源(虽然我现在怀疑是因为我定义了自定义过滤器而不是让它们自动生成);但是我发现效果最好的解决方案是在过滤器的 init 功能中单独定义每个过滤器的标签,如下所示:

    def __init__(self, *args, **kwargs):
        super(StudentFilter, self).__init__(*args, **kwargs)

        # Adding custom translatable labels to autogenerated filters
        self.filters['year'].label = _("Year:")

希望这有助于某人。