pythondjangotranslationdjango-i18n

Change Django Default Language


I've been developing a web application in English, and now I want to change the default language to German.

I tried changing the language code and adding the locale directory with all the translations, but Django still shows everything in English. I also want all my table names to be in German along with the content in the templates.

I also tried Locale Middleware and also this repo for a custom middleware but it still doesn't work.

Not to mention, Django changes the default language of the admin panel, but my field and table names remain English.

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'language.DefaultLanguageMiddleware',
    # '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',
]

LANGUAGE_CODE = 'de'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

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

Here is my locale directory:

enter image description here

This is how I use translation in my templates:

{% load i18n static %} 

{% translate "Single User" %} 

This is how I have defined my models:

from django.utils.translation import gettext_lazy as _

class Facility(models.Model):
    name = models.CharField(_('Name'), max_length=100, null=True, blank=True)
  
    class Meta:
        verbose_name_plural = _('Facilities')


Solution

  • Turns out everything is just right, and the only thing that messed things up was a typo in LOCALE_PATHS.

    settings.py:

    LOCALE_PATHS = (    # notice the S which was forgotten
        os.path.join(BASE_DIR, 'locale')
    )