I am building a translation tool for my app. I have 2 different json files (en.json, fr.json), where my keys and values are.
The en.json file is the referent file which means the keys present in this file need to be added in the fr.json file if they do not exist . For more details, if the key is present in the en.json and not in the fr.json we need to display but if the key is only present in the fr.json, it does not count and we display nothing on the screen. And if the key is already present in both of them we don’t need to display something on the screen.
The en.json file is the referent file with the following keys :
{
"i18n-outterJSON": {
"i18n-value1": "value1",
"i18n-value2": "value2",
"i18n-dad" : "dady"
},
"InnerJSON2":"NoneValue",
"hello" : "good morning",
"onlykey" : "only key",
"secondekey" : "second key",
"mother": {
"daugther": "fille"
}
}
The fr.json file is the Target file. With the following keys:
{
"i18n-outterJSON": {
"i18n-value1": "value1",
"i18n-value2": "value2"
},
"i18n-hello" : "bonjour",
"i18n-variation" : "variation",
"alouette" : "cacahouete",
"InnerJSON2":"pas de valeur",
"hello" : "bonjour"
}
My code in golang is :
fileEnglish := GetAllPathRefFile(); // to open the reference file
files := GetAllI18nFiles(); // to open the french file
for _, fileRef := range fileEnglish {
mapRef := ReadJsonFile(constants.PathRefFile + GetRidOfExtension(fileRef.Name()))
for _, fileTarget := range files {
mapTarget := ReadJsonFile(constants.I18nPath + GetRidOfExtension(fileTarget.Name()));
mapTarget = MissingKey(mapRef, mapTarget)
// range loop to identify the missing keys in the different files
for key, _ := range mapRef { // mapRef is the reference map of the reference file en.json
if mapRef[key] != mapTarget[key] { // mapTarget is map of the target file fr.json
t := strings.Replace(key, key, "", -1)
_ = t
fmt.Println("the missing keys are: ", key)
}
}
}
}
I tried to compare each key and in some cases we have a hierarchy and I tried to identify the missing keys. I need to obtain this result :
i18n-dad
mother
daughter
onlykey
secondekey
But with my current code I have this error : comparing uncomparable type map[string]interface {}
.
How can we identify and print on the screen correctly the keys?