Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Preventing translation of available language names in template

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...

like image 823
ratata Avatar asked Oct 19 '22 16:10

ratata


1 Answers

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.

like image 63
raratiru Avatar answered Oct 23 '22 06:10

raratiru