set display to none if string is not translated

Hi,
I am writing a translation program and I am trying to implement a functionality where, If the translation string in the .po file is empty ( msgstr “” ) the parent elements visibility will be set to none.
As an example, if I have this template code:

{% language target_language %}
 <td><p> {% translate Hello %} </p></td>
{% endlanguage %}

If the target languages po file should look like this:

#: translation/forms.py:10

msgid “Hello”
msgstr “”

then I would like to set:

{% language target_language %}
 <td style="display: none"><p> {% translate Hello %} </p></td>
{% endlanguage %}

How could I go about implementing this?

Best Regards
Max

Hi @3ng7n33r — that’s a nice question.

Not sure if there’s a better way but… :smiley: gettext has a NullTranslations class, that you subclass to provide your own logic.

By default it just returns the message untranslated, but you could create one to return None, and make that the fallback translations… :thinking:

This is the sort of this @claudep knows more about — no doubt there’s a better way. :slight_smile:

https://docs.python.org/3.9/library/gettext.html#the-nulltranslations-class

1 Like

Hey @carltongibson
I hoped there would be an easier way :smiley: this seems tricky but I guess it might be a good learning experience. Thanks.

I suspect it might not be too complex… :grinning: (Famous last words…)

  1. Implement NoneTranslations overriding gettext().
  2. Get django translations, add_fallback() (to your NoneTranslations)
  3. You’re done.

In theory. :thinking:

1 Like