Django Translations Of Third Party Apps
Solution 1:
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 translatedln -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 symlinkrm ./<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.
Solution 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
Solution 3:
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.
Solution 4:
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.
Post a Comment for "Django Translations Of Third Party Apps"