Django, How do I access the LANGUAGE_CODE
When you start translating your app things become very interesting and it really challenges the way you build you applications.
in your settings.py file you add all of the different languages
LANGUAGES = (
('en', gettext('English')),
('sv', gettext('开发者_JAVA百科Swedish')),
('no', gettext('Norwegian')),
)
no I know you can use get_current_language as LANGUAGE_CODE
to access the first bit ie, en
is there a way in my template that I can access the display text ie, English
?
If you have LANGUAGE_CODE
you can use language_name
filter:
{{ LANGUAGE_CODE|language_name }}
(and many other possibilities: django template translation-specific variables)
In django < v1.3:
You can access available languages list via:
{% get_available_languages as LANGUAGES %}
To show this ist:
{% for lang in LANGUAGES %}
CODE:{{ lang.0 }}, NAME:{{ lang.1 }}
{% endfor %}
As you can see language name is lang.1
(second member of the inner tuple)
If you want the name of a language that you have it's code, either put that in the context before passing it to the template or in the loop above add a comparaison condition to get the name.
精彩评论