代码之家  ›  专栏  ›  技术社区  ›  Yippie-Ki-Yay

Qt语言学家是否能够向可编辑的.ts文件添加新条目?

  •  2
  • Yippie-Ki-Yay  · 技术社区  · 15 年前

    如果没有办法做到这一点-应该怎么做(以某种方式 自动

    <message>
        <source>x</source>
        <translation>xx</translation>
    </message>
    

    我的街区 .ts 我想这不是正确的方法。

    2 回复  |  直到 12 年前
        1
  •  7
  •   Frank Osterfeld    15 年前

    不,这不是正确的方式:)使用 tr() 在代码中标记要翻译的字符串。 例如

    label->setText( tr("Error") );
    

    here 还是需要翻译源代码中没有的字符串?

        2
  •  1
  •   Tim    14 年前

    在.ts文件中使用ElementTree进行自建解析器。它并不能使代码变得漂亮 当它加上它,但我相信它工作得很好(到目前为止):

    from xml.etree import ElementTree as ET
    
    tree = ET.parse(infile)
    doc = tree.getroot()
    
    for e in tree.getiterator()
      if e.tag == "context":
        for child in e.getchildren():
          if child.tag == "name" and child.text == target:
            elem = ET.SubElement(e, "message")
            src = ET.SubElement(elem, "source")
            src.text = newtext
            trans = ET.SubElement(elem, "translation")
            trans.text = "THE_TRANSLATION"
    
    tree.write(outfile)
    

    目标是要在其中添加新消息的上下文, 新文本当然是新的源文本。