Django translation and gettext:弃用%(string interpolation)运算符 [英] Django translations and gettext: The deprecation of the % (string interpolation) operator

查看:213
本文介绍了Django translation and gettext:弃用%(string interpolation)运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管Django Django还不支持Python 3,但最终将会让我的代码保持更多的面向未来的可能。



自Python以来2.7字符串插值运算符()已被弃用。我意识到需要翻译的每个字符串都使用插值语法。在Django文档中,没有提到新的 str.format 方法(新的字符串格式的官方方式)...



可能是 gettext 库的限制,但我不这么认为,因为.PO文件中的字符串相同。



问题是如果我可以使用新的字符串格式方法进行翻译。



旧方式:

  class Post(models.Model):
title = models.CharField(max_length = 50)
date = models.DateField()
#...
def __unicode __(self):
return _('%(title)s (%(date)s)')%{
'title':self.title,
'date':self.date,
}

新方式:

  Post(models.Model):
title = models.CharField(max_length = 50)
date = models.DateField()
#...
def __unicode __(self)
return _('{title}({date})')。format(
title = self.title,
date = self.date,

另外, ugettext_lazy 不会真正返回字符串,而 Promises ,仅在需要时进行评估的对象。

解决方案

您可以安全使用。例如

  ugettext_lazy('{foo}')。format(foo ='bar')

Django使用的翻译程序 xgettext 不关心要翻译的内容。它只是针对 ugettext_lazy _ .py c>收集可翻译的字符串(参考 xgettext的手册 Django代码



此外,上面的 .format()方法是由代理对象提供的包装器,如: / p>

 >>> ugettext_lazy(u'{foo}')。格式
< bound方法__proxy __.__ wrapper__ of< django.utils.functional .__ proxy__ object at 0x102f19050>>

调用上述 .format() u'{foo}'转换为某些unicode ,然后调用 value.format 带实际参数。您可以看到翻译和 value.format 发生在不同的阶段。


Although Django Django does not yet support Python 3, it eventually will, so I want to keep my code the more "future-proof" possible.

Since Python 2.7 the string interpolation operator (%) is being deprecated. And I realized that every string that needs to be translated is using the % interpolation syntax. And in the Django docs there is no mention of the new str.format method (the "new" official way of string formatting)...

Maybe there is a limitation of the gettext library, but I don't think so, since the string appears identical in the .PO files.

The question is if I can use the new string format method for translation.

The old way:

class Post(models.Model):
    title = models.CharField(max_length=50)
    date = models.DateField()
    # ...
    def __unicode__(self):
        return _('%(title)s (%(date)s)') % {
            'title': self.title,
            'date': self.date,
        }

The "new" way:

class Post(models.Model):
    title = models.CharField(max_length=50)
    date = models.DateField()
    # ...
    def __unicode__(self):
        return _('{title} ({date})').format(
            title=self.title,
            date=self.date,
        )

Also, ugettext_lazy does not really return strings, but Promises, objects that are evaluated only when needed.

解决方案

You could use it safely. For example

ugettext_lazy('{foo}').format(foo='bar')

The translation program xgettext, which is used by Django, does not care about the content to be translated. It just searches .py file for keywords such as ugettext_lazy or _ to collect the translatable strings(refs the manual of xgettext and Django code)

Furthermore, the .format() method above is a wrapper provided by the proxy object, like:

>>> ugettext_lazy(u'{foo}').format
<bound method __proxy__.__wrapper__ of <django.utils.functional.__proxy__ object at 0x102f19050>>

The invoking of the above .format() would get u'{foo}' to be translated to some unicode value, then call value.format with actual arguments. You could see that the translation and value.format happen in different stages.

这篇关于Django translation and gettext:弃用%(string interpolation)运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆