代码之家  ›  专栏  ›  技术社区  ›  frequent

如何最好地转换作为变量传递给django模块的字符串?

  •  0
  • frequent  · 技术社区  · 6 年前

    我在Django做了我的第一步,并尝试将文本翻译成我要通过.in i文件传递到应用程序中的文本。

    说我的 init.ini 是:

    [test]
    GREETING = Hello World
    

    在我的 settings.py ,我正在做:

    from six.moves import configparser
    from django.utils.translation import ugettext_lazy as _
    
    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    # Retrieve initialization configuration
    init_parser = configparser.ConfigParser()
    init_parser.read(os.path.join(BASE_DIR, 'init.ini'))
    
    ...
    HELLO = init_parser.get('test', 'GREETING')
    HELLO = _(init_parser.get('test', 'GREETING'))
    

    我的标签翻译在调用时不显示 makemessage .

    这个 documentation

    (使用变量或计算值的警告,如 前两个例子是djangos翻译字符串检测 实用程序,django admin makemessages,won_t can to find these 串。更多关于makemessages的信息。)

    但是他们虽然 生成消息 在文档页面上有很多介绍,它没有提供如何转换这样的变量/计算值的解决方案。因此我的问题是:

    是否有任何推荐的将字符串作为变量传递到python模块的实用方法,并且 生成消息 抓住他们?

    编辑 以下内容:
    添加扩展名,如中所示 django-admin makemessages -l de --e=html,py,txt,ini 也不管用,但现在我很好奇我怎么能 txt 将要覆盖的文件。也许这是个主意。

    1 回复  |  直到 6 年前
        1
  •  0
  •   frequent    6 年前

    所以这个 django-admin makemessages -l de --e=html,py,txt,ini 让我走上正轨,因为如果 txt 默认包含在 documentation 它一定能起作用。

    我创造了 foo.txt 以下内容:

    _("yeah why not")
    {% trans "maybe like this" %}
    

    第二个片段出现在 .po 调用时的文件 makemessages .

    我创造了 foo.ini 以下内容:

    _("from ini, yeah why not")
    {% trans "from ini, maybe like this" %}
    

    和跑步 django admin makemessages-l de--e=html、py、txt、ini .采购订单号 也要归档。

    所以 %trans“% -初始化文件中的所有文本片段似乎都能完成这项工作。然后我可以从我的模块中获取翻译值。

    推荐文章