HOWTO Enable multiple languages for your Open edX platform

Open edX supports many different languages out of the box. That means that the platform should be automatically localized depending on your browser settings. Under the hood, Django parses the Accept-Language request header and automatically picks the most appropriate language, as described in the official Django docs. This is achieved by the django.middleware.locale.LocaleMiddleware and is standard Django behaviour.

However, I was recently surprised to discover that, by default, it is only possible to display the Open edX platform in the language set in the LANGUAGE_CODE Django setting. The reason for that is that Open edX tinkers with the ACCEPT_LANGUAGE header in the (aptly named) DarkLang middleware. The DarkLang middleware was designed to release only a subset of locales in production. The middleware re-writes the language header by removing those values that are not present in the current DarkLang configuration. Thus, there are two solutions to enable multiple languages:

  1. Remove the openedx.core.djangoapps.dark_lang.middleware.DarkLangMiddleware middleware from the LMS/CMS settings:

    MIDDLEWARE_CLASSES.remove("openedx.core.djangoapps.dark_lang.middleware.DarkLangMiddleware")
    
  2. Manually add the languages to the current configuration, either from the GUI or from the command line:
    a. From the admin UI: go to /admin/dark_lang/darklangconfig/, click “Add Dark Lang config” and in the “Released languages” field select the language codes, separated by a comma. For instance, to enable English and Chinese, write “en,zh”. Then, click save.
    b. From the command line:

    tutor local run lms ./manage.py lms shell -c "from openedx.core.djangoapps.dark_lang.models import DarkLangConfig; DarkLangConfig.objects.create(enabled=True, released_languages='zh,en')"
    

The supported language codes are listed here. Note that you don’t need to add the default language of your platform (defined during quickstart and stored in the LANGUAGE_CODE setting) to the created DarkLang configuration.

You can then reload your website and your browser languages should automatically be taken into account. If not, you should delete your cookies.

3 Likes