Question

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?

Was it helpful?

Solution

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

OTHER TIPS

I have 3 different locale folder in my whole project. But; I had only 1 locale path. My project structure is;

├api_auth
├── project_name_1
│   ├── locale
│   └── settings
├── project_name_2
│   └── locale
│
├── locale 
├── manage.py
└── Dockerfile

In settings i have:

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'), )

So i manually add other locale paths like:

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
    os.path.join(BASE_DIR, 'project_name_1/locale'),
    os.path.join(BASE_DIR, 'project_name_2/locale'),
)

Hopefully it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top