Nicholas Hamilton
Nicholas Hamilton

Reputation: 10526

Django Translation in Overridden Form

This is a problem with localisation:

I need to override the login form (base from the allauth library), to modify some formatting etc....

forms.py

from django.utils.translation import ugettext_lazy as _
class CustomLoginForm(LoginForm):
    def __init__(self,*args,**kwargs):
        super(CustomLoginForm ,self).__init__(*args,**kwargs)
        self.fields['login'].help_text    = _(u"Don't have an account? <a href='%(URL)s'>Register</a>" % {'URL':reverse('account_signup')})
        self.fields['password'].help_text = _(u"Forgotten Password? <a href='%(URL)s'>Reset</a>" % {'URL':reverse("account_reset_password")})

In the above, the translation is not being reflected in the form. I have made the necessary changed to the locale file XYZ.po, but it has no effect. All my other translations work fine, so clearly I am doing something wrong.

Here is the corresponding entry from MYAPP/locale/fr/LC_MESSAGES/django.po

#: MYAPP/allauth/forms.py:16
#, python-format
msgid "Don't have an account? <a href='%(URL)s'>Register</a>"
msgstr "Ne pas avoir un compte? <a href='%(URL)s'>Enregistrer </a>"

#: MYAPP/allauth/forms.py:17
#, python-format
msgid "Forgotten Password? <a href='%(URL)s'>Reset</a>"
msgstr "Mot de passe oublié? <a href='%(URL)s'>Réinitialiser</a>"

Can someone steer me in the right direction please?

Cheers.

Upvotes: 0

Views: 248

Answers (1)

Ahmed Hosny
Ahmed Hosny

Reputation: 1172

This should work

_(u"Don't have account? <a href={URL}>Register</a>").format(URL=reverse('account_signup'))

Why .format works, actually I've searched for this before but I could not find a clear answer. My guess is:

  1. Some symbols can make the translation fails such as % and single quote.

  2. The translated string should use a placeholder which is satisfied in {} and %

  3. There are guidlines here http://edx.readthedocs.org/projects/edx-developer-guide/en/latest/internationalization/i18n.html that I usually follow.

Upvotes: 1

Related Questions