curiousguy
curiousguy

Reputation: 3262

How to use Django translated variable (trans) in template if statement

I am facing a prob in django. The following is my code snippet :

{% if pageName != 'My page Name' %}
  .....{{ then this }}

Now this works fine for English , Now when i translated my application in another language the pageName also changed according to that language. So the above logic is not working as it is hard coded English

So i have to try to implement the logic with translated version of 'My page Name'. But i cant use it directly in if like :

{% if pageName != trans 'My page Name' %} 

So I thought of storing the translated version in another variable and then check with that variable like :

{%blocktrans%} "My page Name" {{myvar}} {%endblocktrans%}
{% if pageName != myvar %}

But this is also not working myvartakes the value "My page Name" , not the translated version of it.

Any clue how to fix it. Thanks in advance.

Upvotes: 7

Views: 9182

Answers (1)

fragles
fragles

Reputation: 680

You can use trans template tag but this way

{% trans "My page Name" as myvar %} 
{% if pageName != myvar %}
...

See trans template tag

Upvotes: 12

Related Questions