From 4962f21fbba949ae11638af5fd7292fe07ef94f1 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 21 Sep 2024 11:01:17 +0200 Subject: Support collapsing whitespace in translations --- cmd/exttmpl/main.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'cmd/exttmpl') diff --git a/cmd/exttmpl/main.go b/cmd/exttmpl/main.go index ca7c15f..599879a 100644 --- a/cmd/exttmpl/main.go +++ b/cmd/exttmpl/main.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "slices" + "strings" "text/template/parse" "golang.org/x/text/language" @@ -120,12 +121,11 @@ func process(tmplMsgs *[]pipeline.Message, node parse.Node) { continue } if sn, ok := arg.(*parse.StringNode); ok { + txt := collapse(sn.Text) *tmplMsgs = append(*tmplMsgs, pipeline.Message{ - ID: pipeline.IDList{sn.Text}, - Key: sn.Text, - Message: pipeline.Text{ - Msg: sn.Text, - }, + ID: pipeline.IDList{txt}, + Key: txt, + Message: pipeline.Text{Msg: txt}, }) } } @@ -177,6 +177,29 @@ func languages() []language.Tag { return tags } +func collapse(s string) string { + var ( + sb strings.Builder + prev bool + ) + const spc = " \t\n" + + for _, ch := range strings.Trim(s, spc) { + if strings.ContainsRune(spc, ch) { + if prev { + continue + } + ch = ' ' + prev = true + } else { + prev = false + } + sb.WriteRune(ch) + } + + return sb.String() +} + func try(err error) { if err != nil { die(err) -- cgit v1.2.3