csv_to_translations_map.go 826 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package csv
  2. import (
  3. "encoding/csv"
  4. "os"
  5. )
  6. func CsvToTranslationsMap(csvPath string) (translationsMap map[string]map[string]string, err error) {
  7. f, err := os.Open(csvPath)
  8. if err != nil {
  9. return
  10. }
  11. r := csv.NewReader(f)
  12. records, err := r.ReadAll()
  13. if err != nil {
  14. return
  15. }
  16. var keyValueMap = make(map[int]map[int]string)
  17. for i, record := range records {
  18. for j, value := range record {
  19. if keyValueMap[i] == nil {
  20. keyValueMap[i] = make(map[int]string)
  21. }
  22. keyValueMap[i][j] = value
  23. }
  24. }
  25. translationsMap = make(map[string]map[string]string)
  26. for i, record := range records {
  27. for j, value := range record {
  28. if translationsMap[records[0][j]] == nil {
  29. translationsMap[records[0][j]] = make(map[string]string)
  30. }
  31. translationsMap[records[0][j]][records[i][0]] = value
  32. }
  33. }
  34. return
  35. }