I want to introduce i18n in an existing application. As a first step, I need to replace the string literals with tr("something").
I have created the following regex to extract the strings:
(?<!#include)"([^"\\]|\\.)*"
However, this does not work. Without the negative lookbehind, it does match strings correctly, but also quoted includes which I want to avoid. I’m not quite familiar with negative lookbehinds, I just looked them up here.
Example:
#include "hello.h" // should NOT match "\"hello.h\""
printf("Hello"); // should match "\"Hello\""
cout << "hello" << "hello" << "hello"; // should match each three "\"hello\""'s
How can I do this correctly?
Ok, here is my solution (sort of):
First, I could not do it in Qt Creator, its regex capabilities seem to be not so good at the moment. So I used vim.
I opened all the affected files as vim buffers, then recorded the following macro:
This does the following:
The regex is vim-regex with \v that makes more natural (less escaping). In short, it uses a negative lookbehind as I originally wanted, with a slightly different syntax.
Now, I only had to run this macro for as many buffers I had open (in command mode):
The problem (and this is why it’s only a “sort of” solution) is that whenever vim encounters a buffer where the regex fails (i.e. no string literals in the file) it stops repeating the macro. I couldn’t find out how to fix that. Fortunately I had only a few files like that, so I could get away with manually re-running the above command to start repeating the macro again.