main.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "github.com/qor5/x/i18n/i18n-transfer/csv"
  9. "github.com/qor5/x/i18n/i18n-transfer/parser"
  10. "github.com/manifoldco/promptui"
  11. )
  12. func main() {
  13. prompt := promptui.Select{
  14. Label: "Import Or Export",
  15. Items: []string{"Import", "Export"},
  16. }
  17. _, result, err := prompt.Run()
  18. if err != nil || (result != "Import" && result != "Export") {
  19. fmt.Printf("Please select \"Import\" or \"Export\"\n")
  20. return
  21. }
  22. if result == "Import" {
  23. validate := func(input string) error {
  24. s, err := os.Stat(input)
  25. if err != nil || s.IsDir() {
  26. return errors.New("Please input correct csv file path")
  27. }
  28. return nil
  29. }
  30. prompt := promptui.Prompt{
  31. Label: "Csv File Path",
  32. Validate: validate,
  33. }
  34. result, err := prompt.Run()
  35. if err != nil {
  36. fmt.Printf("Please input correct csv file path\n")
  37. return
  38. }
  39. translationMap, err := csv.CsvToTranslationsMap(result)
  40. if err != nil {
  41. log.Fatalln(err)
  42. }
  43. projectPath, err := os.Getwd()
  44. if err != nil {
  45. fmt.Printf("Please run i18n-transfer in a correct project path\n")
  46. return
  47. }
  48. err = parser.ImportFromTranslationsMap(projectPath, translationMap)
  49. if err != nil {
  50. log.Fatalln(err)
  51. }
  52. }
  53. if result == "Export" {
  54. projectPath, _ := os.Getwd()
  55. projectPath, _ = filepath.Abs(projectPath)
  56. translationsMap, err := parser.ExportToTranslationsMap(projectPath)
  57. if err != nil {
  58. log.Fatalln(err)
  59. }
  60. for locale, translationMap := range translationsMap {
  61. fmt.Println(locale)
  62. for k, v := range translationMap {
  63. fmt.Printf(" %v: %v\n", k, v)
  64. }
  65. }
  66. err = csv.TranslationsMapToCsv(translationsMap)
  67. if err != nil {
  68. log.Fatalln(err)
  69. }
  70. }
  71. }