I’m quite new to Django and I have a beginner question that I need some help with if that’s okay. I would like to build a site that is available in two languages, in this case English and Japanese, and I would like to have a single button for switching between languages.
The following code works fine for displaying two buttons, one for each language.
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<form action="{% url 'set_language' %}" method="post" id="form_{{ language.code }}" >
{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}" />
<input name="language" type="hidden" value="{{ language.code }}" />
</form>
<button class="btn btn-light" type="submit" form="form_{{ language.code }}" value="Submit">{{ language.name_local }}</button>
{% endfor %}
I tried to adapt this to use a simple if/else condition in the following way to show just one button that would make it possible to switch to the other language. However, I am getting an error stating:
Invalid block tag on line 67: ‘LANGUAGE_CODE’, expected ‘elif’, ‘else’ or ‘endif’. Did you forget to register or load this tag?
which refers to the line stating "{% LANGUAGE_CODE = ‘ja’ %}.
{% get_current_language as LANGUAGE_CODE %}
{% if LANGUAGE_CODE == 'en' %}
{% LANGUAGE_CODE = 'ja' %}
{% else %}
{% LANGUAGE_CODE = 'en' %}
{% endif %}
{% get_language_info for LANGUAGE_CODE as language %}
<form action="{% url 'set_language' %}" method="post" id="form_{{ language.code }}" >
{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}" />
<input name="language" type="hidden" value="{{ language.code }}" />
</form>
<button class="btn btn-light" type="submit" form="form_{{ language.code }}" value="Submit">{{ language.name_local }}</button>
Would someone mind giving me some help with this error? And also, is my attempt here a good approach for what I’m trying to do or is there a better approach?
Many thanks in advance