Django translations of third party apps

2019-05-21 08:21发布

问题:

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?

回答1:

My answer is trying to compile together all the steps described in the answer provided by @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.


回答2:

Have you checked makemessages for an app installed in virtualenv?

It says that you need to make a symlink to the app in order for makemessages to find the 3rd party app.

I've just followed that guide and it works for me translating Django REST Framework



回答3:

The easiest way, I found for myself, is to fork the module, clone it to you computer, get an example project running and then do the translations. Once you push those translation back to your forked repository, you can use that instead of the official version. You can use it your requirements.txt with git+https://github.com/my-respository.git. You can also translate the module and send a merge request to the original authors. If you are lucky, they will merge it into the original repo so you can switch back to that one.

Other solutions are problematic. For instance, if your first language is German, but the module was written in English, you will have a problem getting the correct translations.

Hope that helps.



回答4:

According with django 2 documentation "How Django discovers translations":

The directories listed in LOCALE_PATHS have the highest precedence, with the ones appearing first having higher precedence than the ones appearing later.

Just create a locale folder in your root directory and set LOCALE_PATHS in your settings:

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

My locales:

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

( Dont forget to compile messages: django-admin compilemessages )

I just tested for my project and runs like a charm.