I want to make a django.po in the root of the project( contains all model and templates )
in my settings.py
file.
LOCAL_PATHS = ('/path/to/project/locale/', )
but it doesn't work. . After makemessages and compilemessages,the po/mo
file is generated successfully,but nothing happens when I change the language settings(The modeltranslation works well). So I think maybe the locale directory cannot be recognized in the project root .
Here is my project structure:
project
-app/
-app/
-project/
-settings.py
-urls.py
-templates/
-static/
-locale/
then i put the locale directory under an app directory and use the makemessages/compilemessages
tool,
it works. But it only contains the translation which marked in this app, which means I can't make the translations that marked in templates or other apps.
Is there any better solution for this situation?
{% load i18n %} is needed for internationalization. The purpose of internationalization is to allow a single application to read in multiple languages. In order to do this: you need a few hooks called translation strings. To give your template access to these tags, put {% load i18n %} toward the top of your template..
Standard translationSpecify a translation string by using the function gettext() . It's convention to import this as a shorter alias, _ , to save typing. Python's standard library gettext module installs _() into the global namespace, as an alias for gettext() .
PO file is a portable object file, which is text-based. These types of files are used in common in software development. The . PO file may be referenced by Java programs, GNU gettext, or other software programs as a properties file. Django uses gettext to interact with translation PO and mo files.
Here how I implemented:
I kept locale folder like this in my project:
-Project #*main Project directory
-apps
-apps
-templates
-project
-settings.py
-locale #outside project folder but within main Project
-ru_RU
-LC_MESSAGES
-django.po
Now I gave the locale path in settings.py like:
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
LOCALE_PATHS = (
os.path.join(PROJECT_PATH, '../locale'),
)
Language choices:
LANGUAGES = (
('en-us', 'English'),
('ru_RU', 'Russian'),
)
LANGUAGE_CODE = 'en-us' 'ru_RU'
Finally added the middleware:
django.middleware.locale.LocaleMiddleware
Hopefully it will help. I wrote about it in details at my blog: http://ruddra.com/2015/09/17/django-translation-using-po-file/ .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With