mozilla/zamboni — pull request #2588 by jaredkerim (2014-09-24 19:19:38+00:00)
Title: Expand privacy policy help text (bug 1055790)

This turned out to be pretty tricky. We want to embed some HTML with a link inside a lazily translated string in the help text of a form field. Ideally we'd want to use mark_safe_lazy as seen here:

https://docs.djangoproject.com/en/1.6/topics/i18n/translation/#other-uses-of-lazy-in-delayed-translations

However this didn't seem to want to work for me. I performed some experiments like:

In [7]: type(force_text(mark_safe_lazy(_lazy(u'hello'))))
Out[7]: django.utils.safestring.SafeText

which seem to indicate that mark_safe_lazy is behaving as expected, however if I use it like so:

help_text=mark_safe_lazy(_lazy(u'Expanded help text'))

then it would not correctly render the HTML like so:

![screenshot 2014-09-24 14 38 11](https://cloud.githubusercontent.com/assets/119884/4394184/f22a2426-441e-11e4-967d-5df421fe8a51.png)

I tried using the django built in ugettext_lazy rather than the one provided by tower but that didn't make a difference, if I change mark_safe_lazy to mark_safe then it does render correctly like so:

![screenshot 2014-09-24 15 08 19](https://cloud.githubusercontent.com/assets/119884/4394200/20e19ac4-441f-11e4-98bd-86007be80104.png)

However this will be evaluated at code init time, not at request time, so it won't correctly find the translation. so I had to push the mark_safe invocation into form init time (which happens within the request flow) so that it will be translated correctly.

Another separate issue is that using a format string will also force a lazy string to become evaluated so if we were to use the format option to embed the URL, we would also have to do that lazily so that it's not applied until evaluation time during the request path. Altogether this presented a few bizarre edge case challenges, and this solution is not ideal but it functions in the mean time, I have filed a ticket to clean this up if someone knows the correct solution.