i18n_patterns and set_language

Hey, first time poster here.

I’m using the set_language helper function and ran into a small issue. I found that the language would switch from the default language to some other language, but I couldn’t switch back to the default language.

There are two ways to fix the problem. The first way was to set prefix_default_language=True in i18n_patterns. The second way was to slightly modify set_language as so:

def set_language(request):
    # next_url setup at the beginning

    # Strip out the language code from the url before translating the url.
    for lang_code, lang_name in settings.LANGUAGES:
        prefix = '/' + lang_code + '/'
        if next_url.startswith(prefix):
            next_url = next_url[len(prefix) - 1:]
            break
...

This makes set_language work even if the prefix_default_language in i18n_patterns is set to False.

Is there a good reason to keep prefix_default_language set to True, besides SEO?

Hmm, doing a little digging, it seems like the above modification to set_language shouldn’t be necessary. Maybe I should ask a different question:

Is set_language/translate_url suppose to work even if prefix_default_language=False is set in i18n_patterns? Because it doesn’t work for me.

1 Like