Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
805 views
in Technique[技术] by (71.8m points)

python - Django translations of third party apps

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.

These are my current settings:

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

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'))
...

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.

The command:

django-admin.py makemessages --all

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.

What am I missing here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My answer is trying to compile together all the steps described in the answer provided by @catabaran:

# 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...