P.Henry F
P.Henry F

Reputation: 31

Bad translation with Djangojs app Django

i've an issue with my Django APP.

I've tried to translate one string in JS file with javascript_catalogue. My string is only translated in english if i give a value to my string in local/en/LC_MESSAGES/Djangojs.po

Admin user can change WebApp language with Ajax like this:

def language(request):
'''
'''
if not request.is_ajax():                       #on verifi qu'on accede à la fonction par une requete ajax
    return HttpResponse('Not an ajax request...')
if not request.method == 'GET':
    return HttpResponse('Not a post ajax request...')#on verifie que le type de requete est valide
language_code = request.GET.get('langue')
settings.LANGUAGE_CODE = language_code


return HttpResponse()

And Django sends template like this:

template = loader.get_template('my_template')
translation.activate(settings.LANGUAGE_CODE)
html = template.render()
return HttpResponse(html)

in urls.py:

js_info_dict = {
        'domain': 'djangojs',
        'packages': ('IHMWEB',),
  }

urlpatterns = [
        url(r'^jsi18n/$', javascript_catalog, js_info_dict, name='javascript-catalog'),
        url(r'^$',                  IHMWEB.views.login),
                        ..
                        ..

MyAPP.settings:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'IHMWEB'
]

LANGUAGE_CODE = 'fr'

gettext = lambda s: s
LANGUAGES = (
    ('en', gettext('English')),
    ('fr', gettext('French')),
    ('de', gettext('German')),
    ('it', gettext('Italian')),
    ('qa', gettext('Arabic')),

)

In my template i create a menu with title:

        textnode = document.createElement('i');
        textnode.setAttribute("class", "fa fa-plus");
        title = gettext("Ajouter un module"); //"Ajouter un module" is french translation for "Add module" 
        textnode.setAttribute("title", title);

And finally Djangojs.po: en/LC_MESSAGES/djangojs.po

#: frontend/src/MODS/index/js/index.js:70
msgid "Ajouter un module"
msgstr "Add new bundle"

fr/LC_MESSAGES/djangojs.po

#: frontend/src/MODS/index/js/index.js:70
msgid "Ajouter un module"
msgstr "Ajouter un module"

If Admin chooses french language, my whole html is translated in french but the string in my js is translated in english.

If Admin chooses english language, my whole html is translated in english and the string in my js is translated in english.

UPDATE: i forget to mention something template.html

<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>

Upvotes: 0

Views: 712

Answers (1)

ajabdelaziz
ajabdelaziz

Reputation: 132

Another approach you can take is writing out all your templates and placing the strings within trans and blocktrans tags with i18n at the top.

this will update your django.po with your strings and handle translations.

You can also use get_language() to get the users current language.

Upvotes: 1

Related Questions