本文介绍了第三方应用程序的Django翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试翻译Django第三方应用程序( django-recurrence )在我的Django 1.7项目中.尽管我已经在这里阅读了有关同一问题的所有答案,但我仍然无法让Django为该应用程序生成django.po.

I'm trying to translate a Django third-party app (django-recurrence) within my Django 1.7 project.Despite all the answers I've been reading here about the same problem, I'm still being unable to have Django generate the django.po for this app.

这些是我当前的设置:

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的方法,例如:

I've tried modifying LOCALE_PATHS in several ways, like:

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_IT".也没用.

and so on. I've manually translated the django.po from this app tried copying it in such directories accordingly to the settings I was trying time by time, but it never worked. I've tried changing LANGUAGES and LANGUAGE_CODE to almost every possible combination among: 'it', 'it-it', 'it_it', 'it-IT' and 'it_IT'. Didn't work either.

命令:

django-admin.py makemessages --all

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

would only produce locale files for Django itself, totally ignoring the app I want to translate.I've tried using django-rosetta as well, but I can't honestly tell to have deepen this path too much, having already translated the app myself. Basically, I think that finding the correct way of simply telling Django to compile the django.po I've written for django-recurrence and using it should be enough.

我在这里想念什么?

推荐答案

我的答案试图将:

# From the root of your project (BASE_DIR):
# Create a symlink to the application that needs to be translated
ln -s /<path_to_virtualenv>/lib/pythonx.x/site-packages/<app> ./

# Instruct makemessages to follow the symlink
./manage.py makemessages -s

# Create a folder called "tpa_translation"
# (Third Party Applications Translation)
mkdir ./<proj>/tpa_translation/<app>

# Copy inside, the directory tree that springs from your language.
# This is usually located inside the locale directory
# of the third party application.
cp -R ./<app>/locale/<your_lang> ./<proj>/tpa_translation/<app>/

# Result:
# If <your_lang> is "en", the resulting folder will be:
# f"{BASE_DIR / 'tpa_translation' / '<app>' / 'en'}"

# Set accordingly the LOCALE_PATHS in settings.py:
...
LOCALE_PATHS = [
    ...,
    f"{BASE_DIR / '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
# inside the directory where the new .po file exists.

这篇关于第三方应用程序的Django翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 18:52
查看更多