Search code examples
pythondjangotranslationlanguage-translation

Django 2.2 translations not applying, LANGUAGE_CODE and i18n URL pattern correct


I'm using Django 2.2 and Python 3.7.

I'm trying to set up Django translations, and everything seems to work up until the point of seeing the translations.

I'm using i18n urls so the url shows up as

localhost/language-locale

example:

localhost/en-us/
localhost/fr-fr/

url patterns:

urlpatterns += i18n_patterns(
    path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
    path('i18n/', include('django.conf.urls.i18n')),
    ...

settings_l10_i18.py:

LANGUAGE_CODE = 'en-us'
LANGUAGES = [
    ('en-us', _('US English')),
    ('en-gb', _('British English')),
    ('en-ca', _('Canadian English')),
    ('fr-fr', _('French')),
    ('fr-ca', _('Canadian French')),
]

TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = False

USE_THOUSAND_SEPARATOR = True


LOCALE_PATH = [
    os.path.join(BASE_DIR, "locale"),
]

file structure basedir/locale


├── en_CA
│   └── LC_MESSAGES
│       ├── django.mo
│       └── django.po
├── en_GB
│   └── LC_MESSAGES
│       ├── django.mo
│       └── django.po
├── en_US
│   └── LC_MESSAGES
│       ├── django.mo
│       └── django.po
├── fr_CA
│   └── LC_MESSAGES
│       ├── django.mo
│       └── django.po
├── fr_FR
│   └── LC_MESSAGES
│       ├── django.mo
│       └── django.po
└── README.md

Language switcher in template:

<li>
        <form action="{% url 'set_language' %}" method="post">
            {% csrf_token %}
            <input name="next" type="hidden" value="{{ redirect_to }}"/>
            <select name="language" value="" onchange="this.form.submit()">
                {% get_current_language as LANGUAGE_CODE %}
                {% get_available_languages as LANGUAGES %}
                {% for lang in LANGUAGES %}
                    <option value="{{ lang.0 }}" {% if lang.0 == LANGUAGE_CODE %} selected="selected"{% endif %}>
                        {{ lang.1 }} ({{ lang.0 }})
                    </option>
                {% endfor %}
            </select>
        </form>
    </li>

MiddleWare:

MIDDLEWARE =  [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
     ...
]

none of the .po files have fuzzy

I'm using ugettext_lazy and {% trans 'string' %} on everything that needs to be trasnlated, and the .po file is picking it all up

The translations do show up in the .mo files after a run django-admin compilemessages

I've also tried restarting the django server, and PC.

the LANGUAGE_CODE and request.LANGUAGE_CODE show up as en-gb or whatever I select on the selector if I look at the variables in the debugger.

the url also changes to localhost/en-gb or whatever else I select

Really not sure what else to try, any help is appreciated.


Solution

  • Figured it out, only took 4 hours:

    LOCALE_PATH = [
        os.path.join(BASE_DIR, "locale"),
    ]
    

    should be

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