|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/fdddf/xcstrings-translator/internal/model" |
| 9 | + "github.com/fdddf/xcstrings-translator/internal/translator" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/spf13/viper" |
| 13 | +) |
| 14 | + |
| 15 | +// globalOptions holds the shared input/output/language settings, resolved from |
| 16 | +// flags first and falling back to the config file / environment via viper. |
| 17 | +type globalOptions struct { |
| 18 | + InputFile string |
| 19 | + OutputFile string |
| 20 | + SourceLang string |
| 21 | + TargetLangs []string |
| 22 | + Concurrency int |
| 23 | + Verbose bool |
| 24 | +} |
| 25 | + |
| 26 | +// resolveGlobalOptions reads the shared persistent flags for a command. |
| 27 | +func resolveGlobalOptions(cmd *cobra.Command) globalOptions { |
| 28 | + return globalOptions{ |
| 29 | + InputFile: stringFlag(cmd, "input", "global.input_file"), |
| 30 | + OutputFile: stringFlag(cmd, "output", "global.output_file"), |
| 31 | + SourceLang: stringFlag(cmd, "source-language", "global.source_language"), |
| 32 | + TargetLangs: stringSliceFlag(cmd, "target-languages", "global.target_languages"), |
| 33 | + Concurrency: intFlag(cmd, "concurrency", "global.concurrency"), |
| 34 | + Verbose: boolFlag(cmd, "verbose", "global.verbose"), |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// The *Flag helpers return the flag value when the user set it explicitly, |
| 39 | +// otherwise the value resolved by viper (config file / env / default). |
| 40 | +func stringFlag(cmd *cobra.Command, name, viperKey string) string { |
| 41 | + if cmd.Flags().Changed(name) { |
| 42 | + v, _ := cmd.Flags().GetString(name) |
| 43 | + return v |
| 44 | + } |
| 45 | + return viper.GetString(viperKey) |
| 46 | +} |
| 47 | + |
| 48 | +func stringSliceFlag(cmd *cobra.Command, name, viperKey string) []string { |
| 49 | + if cmd.Flags().Changed(name) { |
| 50 | + v, _ := cmd.Flags().GetStringSlice(name) |
| 51 | + return v |
| 52 | + } |
| 53 | + return viper.GetStringSlice(viperKey) |
| 54 | +} |
| 55 | + |
| 56 | +func intFlag(cmd *cobra.Command, name, viperKey string) int { |
| 57 | + if cmd.Flags().Changed(name) { |
| 58 | + v, _ := cmd.Flags().GetInt(name) |
| 59 | + return v |
| 60 | + } |
| 61 | + return viper.GetInt(viperKey) |
| 62 | +} |
| 63 | + |
| 64 | +func boolFlag(cmd *cobra.Command, name, viperKey string) bool { |
| 65 | + if cmd.Flags().Changed(name) { |
| 66 | + v, _ := cmd.Flags().GetBool(name) |
| 67 | + return v |
| 68 | + } |
| 69 | + return viper.GetBool(viperKey) |
| 70 | +} |
| 71 | + |
| 72 | +func float64Flag(cmd *cobra.Command, name, viperKey string) float64 { |
| 73 | + if cmd.Flags().Changed(name) { |
| 74 | + v, _ := cmd.Flags().GetFloat64(name) |
| 75 | + return v |
| 76 | + } |
| 77 | + return viper.GetFloat64(viperKey) |
| 78 | +} |
| 79 | + |
| 80 | +// runTranslation executes the shared load → translate → apply → save pipeline |
| 81 | +// used by every provider subcommand. |
| 82 | +func runTranslation(g globalOptions, providerName string, provider model.TranslationProvider, timeout time.Duration) error { |
| 83 | + if g.InputFile == "" { |
| 84 | + return fmt.Errorf("input file path is required; set --input or global.input_file in the config") |
| 85 | + } |
| 86 | + if g.OutputFile == "" { |
| 87 | + return fmt.Errorf("output file path is required; set --output or global.output_file in the config") |
| 88 | + } |
| 89 | + |
| 90 | + if g.Verbose { |
| 91 | + fmt.Printf("Starting %s with:\n", providerName) |
| 92 | + fmt.Printf(" Input file: %s\n", g.InputFile) |
| 93 | + fmt.Printf(" Output file: %s\n", g.OutputFile) |
| 94 | + fmt.Printf(" Source language: %s\n", g.SourceLang) |
| 95 | + fmt.Printf(" Target languages: %v\n", g.TargetLangs) |
| 96 | + fmt.Printf(" Concurrency: %d\n", g.Concurrency) |
| 97 | + fmt.Println("Loading xcstrings file...") |
| 98 | + } |
| 99 | + |
| 100 | + xcstrings, err := model.LoadXCStrings(g.InputFile) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("loading xcstrings file: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + if g.SourceLang != "" { |
| 106 | + xcstrings.SourceLanguage = g.SourceLang |
| 107 | + } |
| 108 | + |
| 109 | + service := translator.NewTranslationService(provider, g.Concurrency, timeout) |
| 110 | + ctx := context.Background() |
| 111 | + |
| 112 | + if g.Verbose { |
| 113 | + fmt.Println("Starting translation...") |
| 114 | + } |
| 115 | + |
| 116 | + var responses []model.TranslationResponse |
| 117 | + for _, target := range g.TargetLangs { |
| 118 | + reqs := translator.CreateTranslationRequestsForLanguage(xcstrings, target) |
| 119 | + if len(reqs) == 0 { |
| 120 | + continue |
| 121 | + } |
| 122 | + |
| 123 | + if g.Verbose { |
| 124 | + fmt.Printf("Translating to %s (%d strings)...\n", target, len(reqs)) |
| 125 | + } |
| 126 | + |
| 127 | + progress := translator.NewVerboseProgressReporter(target, len(reqs), g.Verbose) |
| 128 | + batch, err := service.TranslateBatch(ctx, reqs, progress) |
| 129 | + responses = append(responses, batch...) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("translation failed for %s: %w", target, err) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if len(responses) == 0 { |
| 136 | + fmt.Println("No strings to translate. Exiting.") |
| 137 | + return nil |
| 138 | + } |
| 139 | + |
| 140 | + successCount, errorCount := 0, 0 |
| 141 | + for _, resp := range responses { |
| 142 | + if resp.Error != nil { |
| 143 | + if g.Verbose { |
| 144 | + fmt.Printf("Error translating %s to %s: %v\n", resp.Key, resp.TargetLanguage, resp.Error) |
| 145 | + } |
| 146 | + errorCount++ |
| 147 | + } else { |
| 148 | + successCount++ |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if g.Verbose { |
| 153 | + fmt.Printf("Translation completed: %d successful, %d failed\n", successCount, errorCount) |
| 154 | + } |
| 155 | + |
| 156 | + if errorCount > 0 { |
| 157 | + fmt.Println("Errors detected during translation. Stopping without applying translations.") |
| 158 | + return nil |
| 159 | + } |
| 160 | + |
| 161 | + if g.Verbose { |
| 162 | + fmt.Println("Applying translations...") |
| 163 | + } |
| 164 | + translator.ApplyTranslations(xcstrings, responses) |
| 165 | + |
| 166 | + if g.Verbose { |
| 167 | + fmt.Printf("Saving output to %s...\n", g.OutputFile) |
| 168 | + } |
| 169 | + if err := model.SaveXCStrings(g.OutputFile, xcstrings); err != nil { |
| 170 | + return fmt.Errorf("saving output file: %w", err) |
| 171 | + } |
| 172 | + |
| 173 | + fmt.Println("Translation completed successfully!") |
| 174 | + fmt.Printf("Results saved to: %s\n", g.OutputFile) |
| 175 | + return nil |
| 176 | +} |
0 commit comments