Ali Abdallah
Ali Abdallah

Reputation: 33

Django makemessages command generates error: "xgettext: Non-ASCII string"

I have a simple django project. I am attempting to translate the site into Russian. Here is my settings.py file:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'n_gc#7lsv48h-b$)9aw6eer6z$zia#@da=bdwgova1=6u!8uvh'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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

ROOT_URLCONF = 'omefx.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+'/omefx/templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.i18n',
            ],
        },
    },
]

WSGI_APPLICATION = 'omefx.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}



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

LOCALE_PATHS = (
    os.path.join(BASE_DIR +'/omefx/', 'locale'),
    'locale',
    # os.path.join(PROJECT_DIR, 'locale'),
)
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (os.path.join(BASE_DIR+'/omefx/', 'static'), )

In my template I have a {% trans %} tag for testing:

<h2>{% trans '160+ TRADING INSTRUMENTS' %}</h2> 

When I run the command below:

python manage.py makemessages -l ru

I get the following error:

xgettext: Non-ASCII string at ./myvenv/lib/python3.6/site-packages/pip/_vendor/webencodings/__init__.py:64.
          Please specify the source encoding through --from-code or through a comment
          as specified in http://www.python.org/peps/pep-0263.html.

Side Note: No apps in my project. My Locale folder path is omefx/omefx/locale as well as my templates folder.

Upvotes: 3

Views: 2056

Answers (2)

You should run the command with --ignore venv or -i venv ignoring virtual environment (myvenv in your case) as shown below, then the error is solved. *You can see my answer explaining why the error occurs in detail:

python manage.py makemessages --ignore ru myvenv
python manage.py makemessages -i ru myvenv

The commands below also work:

django-admin makemessages --ignore ru myvenv
django-admin makemessages -i ru myvenv

Upvotes: 0

JaB
JaB

Reputation: 461

I found a fix in a similar question. Find the directory in which the files are that produce the error and ignore them when exceute the manage.py makemessages command.

So if for example you're venv folder produces the error use the makemessages comand like this:

manage.py makemessages -l ru -i venv

Link to that other question: Django makemessages errors Unknown encoding "utf8"

Upvotes: 8

Related Questions