Unit - Fahrenheit support in language translations

°F Fahrenheit - You will find the default Fahrenheit configuration in the translation_xx.json
 If tempUnitFahrenheit is set to:
   true -  you can switch in menu settings to Fahrenheit or Celsius.
   false - you see only Celsius. All settings are then is in Celsius only.
This commit is contained in:
GeminiServer
2020-03-20 20:37:02 +01:00
parent 797d2c247a
commit ee0767efb8
13 changed files with 168 additions and 8265 deletions

View File

@@ -11,6 +11,7 @@ import re
import subprocess
TRANSLATION_CPP = "Translation.cpp"
UNIT_H = "unit.h"
try:
to_unicode = unicode
@@ -32,6 +33,7 @@ def loadJson(fileName, skipFirstLine):
# Reading all language translations into a dictionary by langCode
def readTranslations(jsonDir):
langDict = {}
UnitDict = {}
# Read all translation files from the input dir
for fileName in os.listdir(jsonDir):
@@ -56,14 +58,19 @@ def readTranslations(jsonDir):
except KeyError:
langCodeFromJson = "(missing)"
try:
TempUnitF_FromJson = lang['tempUnitFahrenheit']
except KeyError:
TempUnitF_FromJson = True # Default to true.
# ...cause they should be the same!
if langCode != langCodeFromJson:
raise ValueError("Invalid languageCode " + langCodeFromJson +
" in file " + fileName)
langDict[langCode] = lang
return langDict
UnitDict[langCode] = TempUnitF_FromJson
return langDict, UnitDict
def writeStart(f):
@@ -78,6 +85,23 @@ def writeStart(f):
"""))
def writeStartUnit(f):
f.write(
to_unicode(
"""// WARNING: THIS FILE WAS AUTO GENERATED BY make_translation.py. PLEASE DO NOT EDIT.
/**
* °F Fahrenheit Support
* You will find the default Fahrenheit configuration in the translation_xx.json
* If tempUnitFahrenheit is set to:
* true - you can switch in menu settings to Fahrenheit or Celsius.
* false - you see only Celsius. All settings are then is in Celsius only.
*/
#ifndef _UNIT_H
#define _UNIT_H\n
"""))
def escapeC(s):
return s.replace("\"", "\\\"")
@@ -453,6 +477,22 @@ def writeLanguage(languageCode, defs, f):
# ----- Block end
f.write(to_unicode("#endif\n"))
def writeUnit(languageCode, defs, f, UnitCodes):
print("Generating unit block for " + languageCode)
lang = langDict[languageCode]
unit = UnitDict[UnitCodes]
try:
langName = lang['languageLocalName']
except KeyError:
langName = languageCode
f.write(to_unicode(" #ifdef LANG_" + languageCode + "\n"))
if unit:
f.write(to_unicode(" #define ENABLED_FAHRENHEIT_SUPPORT" + "\n"))
else: f.write(to_unicode(" //#define ENABLED_FAHRENHEIT_SUPPORT" + "\n"))
# ----- Block end
f.write(to_unicode(" #endif /* ---- " + langName + " ---- */\n"))
def readVersion():
with open(os.path.relpath(jsonDir +
"/../workspace/TS100/version.h"),"r") as version_file:
@@ -473,7 +513,8 @@ def readVersion():
def read_opts():
""" Reading input parameters
First parameter = json directory
Second parameter = target directory
Second parameter = translation directory
Third paramter = unit directory
"""
if len(sys.argv) > 1:
jsonDir = sys.argv[1]
@@ -481,15 +522,21 @@ def read_opts():
jsonDir = "."
if len(sys.argv) > 2:
outFile = sys.argv[2]
outFileTranslationCPP = sys.argv[2]
else:
outDir = os.path.relpath(jsonDir + "/../workspace/TS100/Core/Src")
outFile = os.path.join(outDir, TRANSLATION_CPP)
outFileTranslationCPP = os.path.join(outDir, TRANSLATION_CPP)
if len(sys.argv) > 3:
outFileUnitH = sys.argv[3]
else:
outDir = os.path.relpath(jsonDir + "/../workspace/TS100/Core/Inc")
outFileUnitH = os.path.join(outDir,UNIT_H)
if len(sys.argv) > 4:
raise Exception("Too many parameters!")
return jsonDir, outFile
return jsonDir, outFileTranslationCPP, outFileUnitH
def orderOutput(langDict):
@@ -507,17 +554,22 @@ def orderOutput(langDict):
return mandatoryOrder
def writeTarget(outFile, defs, langCodes):
def writeTarget(outFileTranslationCPP, outFileUnitH, defs, langCodes, UnitCodes):
# Start writing the file
with io.open(outFile, 'w', encoding='utf-8', newline="\n") as f:
with io.open(outFileTranslationCPP, 'w', encoding='utf-8', newline="\n") as f:
writeStart(f)
for langCode in langCodes:
writeLanguage(langCode, defs, f)
with io.open(outFileUnitH, 'w', encoding='utf-8', newline="\n") as f:
writeStartUnit(f)
for langCode, UnitCode in zip(langCodes, UnitCodes):
writeUnit(langCode, defs, f, UnitCode)
f.write(to_unicode("\n#endif /* _UNIT_H */\n"))
if __name__ == "__main__":
try:
jsonDir, outFile = read_opts()
jsonDir, outFileTranslationCPP, outFileUnitH = read_opts()
except:
print("usage: make_translation.py {json dir} {cpp dir}")
sys.exit(1)
@@ -526,11 +578,13 @@ if __name__ == "__main__":
except: print("error: could not get/extract build version"); sys.exit(1)
print("Build version: " + buildVersion)
print("Making " + outFile + " from " + jsonDir)
print("Making " + outFileTranslationCPP + " from " + jsonDir)
print("Making " + outFileUnitH + " from " + jsonDir)
langDict = readTranslations(jsonDir)
langDict, UnitDict = readTranslations(jsonDir)
defs = loadJson(os.path.join(jsonDir, "translations_def.js"), True)
langCodes = orderOutput(langDict)
writeTarget(outFile, defs, langCodes)
UnitCodes = orderOutput(UnitDict)
writeTarget(outFileTranslationCPP, outFileUnitH, defs, langCodes, UnitCodes)
print("Done")