SolnLase
SolnLase

Reputation: 89

Why my translations in django i18n don't work

I've been following 3 different tutorials for text translation in django, and with none of them my translation has worked, however I was doing exactly the same steps as in the tutorials. Django just doesn't translate my text, it goes without any kind of error. My last try was with this course: https://www.youtube.com/watch?v=AlJ8cGbk8ps. But just to be sure I'm adding my code below

settings.py

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

LANGUAGE_CODE = 'es'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

views.py

from django.utils.translation import gettext as _

# Create your views here.
def index(request):
    context = {
        'hello':_('Hello'),
    }
    
    return render(request, 'index.html', context)

index.html

{% load i18n %}

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <h1>{{ hello }}</h1>
    <h2>{% trans "My name is Dawid" %}</h2>

</body>
</html>

My locale folder looks like this:

Locale folder:

I think I should also mention that I use virtual environment, but when I switched it off it doesn't work either. Whether I switch LANGUAGE_CODE to es or pl it takes no effect. I've compiled them too.

Upvotes: 1

Views: 565

Answers (1)

Francisco
Francisco

Reputation: 656

Try add this in your settings.py:

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

Will fix it!

Upvotes: 2

Related Questions