Repository: wagtail/wagtail
File: wagtail/admin/views/generic/models.py (Python)

Code before:
        foreign_field_name = ""
        for model in relations:
            foreign_field = model_class._meta.get_field(model)
            foreign_field_name = foreign_field.verbose_name
            model_class = foreign_field.related_model

        label, attr = label_for_field(field, model_class, return_attr=True)

        # For some languages, it may be more appropriate to put the field label
        # before the related model name
        if foreign_field_name:
            label = _("%(related_model_name)s %(field_label)s") % {
                "related_model_name": foreign_field_name,
                "field_label": label,
            }

        sort_key = getattr(attr, "admin_order_field", None)

        # attr is None if the field is an actual database field,
        # so it's possible to sort by it
        if attr is None:
            sort_key = field_name

        accessor = field_name
        # Build the dotted relation if needed, for use in multigetattr
        if relations:
            accessor = ".".join(lookups)
        return column_class(
            accessor,
            label=capfirst(label),
            sort_key=sort_key,
            **kwargs,
        )

    @cached_property
    def columns(self) -> list[Column]:
        columns = []
        for i, field in enumerate(self.list_display):
            if isinstance(field, Column):
                column = field
            elif i == 0:
                column = self._get_title_column(field)
            else:
                column = self._get_custom_column(field)

            if isinstance(column, LocaleColumn) and not self.i18n_enabled:
                continue

            columns.append(column)

        return columns

Review comment (performance):
Not super happy with this, but we can't initialise `list_display` on init nor make it a (cached) property, because `ModelViewSet` / `SnippetViewSet` fetch `list_display` directly of the index view class, rather than its instance. And `i18n_enabled` checks both the setting and the fact that the given model is translatable

Code after:
            **kwargs,
        )

    @cached_property
    def list_display(self):
        list_display = ["__str__", UpdatedAtColumn()]
        if self.i18n_enabled:
            list_display.insert(1, LocaleColumn())
        return list_display

    @cached_property
    def columns(self):
        # If not explicitly overridden, derive from list_display
        columns = []
        for i, field in enumerate(self.list_display):
            if isinstance(field, Column):
                column = field
            elif i == 0:
                column = self._get_title_column(field)
            else:
                column = self._get_custom_column(field)
            columns.append(column)

        return columns

    def get_edit_url(self, instance):
        if self.edit_url_name and self.user_has_permission("change"):
            return reverse(self.edit_url_name, args=(quote(instance.pk),))

    def get_copy_url(self, instance):
        if self.copy_url_name and self.user_has_permission("add"):
            return reverse(self.copy_url_name, args=(quote(instance.pk),))

    def get_inspect_url(self, instance):
        if self.inspect_url_name and self.user_has_any_permission(
            {"add", "change", "delete", "view"}
        ):
            return reverse(self.inspect_url_name, args=(quote(instance.pk),))

    def get_delete_url(self, instance):
        if self.delete_url_name and self.user_has_permission("delete"):
            return reverse(self.delete_url_name, args=(quote(instance.pk),))

    def get_add_url(self):
        if self.add_url_name and self.user_has_permission("add"):
            return self._set_locale_query_param(reverse(self.add_url_name))

    @cached_property
    def add_url(self):
        return self.get_add_url()