Last active
February 18, 2019 17:28
-
-
Save Dani4kor/c8317bd536223814aebe2989628de463 to your computer and use it in GitHub Desktop.
Python parser for creating VisualStudio Resources.resx
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/python | |
# -*- coding: utf-8 -*- | |
import re, os | |
''' | |
Parsing Translations.txt and creating Resources.resx with different languages | |
Translations format: b([0-9]+,[1-5])='String UTF-8' | |
b(1,1)='Höhe (m)' | |
b(1,2)='Height (m)' | |
b(1,3)='Altezza (m)' | |
b(1,4)='Altitude (m)' | |
b(1,5)='Высота (м)' | |
Returns VS resx format in outputfolder | |
''' | |
DeutschData, EnglishData, ItalianData, FrenchData, RussianData, writedata, id = {}, {}, {}, {}, {}, {}, [] | |
lang = ['.de-DE','','.it-IT','.fr-FR','.ru-RU'] | |
filename = "Translations.txt" | |
def createFolder(directory): | |
try: | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
except OSError: | |
print ('Error: Creating directory. ' + directory) | |
def writetofile(resName, line): | |
with open(resName, 'w', encoding='utf-8') as file: | |
for id, data in line.items(): | |
resxData = '<data name=\'' + str(id) + '\' xml:space=\'preserve\'>\n <value>' + str(data).strip() + '</value>\n</data>' | |
file.write(resxData + "\n") | |
def parseMatch(data, type): | |
if type == "match": | |
iter = re.finditer(r"(\d+)\,([^)])", data) | |
for match in iter: return (match.group(2)) | |
if type == "matchid": | |
iter = re.finditer(r"(\d+)\,([^)])", data) | |
for match in iter: return (match.group(1)) | |
if type == "strip": | |
iter = re.finditer(r"=\'([^']+)\'", data) | |
for match in iter: return (match.group(1)) | |
def main(): | |
createFolder('./output/') | |
with open(filename, encoding='utf-8') as file: | |
content = file.readlines() | |
content = filter(None, [x.strip() for x in content]) | |
for line in content: | |
if "1" == parseMatch(line, "match"): DeutschData[parseMatch(line, "matchid")] = (parseMatch(line, "strip")) | |
elif "2" == parseMatch(line, "match"): EnglishData[parseMatch(line, "matchid")] = (parseMatch(line, "strip")) | |
elif "3" == parseMatch(line, "match"): ItalianData[parseMatch(line, "matchid")] = (parseMatch(line, "strip")) | |
elif "4" == parseMatch(line, "match"): FrenchData [parseMatch(line, "matchid")] = (parseMatch(line, "strip")) | |
elif "5" == parseMatch(line, "match"): RussianData[parseMatch(line, "matchid")] = (parseMatch(line, "strip")) | |
for i in lang: | |
if i == '.de-DE': writedata = DeutschData | |
elif i == '': writedata = EnglishData | |
elif i == '.it-IT': writedata = ItalianData | |
elif i == '.fr-FR': writedata = FrenchData | |
elif i == '.ru-RU': writedata = RussianData | |
writetofile(os.path.join('./output/','myRes'+ i +'.resx'), writedata) | |
print(os.path.join('./output/','myRes'+ i +'.resx')) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment