How to properly add entries for computed values to the django internationalization messages file?

前端 未结 3 1741
不思量自难忘°
不思量自难忘° 2021-01-04 08:11

Django documentation states:

The caveat with using variables or computed values, as in the previous two examples, is that Django\'s translation-stri

3条回答
  •  青春惊慌失措
    2021-01-04 08:24

    We're currently in the process of figuring this out as well. While we haven't done so properly, we do have a rather annoyingly ugly hack to get around it.

    We simply define a "dummy" function somewhere in the code (for example your models.py or even settings.py) and fill it up with all the strings that we need to have a translation for.

    from django.utils.translation import ugettext_lazy as _, pgettext
    
    def dummy_for_makemessages():
        """
        This function allows manage makemessages to find the forecast types for translation.
        Removing this code causes makemessages to comment out those PO entries, so don't do that
        unless you find a better way to do this
        """
        pgettext('forecast type', 'some string')
        pgettext('forecast type', 'some other string')
        pgettext('forecast type', 'yet another string')
        pgettext('forecast type', 'etc')
        pgettext('forecast type', 'etc again')
        pgettext('forecast type', 'and again and again')
    

    This function is never called but simply defining it prevents the message strings from getting commented out by makemessages.

    Not the most elegant solution but it works.

提交回复
热议问题