Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django dynamic localization not working as expected on mobile devices

I have question about show current language on mobile devices if I populate my language list in template dynamically (got from languages array from settings)…
So, this code working properly:

<a href="#" id="language-en" class="pure-drawer-link{% if LANGUAGE_CODE == 'en' %} active{% endif %}"> EN </a>

BUT, when I’m trying this code, I can’t achive that active class added to current language:

{% for lng in settings.LANGUAGES %}
                                    {% if not lng.0 == "ru" %}
                                        <a href="#" id="language-{{ lng.0 }}"
                                           class="pure-drawer-link{% if LANGUAGE_CODE == '{{ lng.0 }}' %} active{% endif %}">
                                            {{ lng.0|upper }}
                                        </a>
                                        {% if LANGUAGE_CODE == '{{ lng.0 }}' %} active {% else %} nonactive{% endif %} => this always return nonactive
                                    {% endif %}
                                {% endfor %}

Can anyone help to understood why this is happening?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

EDIT 1:

My middlewars in settings:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'my_app.middleware.ExpirationMiddleware',
    'my_app.middleware.IPMiddleware',
    'my_app.middleware.TranslationMiddleware',
   ]

and this for i18:

USE_I18N = True

USE_L10N = False

USE_TZ = True

TIME_ZONE = 'Europe/Zurich'

LANGUAGE_CODE = 'en-US'

>Solution :

  1. Make sure you have configured Django’s middleware and settings
    correctly. In order for dynamic localization to work, you need to
    have Django’s middleware installed and configured correctly. This
    includes the LocaleMiddleware, which is responsible for setting the
    language based on the user’s preferences. In addition, you need to
    have the USE_I18N and USE_L10N settings enabled in your Django
    settings file.

  2. Check the headers and settings of your mobile device. Some mobile
    devices, particularly older ones, may not send the correct headers
    or settings for dynamic localization to work. In particular, you
    may need to check the Accept-Language header, which is used by
    Django to determine the user’s preferred language. If this header
    is not set correctly, dynamic localization may not work as
    expected.

  3. Test on different mobile devices and browsers. It is possible that
    the issue you are experiencing is specific to a particular mobile
    device or browser. To rule out this possibility, try testing your
    Django app on a variety of mobile devices and browsers to see if the
    issue persists.

  4. Check for conflicts or issues with other middleware or settings. In
    some cases, dynamic localization may not work as expected if there
    are conflicts or issues with other middleware or settings in your
    Django app. For example, if you have multiple middleware classes
    that manipulate the Accept-Language header, this may cause
    conflicts and result in dynamic localization not working
    properly. It is worth checking for any potential conflicts or
    issues that may be impacting dynamic localization in your app.

It looks like you’re trying to compare the value of LANGUAGE_CODE with a string. In the Django template language, you can’t compare values like that. Instead, you need to use the compare template tag. Here’s how you could modify your code to make the comparison work:

{% for lng in settings.LANGUAGES %}
{% if not lng.0 == "ru" %}
<a href="#" id="language-{{ lng.0 }}"
     class="pure-drawer-link{% if LANGUAGE_CODE == lng.0 %} active{% endif %}">
{{ lng.0|upper }}
</a>
{% endif %}
{% endfor %}

Alternatively, you can use the equalto filter, which is provided by the Django framework, to compare the values of the two variables, like this:

{% for lng in settings.LANGUAGES %}
{% if not lng.0 == "ru" %}
<a href="#" id="language-{{ lng.0 }}"
     class="pure-drawer-link{% if LANGUAGE_CODE|equalto:lng.0 %} active{% endif %}">
{{ lng.0|upper }}
</a>
{% endif %}
{% endfor %}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading