Skip to content

Instantly share code, notes, and snippets.

@BNPrashanth
Last active April 15, 2019 16:38
Show Gist options
  • Save BNPrashanth/84db99ee8b8fbc34f1b5cd1eb168cd1f to your computer and use it in GitHub Desktop.
Save BNPrashanth/84db99ee8b8fbc34f1b5cd1eb168cd1f to your computer and use it in GitHub Desktop.
func WriteLanguageFiles(csvFilePath string) *u.ErrorResponse {
csvFile, err := os.Open(csvFilePath)
if err != nil {
u.ErrorLogger.Println("Cannot open file:"+csvFilePath, err)
return u.ReturnErrorResponse(err, "Cannot open file:"+csvFilePath)
}
csvFileContent, err := csv.NewReader(csvFile).ReadAll()
for i, lang := range csvFileContent[0][1:] {
absPath, err := filepath.Abs(outputPath + lang + ".json")
if err != nil {
u.ErrorLogger.Println("Cannot get path specified: \""+lang+".json\"", err)
return u.ReturnErrorResponse(err, "Cannot get path specified: \""+lang+".json\"")
}
file, err := os.OpenFile(absPath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
u.ErrorLogger.Println("Cannot open file: \""+lang+".json\"", err)
return u.ReturnErrorResponse(err, "Cannot open file: \""+lang+".json\"")
}
file.Truncate(0)
mapLn := map[string]string{}
u.GeneralLogger.Println("Language:", lang, i)
for j, row := range csvFileContent[1:] {
// fmt.Println(csvFileContent[j+1][0], row[i+1])
mapLn[csvFileContent[j+1][0]] = row[i+1]
}
encodedJSON, _ := json.Marshal(mapLn)
// u.GeneralLogger.Println(string(encodedJSON))
file.Write(encodedJSON)
file.Close()
}
return u.ReturnErrorResponse(nil, "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment