diff options
author | Ben V. Brown <[email protected]> | 2019-12-28 11:59:43 +1100 |
---|---|---|
committer | Ben V. Brown <[email protected]> | 2019-12-28 11:59:43 +1100 |
commit | 97f0dcae361a7fb3eb3f5a9767f2e5d71a22a1ef (patch) | |
tree | 987be4a12ec04f8dcdd15d67f494e3a5404699e3 | |
parent | 2d5482ecd241a063836bc9d38e9397c083b32b7c (diff) | |
download | IronOS-97f0dcae361a7fb3eb3f5a9767f2e5d71a22a1ef.tar.gz IronOS-97f0dcae361a7fb3eb3f5a9767f2e5d71a22a1ef.zip |
Cleanup generator code to handle \n in messages
-rwxr-xr-x | Translation Editor/make_translation.py | 126 |
1 files changed, 68 insertions, 58 deletions
diff --git a/Translation Editor/make_translation.py b/Translation Editor/make_translation.py index 304353dd..72d89978 100755 --- a/Translation Editor/make_translation.py +++ b/Translation Editor/make_translation.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3
-#coding=utf-8
+# coding=utf-8
from __future__ import print_function
import json
import os
@@ -81,60 +81,64 @@ def escapeC(s): def getConstants():
# Extra constants that are used in the firmware that are shared across all languages
- consants =[]
- consants.append(('SymbolPlus','+'))
- consants.append(('SymbolMinus','-'))
- consants.append(('SymbolSpace',' '))
- consants.append(('SymbolDot','.'))
- consants.append(('SymbolDegC','C'))
- consants.append(('SymbolDegF','F'))
- consants.append(('SymbolMinutes','M'))
- consants.append(('SymbolSeconds','S'))
- consants.append(('SymbolWatts','W'))
- consants.append(('SymbolVolts','V'))
- consants.append(('SymbolDC','DC'))
- consants.append(('SymbolCellCount','S'))
- consants.append(('SymbolVersionNumber','V2.06'))
+ consants = []
+ consants.append(('SymbolPlus', '+'))
+ consants.append(('SymbolMinus', '-'))
+ consants.append(('SymbolSpace', ' '))
+ consants.append(('SymbolDot', '.'))
+ consants.append(('SymbolDegC', 'C'))
+ consants.append(('SymbolDegF', 'F'))
+ consants.append(('SymbolMinutes', 'M'))
+ consants.append(('SymbolSeconds', 'S'))
+ consants.append(('SymbolWatts', 'W'))
+ consants.append(('SymbolVolts', 'V'))
+ consants.append(('SymbolDC', 'DC'))
+ consants.append(('SymbolCellCount', 'S'))
+ consants.append(('SymbolVersionNumber', 'V2.06'))
return consants
-def getTipModelEnumTS80():
+
+
+def getTipModelEnumTS80():
constants = []
constants.append("B02")
constants.append("D25")
- constants.append("TS80") # end of miniware
- constants.append("User") # User
+ constants.append("TS80") # end of miniware
+ constants.append("User") # User
return constants
-def getTipModelEnumTS100():
+
+def getTipModelEnumTS100():
constants = []
constants.append("B02")
constants.append("D24")
constants.append("BC2")
constants.append(" C1")
- constants.append("TS100")# end of miniware
+ constants.append("TS100") # end of miniware
constants.append("BC2")
- constants.append("Hakko")# end of hakko
+ constants.append("Hakko") # end of hakko
constants.append("User")
return constants
-def getDebugMenu():
+
+def getDebugMenu():
constants = []
constants.append(datetime.today().strftime('%d-%m-%y'))
constants.append("HW G ")
constants.append("HW M ")
- constants.append("HW P ")
+ constants.append("HW P ")
constants.append("Time ")
constants.append("Move ")
constants.append("RTip ")
constants.append("CTip ")
constants.append("CHan ")
constants.append("Vin ")
- constants.append("PCB ") # PCB Version AKA IMU version
+ constants.append("PCB ") # PCB Version AKA IMU version
return constants
def getLetterCounts(defs, lang):
textList = []
- #iterate over all strings
+ # iterate over all strings
obj = lang['menuOptions']
for mod in defs['menuOptions']:
eid = mod['id']
@@ -173,13 +177,13 @@ def getLetterCounts(defs, lang): for mod in defs['menuGroups']:
eid = mod['id']
textList.append(obj[eid]['desc'])
- constants = getConstants()
+ constants = getConstants()
for x in constants:
textList.append(x[1])
textList.extend(getTipModelEnumTS100())
- textList.extend(getTipModelEnumTS80())
+ textList.extend(getTipModelEnumTS80())
textList.extend(getDebugMenu())
-
+
# collapse all strings down into the composite letters and store totals for these
symbolCounts = {}
@@ -187,26 +191,25 @@ def getLetterCounts(defs, lang): line = line.replace('\n', '').replace('\r', '')
line = line.replace('\\n', '').replace('\\r', '')
if len(line):
- #print(line)
+ # print(line)
for letter in line:
symbolCounts[letter] = symbolCounts.get(letter, 0) + 1
symbolCounts = sorted(
symbolCounts.items(),
- key=lambda kv: (kv[1],kv[0])) # swap to Big -> little sort order
+ key=lambda kv: (kv[1], kv[0])) # swap to Big -> little sort order
symbolCounts = list(map(lambda x: x[0], symbolCounts))
symbolCounts.reverse()
return symbolCounts
-
def getFontMapAndTable(textList):
# the text list is sorted
# allocate out these in their order as number codes
symbolMap = {}
- symbolMap['\n'] = '\\x01'
+ symbolMap['\n'] = '\\x01' # Force insert the newline char
index = 2 # start at 2, as 0= null terminator,1 = new line
forcedFirstSymbols = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
- #enforce numbers are first
+ # enforce numbers are first
for sym in forcedFirstSymbols:
symbolMap[sym] = "\\x%0.2X" % index
index = index + 1
@@ -229,12 +232,14 @@ def getFontMapAndTable(textList): print('Missing Large font element for {}'.format(sym))
exit(1)
fontLine = fontTable[sym]
- fontTableStrings.append(fontLine + "//{} -> {}".format(symbolMap[sym],sym))
+ fontTableStrings.append(
+ fontLine + "//{} -> {}".format(symbolMap[sym], sym))
if sym not in fontSmallTable:
print('Missing Small font element for {}'.format(sym))
exit(1)
fontLine = fontSmallTable[sym]
- fontSmallTableStrings.append(fontLine + "//{} -> {}".format(symbolMap[sym],sym))
+ fontSmallTableStrings.append(
+ fontLine + "//{} -> {}".format(symbolMap[sym], sym))
for sym in textList:
if sym not in fontTable:
@@ -242,12 +247,14 @@ def getFontMapAndTable(textList): exit(1)
if sym not in forcedFirstSymbols:
fontLine = fontTable[sym]
- fontTableStrings.append(fontLine + "//{} -> {}".format(symbolMap[sym],sym))
+ fontTableStrings.append(
+ fontLine + "//{} -> {}".format(symbolMap[sym], sym))
if sym not in fontSmallTable:
print('Missing Small font element for {}'.format(sym))
exit(1)
fontLine = fontSmallTable[sym]
- fontSmallTableStrings.append(fontLine + "//{} -> {}".format(symbolMap[sym],sym))
+ fontSmallTableStrings.append(
+ fontLine + "//{} -> {}".format(symbolMap[sym], sym))
outputTable = "const uint8_t USER_FONT_12[] = {" + to_unicode("\n")
for line in fontTableStrings:
# join font table int one large string
@@ -265,7 +272,7 @@ def getFontMapAndTable(textList): def convStr(symbolConversionTable, text):
# convert all of the symbols from the string into escapes for their content
outputString = ""
- for c in text.replace('\\r', '').replace('\\n','\n'):
+ for c in text.replace('\\r', '').replace('\\n', '\n'):
if c not in symbolConversionTable:
print('Missing font definition for {}'.format(c))
else:
@@ -276,7 +283,7 @@ def convStr(symbolConversionTable, text): def writeLanguage(languageCode, defs, f):
print("Generating block for " + languageCode)
lang = langDict[languageCode]
- #Iterate over all of the text to build up the symbols & counts
+ # Iterate over all of the text to build up the symbols & counts
textList = getLetterCounts(defs, lang)
# From the letter counts, need to make a symbol translator & write out the font
(fontTableText, symbolConversionTable) = getFontMapAndTable(textList)
@@ -290,7 +297,6 @@ def writeLanguage(languageCode, defs, f): f.write(to_unicode("// ---- " + langName + " ----\n\n"))
-
# ----- Writing SettingsDescriptions
obj = lang['menuOptions']
f.write(to_unicode("const char* SettingsDescriptions[] = {\n"))
@@ -316,14 +322,15 @@ def writeLanguage(languageCode, defs, f): for mod in defs['messages']:
eid = mod['id']
- if eid not in obj:
- f.write(
+ sourceText = ""
+ if 'default' in mod:
+ sourceText = (mod['default'])
+ if eid in obj:
+ sourceText = (obj[eid])
+ translatedText = convStr(symbolConversionTable, sourceText)
+ f.write(
to_unicode("const char* " + eid + " = \"" +
- convStr(symbolConversionTable, (mod['default'])) + "\";"+ "//{} \n".format(mod['default'])))
- else:
- f.write(
- to_unicode("const char* " + eid + " = \"" +
- convStr(symbolConversionTable, (obj[eid])) + "\";"+ "//{} \n".format(obj[eid])))
+ translatedText + "\";" + "//{} \n".format(sourceText.replace('\n', '_'))))
f.write(to_unicode("\n"))
@@ -335,7 +342,7 @@ def writeLanguage(languageCode, defs, f): eid = mod['id']
f.write(
to_unicode("const char* " + eid + " = \"" +
- convStr(symbolConversionTable, obj[eid]) + "\";"+ "//{} \n".format(obj[eid])))
+ convStr(symbolConversionTable, obj[eid]) + "\";" + "//{} \n".format(obj[eid])))
f.write(to_unicode("\n"))
@@ -344,7 +351,7 @@ def writeLanguage(languageCode, defs, f): for x in constants:
f.write(
to_unicode("const char* " + x[0] + " = \"" +
- convStr(symbolConversionTable, x[1]) + "\";"+ "//{} \n".format(x[1])))
+ convStr(symbolConversionTable, x[1]) + "\";" + "//{} \n".format(x[1])))
f.write(to_unicode("\n"))
# Write out tip model strings
@@ -352,19 +359,22 @@ def writeLanguage(languageCode, defs, f): f.write(to_unicode("const char* TipModelStrings[] = {\n"))
f.write(to_unicode("#ifdef MODEL_TS100\n"))
for c in getTipModelEnumTS100():
- f.write(to_unicode("\t \"" + convStr(symbolConversionTable, c) + "\","+ "//{} \n".format(c)))
+ f.write(to_unicode("\t \"" + convStr(symbolConversionTable,
+ c) + "\"," + "//{} \n".format(c)))
f.write(to_unicode("#else\n"))
for c in getTipModelEnumTS80():
- f.write(to_unicode("\t \"" + convStr(symbolConversionTable, c) + "\","+ "//{} \n".format(c)))
+ f.write(to_unicode("\t \"" + convStr(symbolConversionTable,
+ c) + "\"," + "//{} \n".format(c)))
f.write(to_unicode("#endif\n"))
f.write(to_unicode("};\n\n"))
-
+
# Debug Menu
f.write(to_unicode("const char* DebugMenu[] = {\n"))
-
+
for c in getDebugMenu():
- f.write(to_unicode("\t \"" + convStr(symbolConversionTable, c) + "\","+ "//{} \n".format(c)))
+ f.write(to_unicode("\t \"" + convStr(symbolConversionTable,
+ c) + "\"," + "//{} \n".format(c)))
f.write(to_unicode("};\n\n"))
# ----- Menu Options
@@ -392,12 +402,12 @@ def writeLanguage(languageCode, defs, f): convStr(symbolConversionTable, (obj[eid]['text2'][0])) +
"\", \"" +
convStr(symbolConversionTable, (obj[eid]['text2'][1])) +
- "\" },"+ "//{} \n".format(obj[eid]['text2'])))
+ "\" }," + "//{} \n".format(obj[eid]['text2'])))
else:
f.write(
to_unicode("{ \"" +
convStr(symbolConversionTable, (obj[eid]['text'])) +
- "\" },"+ "//{} \n".format(obj[eid]['text'])))
+ "\" }," + "//{} \n".format(obj[eid]['text'])))
if 'feature' in mod:
f.write(to_unicode("#endif\n"))
@@ -416,7 +426,7 @@ def writeLanguage(languageCode, defs, f): f.write(
to_unicode("\"" +
convStr(symbolConversionTable, (obj[eid]['text2'][0]) +
- "\\n" + obj[eid]['text2'][1]) + "\","+ "//{} \n".format(obj[eid]['text2'])))
+ "\\n" + obj[eid]['text2'][1]) + "\"," + "//{} \n".format(obj[eid]['text2'])))
f.write(to_unicode("};\n\n"))
@@ -433,7 +443,7 @@ def writeLanguage(languageCode, defs, f): f.write(
to_unicode("\"" +
convStr(symbolConversionTable, (obj[eid]['desc'])) +
- "\","+ "//{} \n".format(obj[eid]['desc'])))
+ "\"," + "//{} \n".format(obj[eid]['desc'])))
f.write(to_unicode("};\n\n"))
|