1

I'm trying to implement a language switcher, for which I'm using the Django recommended form:

    <form action="{% url 'set_language' %}" method="post">{% csrf_token %}
        {% get_current_language as LANGUAGE_CODE %}
        <input name="next" type="hidden" value="{{ redirect_to }}">
        <input name="language" type="hidden" value="{% if LANGUAGE_CODE == 'en' %}es{% else %}en{% endif %}">
    </form>

My urls.py is set up like so:

urlpatterns = [

    # Wagtail urls
    re_path(r'^cms/', include(wagtailadmin_urls)),
    re_path(r'^documents/', include(wagtaildocs_urls)),

    # Django urls
    path('admin/', admin.site.urls),
    path('i18n/', include('django.conf.urls.i18n')),

]

urlpatterns += i18n_patterns(
    path(r'', include(wagtail_urls))
)

When I click to change my language, I am correctly forwarded to /en/slug or es/slug, depending on the language I have selected. However the actual slug value is not being translated. Since I have Spanish slugs for the Spanish pages, I am getting a 404 when I switch languages, because I am directed to the English slug value paired with the Spanish locale prefix (es).

I also tried using the slugurl_trans template tag, but that didn't seem to work (maybe because I'm not explicitly defining any URLs in my i18n_patterns call?).

Any guidance on this would be really helpful, as I've spent way too many hours on this!

  • Have you seen this in the Wagtail docs? https://docs.wagtail.io/en/latest/advanced_topics/i18n/duplicate_tree.html?highlight=translation – Dan Swain Aug 28 '19 at 19:44
  • @DanSwain Thanks for your reply! Unfortunately, I don't really think that's relevant, because it uses a duplicated page tree in order to implement language switching. Since I'm using `django-modeltranslation`, that should be provided out of the box, as far as I understand. With `django-modeltranslation`, if I access the `slug_es` link, the page renders using only the Spanish fields that I've set in the CMS. – user1843757 Aug 28 '19 at 20:15
  • Sure thing. Don't know if this might be useful, but I just came across it: https://github.com/springload/awesome-wagtail#translations. – Dan Swain Aug 29 '19 at 13:27

1 Answers1

0

It has been long but I'm going to post an answer anyway, the best way to get rid of the different slugs problem is by using django signals like this

@receiver(pre_save)
def set_translated_slug_on_new_instance(sender, instance, **kwargs):
    if isinstance(instance, Page):
        instance.slug_es = instance.slug_en
Miriam Arbaji
  • 319
  • 1
  • 3
  • 17