简体   繁体   中英

Translating dynamic content in django

I have a text that has both static and dynamic part like this.

Custom message with %(card_status)s text inside

I am in fix as to what the best method there is to translate the text.

This is what I currently have,

{% blocktrans with obj.card_status as card_status %}Custom message with {{ card_status }} text inside{% endblocktrans %}

If I do that, the message generated is

msgid "Custom message with %(card_status)s text inside"
msgstr "This will be translated"

But the problem with this approach is that, no matter what the card_status variable is, the translated text will be same.

I tried enumerating the django.po file manually with msgid for each of the card_status's possible values.

But that is not being considered, example,

msgid "Custom message with ACTIVE text inside"
msgstr "This will be translated with ACTIVE text"

Can somebody suggest a method or a hack that can be used here. There are many similar questions in stack that I referred, but somehow I am not able to get the solution I require.

Hope someone can put an end to this question once and for all for everybody's joy.

Answering this for people who might needs this in the future.

This is more of an understanding than a solution that is created by me.

First I had this

{% blocktrans with obj.card_status as card_status %}Custom message with {{ card_status }} text inside{% endblocktrans %}

Problem : The part card_status was replaced with dynamic value, but not getting translated.

Solution : So I applied a template filter called template_trans to the calculated value ' card_status ' that marks to django that this variable also needs to be translated. (will add that filter code below)

{% blocktrans with obj.card_status|template_trans as card_status %}Custom message with {{ card_status }} text inside{% endblocktrans %}

Doing a makemessages command now generates this text inside the po file like before

Custom message with %(card_status)s text inside

Now you need to manually add all the possible values the card_status can take into the same po file . Like in my case I added these values

msgid "ACTIVE"
msgstr ""

msgid "INACTIVE"
msgstr ""

msgid "LOST"
msgstr ""

Now the code for the template_trans is here, add this as a filter where you would generally have your other filters.

from django.utils.translation import ugettext
@register.filter(name='template_trans')
def template_trans(text):
    try:
        return ugettext(text)
    except Exception, e:
        return text

Thats it, django now does two translations for you, one the static part using the first msgid posted above. It then does the second one based on the actual value ACTIVE or INACTIVE etc to give you a combined output.

Note 1: Translators should see this %(variable_name)s in the message id and not {{ variable_name }}. That is archieved by using with tag along with blocktrans and template trans filter. Example shown above.

Note 2: You should have all the possible values of the %(variable_name)s populated in django.po. If not you will get the value of the variable and not the translated one.

Note 3: Ensure that the individual values that you poplated in the po file have their msgstr part filled in...

Django provides lots of tools for localizing content both in Python code (mainly via gettext and gettext_lazy , including pluralization ) and in templates (via the tags trans , blocktrans and plural ; even _() is available in templates).

If you find that there is untranslated text in your UI, you need to expose that text via the mechanisms above instead of manually tinkering with PO files.

So if you have some status flags ACTIVE , INACTIVE etc., then this is clearly language-specific content that needs to be exposed in some way.

One way to go about it is to imagine that the flag values are meaningless to humans – what would you do to ensure they make sense in a UI? Exactly: You would assign string labels to them, and you would display those string labels instead of any cryptic status values.

Now you only have to expose those labels via gettext and you're set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM