javascript - Djangojs 应用程序的翻译错误 Django

标签 javascript python django translation

我的 Django 应用程序有问题。

我尝试使用 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.设置:

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: 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"

如果管理员选择法语,我的整个 html 都会翻译成法语,但我的 js 中的字符串会翻译成英语。

如果管理员选择英语,我的整个 html 都会翻译成英语,我的 js 中的字符串也会翻译成英语。

更新:我忘记提及一件事 模板.html

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

最佳答案

您可以采取的另一种方法是写出所有模板,并将字符串放在 transblocktrans 标记内,并在顶部使用 i18n

这将使用您的字符串更新您的 django.po 并处理翻译。

您还可以使用get_language()来获取用户当前的语言。

关于javascript - Djangojs 应用程序的翻译错误 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36769122/

相关文章:

javascript - 在 JavaScript 中获取真实的对象类型

javascript - Angular6 轮询不返回数据

python - 如何在列表理解中不向列表中添加任何内容?

python - django unique() 和 order_by() extra() 一起使用

django - 使用 Django DecimalField 和 PostgreSql Numeric 字段保持输出精度

由于重复键,带有固定装置的 Django TestCase 导致 IntegrityError

javascript - HTML 表单中的元素在 Javascript 中未定义

javascript - AngularJS - 如何在 mousemove 上存储鼠标坐标?

python - django annotate - 条件计数

python - Django异步-关于模型保存功能