简体   繁体   English

django翻译不起作用,但模板中的翻译有效

[英]django translation doesn't work but translation in templates works

I try to translate my django site to another languages but translation in python doesn't work. 我尝试将django网站翻译成其他语言,但是python中的翻译不起作用。 But translation in templates using trans tag, works as expected. 但是,使用trans标签在模板中进行翻译的效果符合预期。

I have tried ugettext , gettext , gettext_lazy and ugettext_lazy , and every time I got original untranslated strings. 我已经尝试过ugettextgettextgettext_lazyugettext_lazy ,并且每次我得到原始的未翻译的字符串时。

My sources all in utf-8 encoding, original strings in Ukrainian language 我的资料全部以utf-8编码,乌克兰语原始字符串

You want to check this 您要检查一下

Since you didn't post your code, I assume it has something to do with the alias. 由于您没有发布代码,因此我认为它与别名有关。

Use 采用

from django.utils.translation import ugettext_lazy as _

translated_string = _('original_string')

And you should not have a problem 而且你应该没有问题

"You can only use the _ alias for ugettext and ugettext_lazy (or any other related translation function) in Django or else it won't be recognized by makemessages command. The technical explanation can be found in Robert Lujo's answer ." “您只能在Django中将_别名用于ugettext和ugettext_lazy(或任何其他相关的翻译功能),否则makemessages命令将无法识别它。技术说明可以在Robert Lujo的答案中找到 。”

I think the problem lies in your MIDDLEWARE_CLASSES . 我认为问题出在您的MIDDLEWARE_CLASSES The thing is, there are some middlewares that might change your request, including a language prefix. 问题是,有些中间件可能会更改您的请求,包括语言前缀。 Especially, when you use AJAX calls for querying extra template data, translated by ugettext, gettext, etc. 特别是,当您使用AJAX调用查询额外的模板数据时,这些模板数据由ugettext,gettext等翻译。

The ugettext_lazy will not work if string contains non Latin symbols. 如果字符串包含非拉丁符号,则ugettext_lazy将不起作用。 So in my case the original strings must be the Unicode objects. 因此,在我的情况下,原始字符串必须是Unicode对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM