为什么本地化在Django 1.11上无法正常工作?

时间:2019-05-13 14:07:49

标签: django django-models

我将应用程序的名称转换为Russian。但是我不明白为什么有些应用程序会被翻译,而另一些却没有。这是一个例子。

这不起作用:

#: donation/apps.py:9
msgid "Donation"
msgstr "Донат"

这有效:

#: reputation/apps.py:9
msgid "Reputation"
msgstr "Репутация"

捐赠/应用

from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _

from django.apps import AppConfig


class DonationConfig(AppConfig):
    name = 'donation'
    verbose_name = _('Donation')

信誉/应用

from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _

from django.apps import AppConfig


class ReputationConfig(AppConfig):
    name = 'reputation'
    verbose_name = _('Reputation')

设置

# Internationalization
LANGUAGE_CODE = 'ru'

USE_I18N = True

USE_L10N = False

LANGUAGES = (
    ## Customize this
    ('ru', gettext('Russian')),
)

LOCALE_PATHS = (
    path('locale'),
)

模板

<div class="app-donation module">
        <table>
        <caption>
            <a href="/admin/donation/" class="section" title="Модели в приложении Donation">Donation</a>
        </caption>

            <tr class="model-projectfriend">

                <th scope="row"><a href="/admin/donation/projectfriend/">Project Friends</a></th>



                <td><a href="/admin/donation/projectfriend/add/" class="addlink">Добавить</a></td>



                <td><a href="/admin/donation/projectfriend/" class="changelink">Изменить</a></td>

            </tr>

            <tr class="model-projectrelease">

                <th scope="row"><a href="/admin/donation/projectrelease/">Project Releases</a></th>



                <td><a href="/admin/donation/projectrelease/add/" class="addlink">Добавить</a></td>



                <td><a href="/admin/donation/projectrelease/" class="changelink">Изменить</a></td>

            </tr>

            <tr class="model-projectdonation">

                <th scope="row"><a href="/admin/donation/projectdonation/">Project donations</a></th>



                <td><a href="/admin/donation/projectdonation/add/" class="addlink">Добавить</a></td>



                <td><a href="/admin/donation/projectdonation/" class="changelink">Изменить</a></td>

            </tr>

            <tr class="model-releasetask">

                <th scope="row"><a href="/admin/donation/releasetask/">Release Tasks</a></th>



                <td><a href="/admin/donation/releasetask/add/" class="addlink">Добавить</a></td>



                <td><a href="/admin/donation/releasetask/" class="changelink">Изменить</a></td>

            </tr>

        </table>
        </div>

enter image description here

2 个答案:

答案 0 :(得分:1)

修复 0 0 1 以指向实际路径。这不是用于网址的path,而是实际路径,例如LOCALE_PATHS

我在项目中使用的是:

"/home/user/path/to/locale/dir"

以上内容声明在我的# settings.py def here(*path): return join(abspath(dirname(__file__)), *path) PROJECT_ROOT = here("..") # adjust here if your settings.py is nested more def root_is(*path): return join(abspath(PROJECT_ROOT), *path) ROOT = root_is # ROOT("") == project root 文件的顶部。然后,如果要引用目录路径(例如settings.py),请执行以下操作:

LOCALE_PATHS

如果要使用Django默认使用的常规格式(LOCALE_PATHS = ( ROOT('path/to/locale'), ) ),则可以使用:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

请确保首先LOCALE_PATHS = ( ROOT(os.path.join(BASE_DIR, "locale")), ) 这些变量,以确保路径正确。

答案 1 :(得分:0)

该应用(Donation)尚未在此应用的__init__文件中定义。

default_app_config = 'donation.apps.DonationConfig'