Why django does not account for my language file?

I followed all the translation documentation, but django does not account for my .mo file.

Some facts:

  • I created the conf/locale/ folder in the root of my project
  • django.po successfully generated using django-admin.py makemessages -l fr
  • django.mo successfully generated using django-admin.py compilemessages

So my folder structure:

 project/ site/ locale/ fr/ LC_MESSAGES/ django.mo django.po 
  • In settings.py I set LANGUAGE_CODE = 'fr'
  • My browser sends Accept-Language:fr-FR,fr; correctly Accept-Language:fr-FR,fr;
  • request.LANGUAGE_CODE shows fr from my views

But I don't get anything translated ... How do I get django to account for these files?


Edit

Runs /home/www/project/locale until settings.LOCALE_PATHS . However, Django has to find this path on its own, and I don't like to use absolute paths. What's going on here?

+6
source share
1 answer

LOCALE_PATHS

Django searches for translation files in 3 places by default:

  • LOCALE_PATHS/(language)/LC_MESSAGES/django.(po|mo)
  • $APPPATH/locale/(language)/LC_MESSAGES/django.(po|mo)
  • $PYTHONPATH/django/conf/locale/(language)/LC_MESSAGES/django.(po|mo)

LOCALE_PATHS should only be used if your translation files are not in your application directories or on PYTHON_PATH.

LOCALE_PATHS example in settings.py

 LOCALE_PATHS = ( '/home/www/project/conf/locale', # replace with correct path here ) 

MIDDLEWARE_CLASSES

Add django.middleware.locale.LocaleMiddleware to MIDDLEWARE_CLASSES in settings.py

LANGUAGES

 LANGUAGES = ( ('fr', 'Franรงais'), ) 
+13
source

Source: https://habr.com/ru/post/921280/


All Articles