I’m trying to change my language from french (the default language of my web navigator and my app) to the English language.
LANGUAGES= [
('fr', 'French'),
('en', 'English'),
]
Here is my form to change the language, I found it on the documentation
<form action="{% url 'set_language' %}" method ="post"> {% csrf_token %}
<input type="hidden" name="next" value ="{{ redirect_to }}">
<select name="language" id="">
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages%}
<option value="{{ langauge.code }}" {% if language.code == LANGUAGE_CODE %} selected {% endif %}>
{{language.name_local}} ({{language.code}})
</option>
{% endfor %}
</select>
<input type = "submit" value="Go">
</form>
Here is my urls pattern
urlpatterns = [
path('i18n/', include('django.conf.urls.i18n')),
path('mission/',include('mission.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
url(r'^favicon\.ico$', RedirectView.as_view(url='/static/images/favicon.ico'))
]
at my views.py code I add this
def index(request):
from django.utils import translation
#user_language = 'en'
#translation.activate(user_language)
#request.session[translation.LANGUAGE_SESSION_KEY] = user_language
if translation.LANGUAGE_SESSION_KEY in request.session:
del request.session[translation.LANGUAGE_SESSION_KEY]
return render(request, 'mission/index.html', {})
But when I try this in my views.py code to change the language manually it works
user_language = 'en'
translation.activate(user_language)
request.session[translation.LANGUAGE_SESSION_KEY] = user_language
I think the problem is on the function set_langauge
It is predefined on a file named i18n.py but I’m not sure