File indexing completed on 2024-04-28 16:51:34
0001 #!/bin/env python3 0002 0003 import json 0004 import datetime 0005 import os, sys 0006 0007 if len(sys.argv) < 3: 0008 print ("Usage ./convertpottojson input_pot_file language") 0009 sys.exit(-1) 0010 0011 potFileName = sys.argv[1] 0012 lang = sys.argv[2] 0013 path = "./extension/_locales/%s" % lang 0014 enPath = "./extension/_locales/en/messages.json" 0015 0016 os.makedirs(path, exist_ok=True) 0017 outfile = open(path + "/messages.json" , 'w') 0018 0019 translations = {} 0020 currentGroup = {} 0021 currentId = "" 0022 currentMsg = "" 0023 0024 def cleanupMessage(msg): 0025 # strip wrapping "'s 0026 msg = msg[1:-1] 0027 # unescape quotes in the pot file 0028 msg = msg.replace('\\\"', '\"') 0029 return msg 0030 0031 with open(potFileName, 'r') as infile: 0032 for line in infile.readlines(): 0033 line = line.strip() 0034 0035 if not line: 0036 if not currentId.isspace() and not currentMsg.isspace(): 0037 translations[currentId] = currentMsg 0038 currentId = "" 0039 currentMsg = "" 0040 continue 0041 if line.startswith("#:"): 0042 parts = line.split(' ', 1) 0043 if len(parts) != 2: 0044 continue 0045 currentId = parts[1].split(":")[0] 0046 elif line.startswith("msgstr"): 0047 parts = line.split(' ', 1) 0048 if len(parts) != 2: 0049 continue 0050 currentMsg = cleanupMessage(parts[1]) 0051 else: 0052 currentMsg += cleanupMessage(line) 0053 0054 0055 outTranslations = {} 0056 0057 with open(enPath, 'r') as infile: 0058 enData = json.load(infile) 0059 0060 for msgId in enData: 0061 msg = "" 0062 if msgId in translations: 0063 msg = translations[msgId] 0064 if not msg: 0065 msg = enData[msgId]["message"] 0066 outTranslations[msgId] = {"message" : msg} 0067 0068 outfile.write(json.JSONEncoder(indent=4, ensure_ascii=False, sort_keys=True).encode(outTranslations))