Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How to get language code in template?

Is there's some global variable for gettin' language code in django template or atleast passing it through view? something like: {{ LANG }} should produce "en" for example.. I really not comfortable when people using request.LANGUAGE_CODE.

Detailed explanation would be appreciated =)

like image 603
holms Avatar asked Nov 25 '11 05:11

holms


People also ask

How do I get current language in Django?

Functions of particular interest are django. utils. translation. get_language() which returns the language used in the current thread.

How do I use Python code in Django template?

You cannot use python code in django template. This is by design, Django's idea of template is to isolate the presentation logic from the programming code.

How does Django discover language preferences?

This process relies on the GNU gettext toolset. Once this is done, Django takes care of translating web apps on the fly in each available language, according to users' language preferences.


1 Answers

It's an old topic. But some might find it useful.

{% load i18n %} ... {% get_current_language as LANGUAGE_CODE %} 

Django reference and example.

like image 183
Rafael Avatar answered Oct 11 '22 14:10

Rafael