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… 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…
This is the sort of this @claudep knows more about — no doubt there’s a better way.
https://docs.python.org/3.9/library/gettext.html#the-nulltranslations-class
1 Like
Hey @carltongibson
I hoped there would be an easier way this seems tricky but I guess it might be a good learning experience. Thanks.
I suspect it might not be too complex… (Famous last words…)
- Implement
NoneTranslations
overriding gettext()
.
- Get django translations,
add_fallback()
(to your NoneTranslations
)
- You’re done.
In theory.
1 Like