Search code examples
pythonruby-on-railsdjangorails-i18ndjango-i18n

Address Django translation messages using variables


Is there a way to set translation messages parametrically in Django? For example, when I need to traslate a message in a template, I always need to write the whole text:

<h1>{% trans "Hello World" %}</h1>

Instead, I'm wondering if there is something that allows to use variables, something similar to Ruby on Rails:

<h1><%= t :hello_world %></h1>

Solution

  • As stated in the trans template tag documentation you can pass a variable in the same way you pass a string literal:

    <h1>{% trans myvar %}</h1>
    

    Of course that variable need to be available in the template context