【发布时间】:2016-04-21 12:07:15
【问题描述】:
我的 Django APP 有问题。
我尝试用 javascript_catalogue 翻译 JS 文件中的一个字符串。如果我在 local/en/LC_MESSAGES/Djangojs.po 中为我的字符串赋值,我的字符串只会翻译成英文
管理员用户可以像这样使用 Ajax 更改 WebApp 语言:
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()
然后 Django 发送这样的模板:
template = loader.get_template('my_template')
translation.activate(settings.LANGUAGE_CODE)
html = template.render()
return HttpResponse(html)
在 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')),
)
在我的模板中,我创建了一个带有标题的菜单:
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);
最后是 Djangojs.po: zh/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"
如果管理员选择法语,我的整个 html 会翻译成法语,但我的 js 中的字符串会翻译成英语。
如果管理员选择英文,我的整个 html 被翻译成英文,我的 js 中的字符串被翻译成英文。
更新:我忘了提一些事情 模板.html
<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
【问题讨论】:
标签: javascript python django translation