1

我正在尝试使用 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文件

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>
4

1 回答 1

1

PyQt4 和 PyQt5 都缺少QT_TRANSLATE_NOOP3,它提供了带有注释的第三个参数。这显然是一个错误(应该在pyqt 邮件列表中报告),但它很容易解决。幸运的是,即使 PyQt 本身不提供实际函数,它pylupdate也会正确解析三参数。QT_TRANSLATE_NOOP

下面是一个工作演示。.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>
于 2017-12-23T01:31:42.363 回答