• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


28 Nov, 2024

Updated at 05 Dec, 2024

Translation in Django template does not work

I have a wagtail project where wagtail handles the dynamic page translations. In addition to this I have some string literals in my django templates where I use translation. Actually I have a subscription form where two labels are correctly translated but the rest of the texts are not.

Even when I try to check the translation logic in the shell, it is not working.

from django.utils.translation import activate, gettext as _
activate("hu")
print(_("Hello"))

The result is Hello, despite of the fact that it should be “Szia” in Hungarian. The relevant part of the .po file:

#: newsletter/templates/newsletter/emails/confirmation_asking.html:35
msgid "Hello"
msgstr "Szia"

My django template:

{% load i18n wagtailcore_tags wagtailimages_tags %}
<div id="subscription-modal" class="modal {% if form_has_errors %}is-active{% endif %}">
    <div class="modal-background"></div>
    <div class="modal-card">
        <header class="modal-card-head">
            <p class="modal-card-title">{% translate "Join the Inner Circle for Business AI Insights" %}</p>
            <button class="delete" aria-label="close" onclick="closeModal()"></button>
        </header>
        <section class="modal-card-body">
            <p>{% translate "Subscribe to receive weekly insights, use cases, and best practices on AI and business transformation." %}</p>

            <!-- Display form-level errors -->
            {% if form.non_field_errors %}
            <div class="notification is-danger">
                <ul>
                    {% for error in form.non_field_errors %}
                    <li class="has-text-danger">{{ error }}</li>
                    {% endfor %}
                </ul>
            </div>
            {% endif %}

            <!-- Subscription Form -->
            <form method="post" id="subscription-form" action="{% url 'subscribe' %}">
                {% csrf_token %}
                
                <!-- Hidden field to capture the current page URL -->
                <input type="hidden" name="next" value="{{ request.path }}">
                
                <!-- Name Field -->
                <div class="field">
                    <label class="label" for="id_name">{% translate "Name" %}</label>
                    <div class="control">
                        <input class="input" type="text" name="name" id="id_name" placeholder="{% translate 'Write your nickname here' %}" {% if subscription_form.name.value %} value="{{ subscription_form.name.value }}" {% endif %} {% if subscription_form.name.field.required %}required{% endif %}>
                    </div>
                    {% if subscription_form.name.errors %}
                    <p class="help has-text-danger">{{ subscription_form.name.errors }}</p>
                    {% endif %}
                </div>

                <!-- Email Field -->
                <div class="field">
                    <label class="label" for="id_email">{% translate "Email" %}</label>
                    <div class="control">
                        <input class="input" type="email" name="email" id="id_email" placeholder="{% translate '[email protected]' %}" {% if subscription_form.email.value %} value="{{ subscription_form.email.value }}" {% endif %} {% if subscription_form.email.field.required %}required{% endif %}>
                    </div>
                    {% if subscription_form.email.errors %}
                    <p class="help has-text-danger">{{ subscription_form.email.errors }}</p>
                    {% endif %}
                </div>

                <!-- Topic Selection (Rendered by Django) -->
                <div class="field">
                    <label class="label">{% translate "Topics (You can choose more)" %}</label>
                    <div class="control">
                        {{ subscription_form.topics }}
                    </div>
                </div>
                {% if subscription_form.topics.errors %}
                <p class="help has-text-danger">{{ subscription_form.topics.errors }}</p>
                {% endif %}

                <hr class="is-divider" style="margin: 1.5em 0;">
                <label class="label">{% translate "Legal Statements" %}</label>

                <!-- Privacy Agreement -->
                <div class="field">
                    <div class="control">
                        <label class="checkbox">
                            <input type="checkbox" name="brevo_agreement" id="id_brevo_agreement" {% if subscription_form.brevo_agreement.value %}checked{% endif %}>
                            I agree to <a href="https://www.brevo.com/privacy-policy" target="_blank">{% translate "Privacy Policy" %}</a> {% translate "and" %} <a href="https://www.brevo.com/terms-of-service" target="_blank">{% translate "Terms of Service" %}</a>.
                        </label>
                    </div>
                    {% if subscription_form.privacy_approved.errors %}
                    <p class="help has-text-danger">{{ subscription_form.privacy_approved.errors }}</p>
                    {% endif %}
                </div>

                <!-- Subscription Agreement -->
                <div class="field">
                    <div class="control">
                        <label class="checkbox">
                            <input type="checkbox" name="email_opt_in" id="id_email_opt_in" {% if subscription_form.email_opt_in.value %}checked{% endif %}>
                            {% translate "I agree to receive newsletters and accept the data privacy statement." %}
                        </label>
                    </div>
                    {% if subscription_form.subscription_agreed.errors %}
                    <p class="help has-text-danger">{{ subscription_form.subscription_agreed.errors }}</p>
                    {% endif %}
                </div>

                <!-- Submit Button -->
                <div class="field">
                    <div class="control">
                        <button type="submit" class="button is-primary is-fullwidth">{% translate "Subscribe" %}</button>
                    </div>
                </div>
            </form>
        </section>
    </div>
</div>

1 post - 1 participant

Read full topic