Tuesday, April 28, 2020

i18n: Miscellaneous as dropdown-menu

Dear django user,

could you please help to the webpage newbie with BFU html skills? :-)

I am building a multi-lingual webpage. I have read the documentation, have seen severel youtube videos and was able to setup the language change, including the "Misscellaneous" language changing button. 

{% load i18n %}

<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
   
<input name="next" type="hidden" value="{{ redirect_to }}">
   
<select name="language">
       
{% get_current_language as LANGUAGE_CODE %}
       
{% get_available_languages as LANGUAGES %}
       
{% get_language_info_list for LANGUAGES as languages %}
       
{% for language in languages %}
           
<option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
               
{{ language.name_local }} ({{ language.code }})
           
</option>
       
{% endfor %}
   
</select>
   
<input type="submit" value="Go">
</form>

This works fine and I have not identified any error with the above mentioned code. Nevertheles, I would prefer implement it as working dropdown-menu at my webpage for the language change (to have it more consistent with other dropdown menus in my bar).

As I mentioned, I am a newbie and I have tried something like:
 
{% load i18n %}

<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownBlog" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" name="next" type="hidden" value="{{ redirect_to }}">
        Language
    </a>
    <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog" name="language">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
            <option type="submit" value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
                {{ language.name_local }} ({{ language.code }})
            </option>
        {% endfor %}
    </div>
</form>

It draw the dropdown menu. In the list, there are written languages which I expect with  the appearance which I expect. But it do nothing, any webpage change or any error that the webpage is broken.

Could you please try to help me to identify what is wrong and how to make it work? It started to be behing my imaginary... :-(

Most ideal would be, if the text "Language" at line 5 would be changed as default at the actual set language code.

If it would be helpful, here is my project urls.py:

from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from django.conf.urls.i18n import i18n_patterns

urlpatterns = [
    path('i18n/', include('django.conf.urls.i18n')),
]

urlpatterns += i18n_patterns(
    path('admin/', admin.site.urls),
    path('',include('mainapp.urls')),
)

And here is my related settings.py:

ANGUAGE_CODE = 'en-us'

TIME_ZONE = 'US/Central'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LANGUAGES = [
    ('en', 'English'),
    ('cs', 'Czech'),
    ('es', 'Spanish')
]

LOCALE_PATHS = (
    os.path.join(BASE_DIR, "locale"),
    os.path.join(BASE_DIR, "mainapp/locale"),
)

Many thanks for any feedback,
Rene

PS: I have also found two different threads about it at the https://stackoverflow.com/, but it seems to me they are not using i18n settings recommended by basic django documentation.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/db7cb025-21c7-42cc-8f97-3c1dc17a4e34%40googlegroups.com.

No comments:

Post a Comment