Menú de navegación traducido de Wagtail

Al manejar contenido multilingüe en su sitio web, es posible que su menú de navegación no se muestre en el idioma local.

C05348A3-9AB8-42C9-A6E0-81DB3AC59FEB
           

Recientemente me encontré con un problema al usar el complemento Wagtail Localize para traducir contenido a varios idiomas. Las páginas se tradujeron bien, pero el menú de navegación superior siempre se mantuvo en el idioma predeterminado.

Después de investigar ese problema durante demasiado tiempo, finalmente encontré una solución y quería documentarla en caso de que alguien más tuviera el mismo problema.

Este era el contenido de base/templatetags/navigation_tags.py antes:

from django import template
from wagtail.core.models import Page, Site
from mybase.base.models import FooterText
 
 
register = template.Library()
# https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/
 
 
@register.simple_tag(takes_context=True)
def get_site_root(context):
    # This returns a core.Page. The main menu needs to have the site.root_page
    # defined else will return an object attribute error ('str' object has no
    # attribute 'get_children')
    return Site.find_for_request(context['request']).root_page
 
 
def has_menu_children(page):
    # This is used by the top_menu property
    # get_children is a Treebeard API thing
    # https://tabo.pe/projects/django-treebeard/docs/4.0.1/api.html
    return page.get_children().live().in_menu().exists()
 
...

Y este fue el contenido de templates/tags/top_menu.html:

{% load navigation_tags wagtailcore_tags %}
{% get_site_root as site_root %}
 
{% for menuitem in menuitems %}
  <li class="presentation {{ menuitem.title|lower|cut:" " }}{% if menuitem.active %} active{% endif %}{% if menuitem.show_dropdown %} has-submenu{% endif %}">
      {% if menuitem.show_dropdown %}
          <a href="{% pageurl menuitem %}" class="allow-toggle">{{ menuitem.title }} <span><a class="caret-custom dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"></a></span></a>
              {% top_menu_children parent=menuitem %}
              {# Used to display child menu items #}
      {% else %}
          <a href="{% pageurl menuitem %}" role="menuitem">{{ menuitem.title }}</a>
      {% endif %}
  </li>
{% endfor %}

Puede haber otras soluciones y, como de costumbre, la solución real depende en gran medida de su implementación particular de Wagtail, pero la solución para mí fue cambiar base/templatetags/navigation_tags.py para recuperar la página que coincidía con el idioma de la solicitud:

from django import template
from wagtail.models import Page, Site
from mybase.base.models import FooterText
from wagtail.core.models import Locale
 
 
register = template.Library()
# https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/
 
 
@register.simple_tag(takes_context=True)
def get_site_root(context):
    # This returns a core.Page. The main menu needs to have the site.root_page
    # defined else will return an object attribute error ('str' object has no
    # attribute 'get_children')
    request = context['request']
    language = request.LANGUAGE_CODE
    locale = Locale.objects.get(language_code=language)
    return Site.find_for_request(context['request']).root_page.get_translation(locale)
 
...
Comentarios publicados: 0

Tagged with:
Wagtail