Transifex

  • Documentation
  • Django SDK
  • Display translated content

Display translated content

Note

Transifex Native is currently in BETA. To learn more about the solution as well as to have a hands on experience please get in touch with us.

Transifex Native will automatically display content in the language currently selected in your Django application.

In order to allow changing the current language, you will need to implement a language picker in your code.

Follows a language picker example for your Django project:

  1. Αdd a language selection form on the same HTML file you added the translatable strings before, like so:
{% load i18n %}
{% load transifex %}

<p>{% t "Hello!" %}</p>
<p>{% t "I want to be translated." %}</p>

<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
    <input name="next" type="hidden" value="/" />
    <select name="language">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
        <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
        {{ language.name_local }} ({{ language.code }})
        </option>
        {% endfor %}
    </select>
    <input type="submit" value="Go" />
</form>


2. Add the following route in your Project's routes, so that the set_language hook shown above will work when submitting the form.

from django.conf.urls import url, include

urlpatterns = [
    ...,
    url(r'^i18n/', include('django.conf.urls.i18n')),
]


3. Lastly, add 'django.middleware.locale.LocaleMiddleware' in your settings.MIDDLEWARE to enable the functionality.

Now you can test the language picker. Each string will be shown translated in the current language.

If a translation is not available on Transifex, the source string will appear instead by default. This behaviour is configurable by defining a different missing policy. For example, you can choose to show dummy content instead of the source string, a method often referred to as “pseudo-localization”. This way, you can test the UI and allow strings that have not been translated to stand out.

TRANSIFEX_MISSING_POLICY = 'transifex.native.rendering.PseudoTranslationPolicy'
# _t("Hello, friend") -> returns "Ȟêĺĺø, ƒȓıêñđ"

Please refer to Django documentation for further information on creating a language picker: