Issue trying to change language from Django template

前端 未结 7 2129
悲&欢浪女
悲&欢浪女 2020-12-01 06:46

I need to include two buttons or links to allow users change language between English and Spanish. I\'ve read the docs and tried this:

相关标签:
7条回答
  • 2020-12-01 07:51

    If you only need two languages, ex. English and French and you've defined this in your settings.py and you have set the default language and you have configured urls.py in your main app correctly. Then, just use this in your template (or partial, topbar etc.) btn-kinito "btn-header are just styling classes you can manipulate that with css or JS.

    The loop or iteration inside is just looping through the LANGUAGES[] list, which you've defined in settings.py, then it creates a button. with the character "|" and a space   to make it look cute since we have just two langs.

    The {% url 'set_language' %} is Django's redirect view called set_language it redirects to URL. This is why in your main apps's urls.py you need to put path('i18n/', include('django.conf.urls.i18n')), In this case. So after the button is created for each language in the list you will be able to be redirected to that url.

      <div class="btn-header">
        <form action="{% url 'set_language' %}" method="post">
          {% csrf_token %}
          <input name="next" type="hidden" value="{{ redirect_to }}" />
            {% get_current_language as LANGUAGE_CODE %}
            {% get_available_languages as LANGUAGES %}
            {% get_language_info_list for LANGUAGES as languages %}
            {% for language in languages %}
              <button type="submit" name="language" value="{{ language.code }}"
                class="btn-kinito">
                {{ language.code }}
              </button>|&nbsp;
            {% endfor %}
        </form>
      </div>
    

    For urls.py I think it could look like this:

    from django.contrib import admin
    from django.urls import path, include
    from django.conf import settings
    from django.conf.urls.static import static
    from django.conf.urls.i18n import i18n_patterns
    
    
    # I don't want my admin translated
    urlpatterns = [
        path('admin/', admin.site.urls),
    ]
    
    urlpatterns += i18n_patterns (
        path('i18n/', include('django.conf.urls.i18n')),
        path('', include('pages.urls')),
        path('cats', include('cats.urls')),
        path('dogs', include('dogs.urls')),
        prefix_default_language=False,
     ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    The prefix_default_language=False, this is optional and it removes the default language prefix from the url, which makes sense if you just got two or three languages. Although I did run into problems with this in the past, where the prefix_default_language=False, did not work.

    How to fix the problem with prefix_default_language=False, NOT working, or not removing the default language prefix from urls

    In my settings.py I changed: LANGUAGE_CODE = 'en-us' to LANGUAGE_CODE = 'en'

    (seems to have solved it)

    0 讨论(0)
提交回复
热议问题