QT_TRANSLATE_NOOP 与 pylupdate 消歧

QT_TRANSLATE_NOOP disambiguation with pylupdate

我正在尝试使用 pylupdate 创建翻译文件。

import sys
from PyQt5 import QtCore
from PyQT5.QtCore import QT_TRANSLATE_NOOP
_translate = QtCore.QCoreApplication.translate


if __name__ == '__main__':
app = QApplication(sys.argv)
qt_translator = QtCore.QTranslator(app)
list_translate = [
    QT_TRANSLATE_NOOP("test", "fake"), 
    QT_TRANSLATE_NOOP("test", "thing"), 
    QT_TRANSLATE_NOOP("test", "something")
]
for item in list_translate:
    _translate("test", item, "1")
_translate("test", "other thing")

然后从 我找到了 QT_TRANSLATE_NOOP。但我无法弄清楚如何让 pylupdate 继续使用消歧注释。每次我 运行 它,它都会将我文件中的那个标记为过时并创建一个新条目。有没有办法在文字中指定注释?

.ts 开始:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS><TS version="2.0">
<context>
    <name>test</name>
    <message>
        <location filename="test.py" line="17"/>
        <source>other thing</source>
        <translation>tran</translation>
    </message>
    <message>
        <location filename="test.py" line="11"/>
        <source>fake</source>
        <comment>1</comment>
        <translation>slate</translation>
    </message>
    <message>
        <location filename="test.py" line="12"/>
        <source>thing</source>
        <translation>something</translation>
    </message>
    <message>
        <location filename="test.py" line="13"/>
        <source>something</source>
        <translation>still something</translation>
    </message>
</context>
</TS>

.ts 文件在 运行ning

之后
pylupdate5 -verbose test.py -ts translate/test.ts

通知第 14 行:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS><TS version="2.0">
<context>
    <name>test</name>
    <message>
        <location filename="test.py" line="17"/>
        <source>other thing</source>
        <translation>tran</translation>
    </message>
    <message>
        <location filename="test.py" line="11"/>
        <source>fake</source>
        <comment>1</comment>
        **<translation type="obsolete">slate</translation>**
    </message>
    <message>
        <location filename="test.py" line="12"/>
        <source>thing</source>
        <translation>something</translation>
    </message>
    <message>
        <location filename="test.py" line="13"/>
        <source>something</source>
        <translation>still something</translation>
    </message>
    <message>
        <location filename="test.py" line="11"/>
        <source>fake</source>
        <translation type="unfinished">slate</translation>
    </message>
</context>
</TS>

PyQt4 和 PyQt5 都缺失了 QT_TRANSLATE_NOOP3, which provides a third argument which takes a comment. This is obviously a bug (which should be reported on the pyqt mailing list),但它很容易变通。幸运的是,pylupdate 将正确解析三参数 QT_TRANSLATE_NOOP,即使 PyQt 本身不提供实际函数。

下面是一个工作演示。 .ts 文件需要先编译使用:

lrelease-qt5 test.ts -qm test.qm

test.py 文件:

import sys
from PyQt5.QtCore import QCoreApplication, QTranslator
_translate = QCoreApplication.translate

def QT_TRANSLATE_NOOP(context, source, comment=None):
    return source

if __name__ == '__main__':

    app = QCoreApplication(sys.argv)
    translator = QTranslator(app)
    translator.load('test.qm')
    app.installTranslator(translator)

    strings = [
        QT_TRANSLATE_NOOP('test', 'object', 'thing'),
        QT_TRANSLATE_NOOP('test', 'object', 'purpose'),
        QT_TRANSLATE_NOOP('test', 'object', 'disagree')
        ]

    print('object/thing:', _translate('test', strings[0], 'thing'))
    print('object/purpose:', _translate('test', strings[1], 'purpose'))
    print('object/disagree:', _translate('test', strings[2], 'disagree'))

test.ts 文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS><TS version="2.0" language="fr_FR" sourcelanguage="en_GB">
<context>
    <name>test</name>
    <message>
        <location filename="test.py" line="16"/>
        <source>object</source>
        <comment>thing</comment>
        <translation>objet</translation>
    </message>
    <message>
        <location filename="test.py" line="17"/>
        <source>object</source>
        <comment>purpose</comment>
        <translation>objectif</translation>
    </message>
    <message>
        <location filename="test.py" line="18"/>
        <source>object</source>
        <comment>disagree</comment>
        <translation>objecter</translation>
    </message>
</context>
</TS>