Question

Qt has special functions for localization call. Functions like qsTr(), tr(), QT_TR_NOOP(), qsTranslate(), etc.

I need to parse the content of above functions` calls params.

For example, qsTr():

qsTr(string source, string disambiguation, int n)

There are just 3 possible calls for that function

qsTr("source")
qsTr("source", "disambiguation")
qsTr("source", "disambiguation", count) // count - some int value for source 
                                        // if it contains plurals - %n

Let's assume we found in source Qt or QML this line:

qsTr("source string")

For such a case, I have written a Java regex:

(?<=qsTr\\()(\\s*(\\".*?(?<\\\\)\\")?)(?=\\s*\\))

Above regex will exactly match "source string" and that is correct.

But I need a DOTALL regex, not just for single line.

One of possible problems is that we can find next call that is incorrect and we should ignore it:

qsTr("source", count)

The above regex will fail because of greedy quantifiers. It will look for next "\\s*)" down the whole text. Any ideas how to fix that?

Était-ce utile?

La solution

Actually it's possible to reach content between quotes and then seeking for ).

Pattern will look like

Pattern.compile("\\qsTr?\\s*\\(\\s*(((\".*?(?<!\\\\)\")|('.*?(?<!\\\\)')).*?)(?=\\))", Pattern.DOTALL);

It guarantees if content wrapped by qsTr() starts from " it will definetely be extracted. For example if you to parse next string:

qsTr("source", count)

you'll get next result:

"source", count

And then you can check for validity of extracted string (for example, if it is possible for qsTr function to contain another params after static string param).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top