diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-08-30 14:54:45 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-08-30 16:18:43 +0000 |
commit | 3f7108cfa0abbce35d35352212f9d0a12312963d (patch) | |
tree | be63c223bb4f55a44d4ca35c03765d6cdbadf1d1 | |
parent | a2fdf1732f03a54102a667cb5f9d809ba48fe4c5 (diff) |
lupdate/Python: Add support for extra comments and ids
Fixes: PYSIDE-1376
Task-number: PYSIDE-1252
Change-Id: Ie60f86a6c5a771bcdd2ba455fb86cde3cb8f75e6
Reviewed-by: Christian Tismer <tismer@stackless.com>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
(cherry picked from commit 5217a8553f41d43c21755667f7f05064209ee8f3)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
3 files changed, 55 insertions, 11 deletions
diff --git a/src/linguist/lupdate/python.cpp b/src/linguist/lupdate/python.cpp index d6a4065f7..d134d3187 100644 --- a/src/linguist/lupdate/python.cpp +++ b/src/linguist/lupdate/python.cpp @@ -70,6 +70,9 @@ static int yyParenDepth; static int yyLineNo; static int yyCurLineNo; +static QByteArray extraComment; +static QByteArray id; + // the file to read from (if reading from a file) static FILE *yyInFile; @@ -267,6 +270,18 @@ static Token parseString() return Tok_String; } +static QByteArray readLine() +{ + QByteArray result; + while (true) { + yyCh = getChar(); + if (yyCh == EOF || yyCh == '\n') + break; + result.append(char(yyCh)); + } + return result; +} + static Token getToken() { yyIdentLen = 0; @@ -337,10 +352,22 @@ static Token getToken() } switch (yyCh) { case '#': - yyCh = getChar(); - do { - yyCh = getChar(); - } while (yyCh != EOF && yyCh != '\n'); + switch (getChar()) { + case ':': + extraComment = readLine().trimmed(); + break; + case '=': + id = readLine().trimmed(); + break; + case EOF: + case '\n': + break; + default: + do { + yyCh = getChar(); + } while (yyCh != EOF && yyCh != '\n'); + break; + } break; case '"': case '\'': @@ -564,6 +591,18 @@ static bool parseTranslate(QByteArray *text, QByteArray *context, QByteArray *co return true; } +static inline void setMessageParameters(TranslatorMessage *message) +{ + if (!extraComment.isEmpty()) { + message->setExtraComment(QString::fromUtf8(extraComment)); + extraComment.clear(); + } + if (!id.isEmpty()) { + message->setId(QString::fromUtf8(id)); + id.clear(); + } +} + static void parse(Translator &tor, ConversionData &cd, const QByteArray &initialContext = {}, const QByteArray &defaultContext = {}) @@ -627,6 +666,7 @@ static void parse(Translator &tor, ConversionData &cd, QString::fromUtf8(comment), {}, yyFileName, yyLineNo, {}, TranslatorMessage::Unfinished, plural); + setMessageParameters(&message); tor.extend(message, cd); } } @@ -640,6 +680,7 @@ static void parse(Translator &tor, ConversionData &cd, QString::fromUtf8(comment), {}, yyFileName, yyLineNo, {}, TranslatorMessage::Unfinished, plural); + setMessageParameters(&message); tor.extend(message, cd); } } diff --git a/tests/auto/linguist/lupdate/testdata/good/parsepython/main.py b/tests/auto/linguist/lupdate/testdata/good/parsepython/main.py index 0e6b60c5b..ec7ecf552 100644 --- a/tests/auto/linguist/lupdate/testdata/good/parsepython/main.py +++ b/tests/auto/linguist/lupdate/testdata/good/parsepython/main.py @@ -13,7 +13,9 @@ import linguist_rc class Window(QMainWindow): def __init__(self): super().__init__() + #: File menu file_menu = self.menuBar().addMenu(self.tr("&File")) + #= quit_id quit_action = file_menu.addAction(self.tr("Quit")) quit_action.setShortcut(self.tr("CTRL+Q")) quit_action.triggered.connect(self.close) diff --git a/tests/auto/linguist/lupdate/testdata/good/parsepython/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/parsepython/project.ts.result index ef3ecaeca..629b0a72b 100644 --- a/tests/auto/linguist/lupdate/testdata/good/parsepython/project.ts.result +++ b/tests/auto/linguist/lupdate/testdata/good/parsepython/project.ts.result @@ -4,32 +4,33 @@ <context> <name>Window</name> <message> - <location filename="main.py" line="16"/> + <location filename="main.py" line="17"/> <source>&File</source> + <extracomment>File menu</extracomment> <translation type="unfinished"></translation> </message> - <message> - <location filename="main.py" line="17"/> + <message id="quit_id"> + <location filename="main.py" line="19"/> <source>Quit</source> <translation type="unfinished"></translation> </message> <message> - <location filename="main.py" line="18"/> + <location filename="main.py" line="20"/> <source>CTRL+Q</source> <translation type="unfinished"></translation> </message> <message> - <location filename="main.py" line="20"/> + <location filename="main.py" line="22"/> <source>&Help</source> <translation type="unfinished"></translation> </message> <message> - <location filename="main.py" line="21"/> + <location filename="main.py" line="23"/> <source>About Qt</source> <translation type="unfinished"></translation> </message> <message numerus="yes"> - <location filename="main.py" line="35"/> + <location filename="main.py" line="37"/> <source>%n language(s) selected</source> <translation type="unfinished"> <numerusform></numerusform> |