﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35034	In some cases i18n set_language does not change url language	Eric Soroos	nobody	"Related to #29281, but this time with repro

When posting to `/i18n/setlang/`:
 * with a referrer in a language that's not the current django cooke language like `/km/admin/foo/bar`, 
 * with a current language cookie of `django_language=en` 
 * and a post content of `next=&language=en`

`django.urls.translate_urls` attempts to resolve the url using the current request language (in this case, en) and fails, because the url is in a different language (km). 

`i18n.set_language` then returns the original referrer in the location header of the redirect, and the language doesn't apparently change, confusing the user.

The patch for `translate_urls` is approximately:

{{{
#!python
from django.utils.translation import override, check_for_language, get_language_from_path

...

def translate_url(url, lang_code):
    """"""                                                                                                                                                                            
    Given a URL (absolute or relative), try to get its translated version in                                                                                                       
    the `lang_code` language (either by i18n_patterns or by translated regex).                                                                                                     
    Return the original URL if no translated version is found.                                                                                                                     
    """"""
    parsed = urlsplit(url)
    try:
        # URL may be encoded.                                                                                                                                                      
        try:
            match = resolve(unquote(parsed.path))
        except Resolver404:
            url_lang_code = get_language_from_path(unquote(parsed.path))
            if url_lang_code and check_for_language(url_lang_code):
                with override(url_lang_code):
                    match = resolve(unquote(parsed.path))
            else:
                raise
    except Resolver404:
        pass
    else:
...
}}}

https://github.com/django/django/compare/main...EricSoroos:django:35034-translate-url"	Bug	new	Internationalization	4.2	Normal				Unreviewed	1	0	0	0	0	0
