Django gettext 不起作用

Django gettext doesn't works

我尝试以多种语言为我的 django-cms 网站提供服务

makemessagescompilemessages 很有魅力,但是当我 runserver 时,我的字符串保持不翻译

我使用的 python 个文件 ugettext_lazy:

from cms.toolbar_pool import toolbar_pool
from cms.extensions.toolbar import ExtensionToolbar
from django.utils.translation import ugettext_lazy as _
from .models import CategoryExtension


@toolbar_pool.register
class CategoryExtensionToolbar(ExtensionToolbar):
    # defines the model for the current toolbar
    model = CategoryExtension

    def populate(self):
        # setup the extension toolbar with permissions and sanity checks
        current_page_menu = self._setup_extension_toolbar()

        # if it's all ok
        if current_page_menu:
            # retrieves the instance of the current extension (if any) and the toolbar item URL
            page_extension, url = self.get_page_extension_admin()
            if url:
                # adds a toolbar item in position 0 (at the top of the menu)
                current_page_menu.add_modal_item(_('Page Category'), url=url,
                                                 disabled=not self.toolbar.edit_mode, position=0)

我的翻译相关设置:

def gettext(s):
    return s


LOCALE_PATHS = (
    BASE_DIR + "/locale",
)

LANGUAGE_CODE = 'fr-fr'

TIME_ZONE = 'Europe/Paris'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LANGUAGES = (
    ('fr-fr', gettext('fr-fr')),
)

CMS_LANGUAGES = {
    1: [
        {
            'code': 'fr-fr',
            'name': gettext('fr-fr'),
            'redirect_on_fallback': True,
            'public': True,
            'hide_untranslated': False,
        },
    ],
    'default': {
        'redirect_on_fallback': True,
        'public': True,
        'hide_untranslated': False,
    },
}

还有我的 po 文件(在 PROJECT_ROOT/locale/fr-fr/LC_MESSAGES/django.po):

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE 
# package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-06-06 19:11+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#: myapp/cms_toolbars.py:22
msgid "Page Category"
msgstr "Catégorie de Page"

#: myapp/configuration/cms_settings.py:6
msgid "Page"
msgstr "Page"

#: myapp/configuration/cms_settings.py:7
msgid "Page with instagram pictures"
msgstr "Page avec bandeau instagram"

#: myapp/configuration/cms_settings.py:8
msgid "Page who list children"
msgstr "Page qui liste ses enfants"

#: myapp/configuration/i18n_settings.py:25
#: myapp/configuration/i18n_settings.py:32
msgid "fr-fr"
msgstr "fr-fr"

我认为您的 LANGUAGES 设置不正确,请试试这个:

LANGUAGES = [
    ('fr', gettext('French')),
]