Reputation: 173
I'm facing a weird situation when trying to change language after appending the language code to the url
example -- domain.pt/en
If I visit the site normally throught the domain without the language code appended, it works perfectly, I'm able to change language without problem.
If somehow I copy the link and paste it on the browser, the language switcher stops working.
example -- domain.pt
I have translated a few Django websites so far and found out this happens on all of them.
#urls.py
urlpatterns = [
path('i18n', include('django.conf.urls.i18n')),
path('admin/', admin.site.urls),
]
urlpatterns += i18n_patterns(
path('', include('base.urls')),
prefix_default_language=True,
)
template
<form id="lang-switcher" action="{% url 'set_language' %}" method="post">{% csrf_token %}
<input type="hidden" name="next" value"{{ redirect_to }}">
<select class="language_selector" name="language" id="" onchange="this.form.submit()">
{% 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.code }}
</option>
{% endfor %}
</select>
</form>
settings.py
LANGUAGE_CODE = 'pt'
TIME_ZONE = 'Europe/Lisbon'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = [
BASE_DIR / 'locale',
]
LANGUAGES = [
('pt', 'Português'),
('en', 'Inglês'),
('es', 'Espanhol'),
('fr', 'Francês'),
('de', 'Alemão'),
]
A real-world example of this issue would be like, copying the link, sharing it with someone and the person not being able to change the language of the website.
Thanks in advance!
Upvotes: 0
Views: 259
Reputation: 1
Move the locale midleware after de session middleware on settings.
Upvotes: 0