summaryrefslogtreecommitdiffhomepage
path: root/cmd
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-09-21 11:01:17 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-09-21 11:01:17 +0200
commit4962f21fbba949ae11638af5fd7292fe07ef94f1 (patch)
treebff5b048fb58259cce37f04595279964bd169a8c /cmd
parent42d0c8cc527dd0732b8a752116d676fd1863bb15 (diff)
Support collapsing whitespace in translations
Diffstat (limited to 'cmd')
-rw-r--r--cmd/exttmpl/main.go33
1 files changed, 28 insertions, 5 deletions
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)