Created
January 19, 2021 16:34
-
-
Save dorianim/e0f251e216f4bcb9303018aa6c0ad6ab to your computer and use it in GitHub Desktop.
A small python script to switch a Qt .cpp file from "normal" to ID-based translation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import subprocess | |
inFile = "./frontend/linboosselectionrow.cpp" | |
with open(inFile, "rt") as fin: | |
with open("tmp.cpp", "wt") as fout: | |
translationId = "" | |
translationIdPrefix = "" | |
for line in fin: | |
if "//=" in line: | |
translationId = line.split("//= ")[1].replace("\n", "") | |
translationIdPrefix = line.split("//= ")[0] | |
continue | |
if translationId: | |
# We need to recreate this line and the previous one! | |
thisLineUntilString = line.split("tr(")[1] | |
defaultTranslation = thisLineUntilString.split(")")[0] | |
previousLine = translationIdPrefix + "//% " + defaultTranslation + "\n" | |
print(previousLine.replace("\n", "")) | |
fout.write(previousLine) | |
thisLineEndArray = thisLineUntilString.split(")") | |
thisLineEndArray.pop(0) | |
thisLineEnd = ")".join(thisLineEndArray) | |
thisLine = line.split("tr(")[0] + "qtTrId(\"" + translationId + "\")" + thisLineEnd | |
print(thisLine.replace("\n", "")) | |
fout.write(thisLine) | |
translationId = "" | |
continue | |
fout.write(line) | |
process = subprocess.Popen(["rm", inFile], stdout=subprocess.PIPE) | |
output, error = process.communicate() | |
process = subprocess.Popen(["mv", "tmp.cpp", inFile], stdout=subprocess.PIPE) | |
output, error = process.communicate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment