#32737: get_language_info replaces requested language code with the one found in
django.conf.locale.LANG_INFO
-------------------------------------+-------------------------------------
               Reporter:  ruffni     |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:             |        Version:  3.1
  Internationalization               |       Keywords:  i18n,
               Severity:  Normal     |  get_language_info, language code,
           Triage Stage:             |  LANG_INFO, documentation
  Unreviewed                         |      Has patch:  0
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 == 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

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32737>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.17f83749741b2f906a288551fa942500%40djangoproject.com.

Reply via email to