Search code examples
htmlflaskinternationalizationjinja2flask-babel

How to internationalize title in Flask?


I'm trying to internationalize a flask webpage and I did every variable but couldn't succeed at internationalizing the title.

My code is below:

{% extends "base.html" %}{% block title %}Test Title{% endblock %}{% block content %}

I already tried this:

{% extends "base.html" %}{% block title %}_(Test Title){% endblock %}{% block content %}

and this:

{% extends "base.html" %}{% block title %}_("Test Title"){% endblock %}{% block content %}

both of them didn't work. Can you help me?


Solution

  • The third one proposition is the closest, because "Test Title" is a string so it needs quotes, but you must put it into double braces {{. When your string is surrounded by _( ) it becomes a variable and the template needs the double braces to display it.

    The result is : {{ _("Test Title") }}