1

I am using django-tables2 with django-modeltranslation in one of my new projects. The site is using localization for english and french. If I render the table directly in html everything works fine, but django_tables2 seems to pour everything out, ignoring the localization request. Django-tables2 has a "localize/unlocalize" option for multilingual sites but it does not seem to work with django-modeltranslation. Here is my setup.

models.py

class DataSet(models.Model):
    name = models.CharField(max_length=255)
    source = models.CharField(max_length=255, null=True)
    data_type = models.CharField(max_length=255, null=True)

translation.py

@register(DataSet)
class DataSetTranslationOptions(TranslationOptions):
    fields = (
        "name",
        "source",
        "data_type",)

tables.py

class DataSetTable(tables.Table):
   name = tables.Column(order_by="name")

    class Meta:
        model = DataSet
        sequence = (
            "name",
            "source",
            "data_type",)
        unlocalize = ("id",)
        localize = (
           "name",
           "source",
           "data_type",)

datasets.html

{% load i18n %}
{% load render_table from django_tables2 %}

{% block content %}
    {% render_table table %}
{% endblock content %}

This table is rendered as follows:

name source data_type name_en name_fr source_en source_fr data_type_en data_type_fr
content content content content content content content content content
content content content content content content content content content

Please let me know what I am missing.

jcuot
  • 935
  • 9
  • 24
  • In the DataSetTable.Meta, have you tried specifying the columns based on what localization is being used? something like: `fields = ('name_en', 'source_en') if localization == "EN") else ('name_fr', 'source_fr')` – Ben Aug 24 '22 at 19:48

0 Answers0