简体   繁体   中英

Django template translation not working as expected

I am using django 2.1 , here is all the settings related to translation:

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
]

LANGUAGE_CODE = 'en'

LANGUAGES = (
    ('en', _('English')),
    ('bn', _('Bengali'))
)

LOCALE_PATH = (
    os.path.join(BASE_DIR, 'locale')
)

TIME_ZONE = 'Asia/Dhaka'

USE_I18N = True

USE_L10N = True

USE_TZ = False

Template tag that I want to translate:

{% load i18n %}
<div class="widget widget-about">
      <h4 class="widget-title">{% trans "About Us" %}</h4>
</div

I run both ./manage.py makemessages --all ./manage.py compilemessages commands , also added translation in .po file after makemessages command:

# locale/bn/LC_MESSAGES/django.po
#: templates/partials/footer.html:8
msgid "About Us"
msgstr "আমাদের সম্পর্কে"

When I changed the language code from en to bn , template string still rendering the default english "About Us".

Here are all the codes that I am using for changing language:

<div class="header-dropdown">
                        {% get_current_language as LANGUAGE_CODE %}
                        {% if LANGUAGE_CODE == 'en' %}
                            <a href="#"><img src="{% static 'image/flag/en.png' %}" alt="flag">ENGLISH</a>
                        {% else %}
                            <a href="#"><img src="{% static 'image/flag/bn.png' %}" alt="flag">বাংলা</a>
                        {% endif %}
                        <div class="header-menu">
                            <ul>
                                <li><a href="{% url 'change_language' %}?lan=en"><img src="{% static 'image/flag/en.png' %}" alt="USA Flag">ENGLISH</a></li>
                                <li><a href="{% url 'change_language' %}?lan=bn"><img src="{% static 'image/flag/bn.png' %}" alt="Bangladesh Flag">বাংলা</a></li>
                            </ul>
                        </div><!-- End .header-menu --> 

views.py

def change_lan(request):
    allowed_lan = ('en', 'bn')
    get_lan = request.GET.get('lan', 'en')
    if get_lan in allowed_lan:
        translation.activate(get_lan)
        request.session[translation.LANGUAGE_SESSION_KEY] = get_lan
        return redirect(request.META.get('HTTP_REFERER', '/'))
    else:
        return redirect(request.META.get('HTTP_REFERER', '/'))

Silly mistake ! There is a typo in LOCALE_PATH , s missing, here is correct one :

LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale')
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM