代码之家  ›  专栏  ›  技术社区  ›  pyprism

Django模板翻译未按预期工作

  •  0
  • pyprism  · 技术社区  · 6 年前

    我使用的是django 2.1,以下是与翻译相关的所有设置:

    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
    

    我要翻译的模板标记:

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

    我两个都跑 ./manage.py makemessages --all ./manage.py compilemessages 命令,还添加了.po文件中的翻译 makemessages 命令:

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

    en bn ,模板字符串仍然呈现默认的英语“About Us”。

    以下是我用于更改语言的所有代码:

    <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', '/'))
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   pyprism    6 年前

    愚蠢的错误!这本书有一个打字错误 LOCALE_PATH s 缺少,这里是正确的一个:

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