Reputation: 1169
I have my available languages in settings.py
LANGUAGE_CODE = 'en'
LANGUAGES = (
('fr', _('French')),
('en', _('English')),
)
When i loop in my template, I think it is the expected behavior that django gives me the translated names via {{ lang.1 }}. But i don't want lang names to be translated so i have changed my settings.py as below:
LANGUAGES = (
('fr', 'Francais'),
('en', 'English'),
)
I am still getting translated lang names. Do you have an idea? Does {% get_available_languages as languages %} template tag automatically translates the list items? If so how can i use untranslated language names while looping in available languages?
---- EDIT ---
I have checked the code of get_available_languages template tag of django. I think it is translated here:
class GetAvailableLanguagesNode(Node):
def __init__(self, variable):
self.variable = variable
def render(self, context):
context[self.variable] = [(k, translation.ugettext(v)) for k, v in settings.LANGUAGES]
return ''
Maybe i have to write my own template tag...
Upvotes: 6
Views: 2925
Reputation: 160
I had the same issue (wanting the language names in their own translation) and simply redefined the languages variable in views before passing it to the template:
from django.utils.translation import get_language_info
languages = [(lang[0], get_language_info(lang[0])['name_local'] for lang in settings.LANGUAGES]
That basically allows to have quickly a variable of language codes and names in their own translation, namely :
[('en', 'English'), ('fr', 'Français')]
I guess the same line would do the job in a custom template tag. Hope it helps !
Upvotes: 0
Reputation: 9616
No Hacks, this time
According to the translation documentation you can use the available language tools either in the template or in the python code.
In the template, using the get_language_info template tag:
{% get_language_info for "pl" as lang %}
Language code: {{ lang.code }}<br />
Name of language: {{ lang.name_local }}<br />
Name in English: {{ lang.name }}<br />
Bi-directional: {{ lang.bidi }}
Name in the active language: {{ lang.name_translated }}
which can be combined with other tags and build a mechanism that allows you to change languages:
{% for lang_code, lang_name in languages %}
{% if lang_code != LANGUAGE_CODE %}
{% get_language_info for lang_code as lang_info %}
{% language lang_code %}
{% url request.resolver_match.url_name as no_slug %}
{% url request.resolver_match.url_name slug=object.slug as yes_slug %}
<p>Link to: {% firstof yes_slug no_slug %} Local name: {{ lang_info.name_local }}</p>
{% endlanguage %}
{% endif %}
{% endfor %}
In this thread the same result is achieved in the view.
Otherwise, you can use get_language_info in your code as follows:
>>>from django.utils.translation import get_language_info
>>>li = get_language_info('en')
>>>print(li)
{'bidi': False, 'code': 'en', 'name': 'English', 'name_local': 'English'}
and use it in the context of the following example:
from django.utils import translation
def welcome_translated(language):
cur_language = translation.get_language()
try:
translation.activate(language)
text = translation.ugettext('welcome')
finally:
translation.activate(cur_language)
return text
if i write ('en', 'test'), for example it works and gives 'test' as expected
Thank you! This idea brought forward the following solution hack:
LANGUAGES = (
('fr', 'Francais'),
('en', ' English'),
)
Notice the space added before the word English.
Upvotes: 5