【问题标题】:Django objects are appearing instead of translated string出现 Django 对象而不是翻译的字符串
【发布时间】:2014-10-09 11:57:34
【问题描述】:

我有一个与 python 库“Matplotlib”相关的问题。实际上我正在使用这个库创建折线图和条形图,并使用代码添加 X 轴和 Y 轴标题

yaxis = "Production"

xaxis = "year"

plt.ylabel(yaxis, fontsize=20)

plt.xlabel(xaxis, fontsize=20)![enter image description here][1]

但同时我也想使用语言翻译 i18n 为此我使用了

from django.utils.translation import ugettext_lazy as _
yaxis = _("Production")
xaxis = _("year")
plt.ylabel(yaxis, fontsize=20)
plt.xlabel(xaxis, fontsize=20)

所以它工作正常,但在 Y 轴和 X 轴的翻译术语的图形中,Django 对象正在运行

【问题讨论】:

    标签: python django matplotlib internationalization translation


    【解决方案1】:

    根据Lazy translation - Django documentation

    使用翻译函数的惰性版本 django.utils.translation(通过惰性后缀很容易识别 他们的名字)来懒惰地翻译字符串——当值被访问时 而不是在他们被调用时。

    这些函数存储对字符串的惰性引用——不是实际的 翻译。当字符串为 在字符串上下文中使用,例如在模板渲染中。

    ...


    替换以下行:

    from django.utils.translation import ugettext_lazy as _
    

    使用ugettext(将返回 unicode 字符串):

    from django.utils.translation import ugettext as _
    

    【讨论】:

      猜你喜欢
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 2011-12-25
      • 2012-12-29
      相关资源
      最近更新 更多