Skip to content

Commit 2a8b983

Browse files
committed
refactor(cli.go, translate.go): simplify translation logic and improve code readability
- Removed unnecessary string manipulation and chunk processing in `cli.go` to streamline translation process. - Updated `translate.go` to enhance the `trimDividers` function, making it more efficient and straightforward.
1 parent 4c17993 commit 2a8b983

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

internal/cli/cli.go

+11-20
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"io/fs"
1111
"os"
1212
"os/signal"
13-
"strings"
1413
"syscall"
1514
"time"
1615

@@ -196,25 +195,17 @@ func (app *App) translate() {
196195
options.Translate.SourceLang = ""
197196
}
198197

199-
chunks := getChunks(string(source), options.Translate.SplitChunks, options.Verbose)
200-
201-
var results []string
202-
for _, chunk := range chunks {
203-
chunkResult, err := translator.Translate(
204-
ctx,
205-
dragoman.TranslateParams{
206-
Document: chunk,
207-
Source: options.Translate.SourceLang,
208-
Target: options.Translate.TargetLang,
209-
Preserve: options.Translate.Preserve,
210-
Instructions: options.Translate.Instructions,
211-
},
212-
)
213-
app.kong.FatalIfErrorf(err)
214-
results = append(results, chunkResult)
215-
}
216-
217-
result := strings.Join(results, "\n\n")
198+
result, err := translator.Translate(
199+
ctx,
200+
dragoman.TranslateParams{
201+
Document: string(source),
202+
Source: options.Translate.SourceLang,
203+
Target: options.Translate.TargetLang,
204+
Preserve: options.Translate.Preserve,
205+
Instructions: options.Translate.Instructions,
206+
},
207+
)
208+
app.kong.FatalIfErrorf(err, "failed to translate document")
218209

219210
if options.Translate.Dry {
220211
fmt.Fprintf(os.Stdout, "%s\n", result)

translate.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,14 @@ func (t *Translator) translateChunk(ctx context.Context, chunk string, params Tr
118118
return "", err
119119
}
120120

121-
response = trimDividers(response)
122-
123-
return response, nil
121+
return trimDividers(response), nil
124122
}
125123

126124
func trimDividers(text string) string {
127125
lines := strings.Split(text, "\n")
128126

129-
if len(lines) < 1 {
130-
return strings.TrimSpace(text)
127+
if len(lines) == 0 {
128+
return text
131129
}
132130

133131
out := slices.Clone(lines)

0 commit comments

Comments
 (0)