Home > Software engineering >  Translating {{ model.name }} data dynamically in django template
Translating {{ model.name }} data dynamically in django template

Time:08-11

Can anyone suggest shortest way to translate {{ model.name }} type of data in django template?

CodePudding user response:

Templates aren't supposed to do buisiness logic in django, in a good way you need to pass already translated data in your template.

Although if you actually in need to do it, you can use django filters:

Link to django documentation

Code may look like this:

new_filter.py

from django.template.defaulttags import register

@register.filter
def translator_filter(string_to_translate):
    translated_string = #do logic with translation here
    return translated_string

any_page.html

<p>{{ model.name|translator_filter }}</p>
  • Related