user6594055
user6594055

Reputation:

Django not searching for translation messages

Trying to figure out how Django i18n works. Already have read through the official and several other tutorials. The problem is that Django generates the .po file, but it doesn't contain messages from my models and Python scripts (well, I'm ready to live with the scripts, but models MUST be translated). Here are my settings:

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


LANGUAGES = (
    ('ru', _('Russian')),
    ('en', _('English')),
)

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

LANGUAGE_CODE = 'en'

And here is the .po file ('locale' folder is located in the root of the project):

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-05-12 12:26-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=4; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%"
"10<=4 && (n%100<12 || n%100>14) ? 1 : n%10==0 || (n%10>=5 && n%10<=9) || (n%"
"100>=11 && n%100<=14)? 2 : 3);\n"

#: trackpost/settings.py:127
msgid "Russian"
msgstr ""

#: trackpost/settings.py:128
msgid "English"
msgstr ""

As you see, it contains only strings from settings. In models, I use ugettext_lazy as _.

Have been fighting with this for 3 days now. Any ideas why this is happening?

UPD. I translate some variables. One of them looks like v = 'string' and then _(v) and another is a text variable which I get through API. None of them is discovered by Django, although the first should be.

UPD. Here is the project structure (unimportant files like migrations missed):

├── account
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── binder.py
├── db.sqlite3
├── install
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── locale
│   │   └── ru
│   │       └── LC_MESSAGES
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── locale
│   └── ru
│       └── LC_MESSAGES
│           └── django.po
├── manage.py
├── nginx.conf
├── nohup.out
├── payments.py
├── trackpost
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── uwsgi.ini

Upvotes: 0

Views: 80

Answers (1)

nik_m
nik_m

Reputation: 12086

You should update the LOCALE_PATHS list to include the locale directory of your app that holds the to-be-translated models.

LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale'),
    os.path.join(BASE_DIR, 'myapp', 'locale'),  # translation lookup directory for "myapp" app
    os.path.join(BASE_DIR, 'another_app', 'locale'),  # translation lookup directory for "another_app" app
]

Then, you should manually create the locale structure under your app, like this:

myapp/locale/ru/LC_MESSAGES/

Then, run ./manage.py makemessages --all which will look under each locale dir declared inside the LOCALE_PATHS and create there the .po file. You know the rest (./manage.py compilemessages).

Upvotes: 1

Related Questions