﻿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
32737	get_language_info replaces requested language code with the one found in django.conf.locale.LANG_INFO	ruffni	nobody	"== Issue

while following the instructions at 
[https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#the-set-language-redirect-view  The set_language redirect view] i tried the following:
{{{ 
# settings.py
LANGUAGE_CODE = 'de-ch'                                                                                                                                                                                             
LANGUAGES = [('de-ch', _('Swiss German')),                                                                                                                                                                          
             ('fr-ch', _('Swiss French')),                                                                                                                                                                          
             ('en', _('English'))]
​
# some_template.html
          <form id=""set_lang"" action=""{% url 'set_language' %}"" method=""post"">                                                                                                                                      
            {% csrf_token %}                                                                                                                                                                                        
            <input name=""next"" type=""hidden"" />                                                                                                                                                                     
            <li class=""nav-item input-group"">                                                                                                                                                                       
              <select name=""language"" class=""custom-select"" id=""lang_selection"">                                                                                                                                    
                {% get_current_language as LANGUAGE_CODE %}                                                                                                                                                         
                {% get_available_languages as LANGUAGES %}                                                                                                                                                          
                {% get_language_info_list for LANGUAGES as languages %}                                                                                                                                             
                {% for lang in languages %}                                                                                                                                                                         
                <option value=""{{ lang.code }}""                                                                                                                                                                     
                               {% if lang.code == LANGUAGE_CODE %} selected{% endif%}>                                                                                                                              
                  {{ lang.name_local }} ({{lang.code}})</option>                                                                                                                                                    
                {% endfor %}                                                                                                                                                                                        
              </select>                                                                                                                                                                                             
              <div class=""input-group-append"">                                                                                                                                                                      
                <button class=""btn btn-outline-secondary"" type=""submit"" id=""set_lang_btn"">                                                                                                                          
                  {% translate ""Select language"" %}</button>                                                                                                                                                        
              </div>                                                                                                                                                                                                
            </li>                                                                                                                                                                                                   
          </form>  
}}}

my widget failed to recognize the currently selected language. not sure if i missed something or the documentation doesn't (yet) cover this particular use-case.


== Context
the variables set in the template code above are set as follows:
{{{
LANGUAGE_CODE = 'fr-ch'

LANGUAGES = [('de-ch', 'allemand suisse'), ('fr-ch', 'fran&ccirc;ais'), ('en', 'anglais')]

languages = [{'bidi': False, 'code': 'de', 'name': 'German', 'name_local': 'Deutsch', 'name_translated': 'Allemand'}, {'bidi': False, 'code': 'fr', 'name': 'French', 'name_local': 'français', 'name_translated': 'Français'}, {'bidi': False, 'code': 'en', 'name': 'English', 'name_local': 'English', 'name_translated': 'anglais'}] 
}}}
switching languages and i18n in general work just fine.


== Analysis
testing against 'fr-ch' fails because after applying get_language_info_list() to the list of available languages (LANGUAGES) lang.code is the code from the base language found in django.conf.locale.LANG_INFO ('fr' in this case) and not necessarily the code being requested ('fr-ch').

not sure if i'm missing something from the documentation, or if this is intended behavior.

imho getting the information for the base language is fine, but having the option of (easily) checking the currently selected language is necessary.


== Test
{{{
from django.utils.translation import get_language_info

lang_code = 'fr-ch'
info = get_language_info(lang_code)
assert lang_code == info['code']
}}}

== Possible Solutions
=== a) replace the code in the retrieved data to match the requested code
i wasn't able to test this but i guess one could simply add the requested code to the returned LANG_INFO[lang_code] object, i.e. by adding the line:
{{{
info['code'] = lang_code[0]
}}}
towards the end of django.utils.translation.get_language_info. not sure if this is desired behavior though
=== b) add documentation on how this particular use-case should be handled"	Cleanup/optimization	new	Internationalization	dev	Normal		i18n, get_language_info, language code, LANG_INFO, documentation		Accepted	0	0	0	0	0	0
