第三方应用程序的Django翻译

时间:2014-11-05 09:33:22

标签: python django django-1.7

我试图在我的Django 1.7项目中翻译Django第三方应用程序(django-recurrence)。 尽管我已经在这里读到了关于同样问题的所有答案,但我还是无法让Django为这个应用程序生成django.po。

这些是我目前的设置:

settings.py

LANGUAGE_CODE = 'it-IT'
gettext = lambda s: s
LANGUAGES = (
    ('en-us', gettext('English')),
    ('it-it', gettext('Italian')),
)

LOCALE_PATHS = (
    '/home/seether/.virtualenvs/mytime/lib/python2.7/site-packages/recurrence/locale',)

TIME_ZONE = 'Europe/Rome'

USE_I18N = True

USE_L10N = True

我尝试过多种方式修改LOCALE_PATHS,例如:

LOCALE_PATHS = (os.path.join(BASE_DIR,'locale-recurrence'))
LOCALE_PATHS = (os.path.join(BASE_DIR,'locale'))
...

等等。我已经从这个应用程序手动翻译了django.po,尝试将其复制到这些目录中,相应于我一次又一次尝试的设置,但它从未奏效。我尝试将LANGUAGES和LANGUAGE_CODE改为几乎所有可能的组合:' it',' it-it',' it_it','它-IT'和' it_IT'。也没用。

命令:

django-admin.py makemessages --all

只会为Django本身生成语言环境文件,完全忽略了我想翻译的应用程序。 我也尝试过使用django-rosetta,但我不能过分地加深这条道路,已经自己翻译了应用程序。基本上,我认为找到正确的方法来简单地告诉Django编译django.po我已经为django-recurrence编写并使用它应该就够了。

我在这里缺少什么?

4 个答案:

答案 0 :(得分:3)

我的回答是尝试汇总@catabaran提供的答案中描述的所有步骤:

# Make symlink to the application that needs to be translated
ln -s ~/.virtualenvs/<virtyalenv>/lib/python3.4/site-packages/<app> ./

# Makemessages following the symlink
./manage.py makemessages -s

# Create a folder called tpa_translation (Third Party Applications Translation) 
#  at the main project folder (where settings.py is located). 
#  Copy there, the directory tree that springs from your language and is located
#  inside the locale directory of the third party application.
mkdir <proj>/tpa_translation/<app>
cp -R ./<app>/locale/<your_lang> <proj>/tpa_translation/<app>/

# Include this path in the locale_paths in settings.py:
...
LOCALE_PATHS = [
    ...,
    join('path', 'to', 'tpa_translation', '<app>'),
    ...,
]
...

#Remove the symlink
rm ./<app>

# Translate the .po file:
vim <proj>/tpa_translation/<app>/<your_lang>/LC_MESSAGES/django.po

# Compile messages
./manage.py compilemessages

# At this point django will message that a .mo file was created at the directory where the new .po file exists.

答案 1 :(得分:2)

您检查过makemessages for an app installed in virtualenv吗?

它表示您需要为应用创建符号链接,以便makemessages找到第三方应用。

我刚刚遵循该指南,它适用于我翻译Django REST Framework

答案 2 :(得分:1)

我发现,最简单的方法是分叉模块,将其克隆到您的计算机,运行示例项目,然后进行翻译。一旦将这些转换推送到分叉存储库,就可以使用它而不是正式版本。您可以将requirements.txtgit+https://github.com/my-respository.git一起使用。您还可以翻译模块并向原始作者发送合并请求。如果你很幸运,他们会将它合并到原始仓库中,这样你就可以切换回那个仓库了。

其他解决方案存在问题。例如,如果您的第一语言是德语,但该模块是用英语编写的,那么您将无法获得正确的翻译。

希望有所帮助。

答案 3 :(得分:0)

根据django 2文档“ How Django discovers translations”:

  

LOCALE_PATHS中列出的目录具有最高优先级,首先出现的目录具有更高的优先级。

只需在您的根目录中创建一个locale文件夹,然后在您的设置中设置LOCALE_PATHS

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

我的语言环境:

$ tree locale
locale
└── ca
    └── LC_MESSAGES
        ├── django.mo
        └── django.po

(不要忘记编译消息:django-admin compilemessages

我刚刚为我的项目进行了测试,并且运行起来很有趣。