Strings won't be translated in Django using format function available in Python 2.7

前端 未结 3 2063
轮回少年
轮回少年 2021-02-18 18:52

Does the new and recommended way to format strings available in Python 2.7 using format result in a non translated string in Django?

The strings are in the

相关标签:
3条回答
  • 2021-02-18 19:01

    i had the same problem so i translated the text first and then added the dynamic content like

    title = _(u"is a good website")
    title = " ".join([website_name, title])
    

    There must be a better way to do this

    0 讨论(0)
  • 2021-02-18 19:12

    compilemessages sees the string as "{sitename} is a good website", but when the app is running the string is actually e.g. "MySite is a good website", which of course doesn't have a translation. You must translate the bare string first, and then you can perform template operations on it.

    0 讨论(0)
  • 2021-02-18 19:16

    The following should work:

    _('Foo %(x)s') % {'x': "bar"}
    

    s is the string, d is the intiger.

    0 讨论(0)
提交回复
热议问题