Skip to content

Commit 761fbb1

Browse files
committed
fix: default en_US.UTF-8 if lang has problems
Mac's `defaults read -g AppleLocale` command can return locales that don't have a language in /usr/share/locale. This prevents those locales from using a language with UTF-8 support. This will use en_US.UTF-8 as a default to cover those cases if there are problems. As per usual, if LANG is already set, that value will be used instead.
1 parent b51ff83 commit 761fbb1

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

pkg/wavebase/wavebase.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"strings"
1818
"sync"
1919
"time"
20+
21+
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
2022
)
2123

2224
// set by main-server.go
@@ -199,21 +201,41 @@ func TryMkdirs(dirName string, perm os.FileMode, dirDesc string) error {
199201
return nil
200202
}
201203

204+
func listValidLangs(ctx context.Context) []string {
205+
out, err := exec.CommandContext(ctx, "locale", "-a").CombinedOutput()
206+
if err != nil {
207+
log.Printf("error running 'locale -a': %s\n", err)
208+
return []string{}
209+
}
210+
// don't bother with CRLF line endings
211+
// this command doesn't work on windows
212+
return strings.Split(string(out), "\n")
213+
}
214+
202215
var osLangOnce = &sync.Once{}
203216
var osLang string
204217

205218
func determineLang() string {
219+
defaultLang := "en_US.UTF-8"
206220
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
207221
defer cancelFn()
208222
if runtime.GOOS == "darwin" {
209223
out, err := exec.CommandContext(ctx, "defaults", "read", "-g", "AppleLocale").CombinedOutput()
210224
if err != nil {
211-
log.Printf("error executing 'defaults read -g AppleLocale': %v\n", err)
212-
return ""
225+
log.Printf("error executing 'defaults read -g AppleLocale', will use default 'en_US.UTF-8': %v\n", err)
226+
return defaultLang
213227
}
214228
strOut := string(out)
215229
truncOut := strings.Split(strOut, "@")[0]
216-
return strings.TrimSpace(truncOut) + ".UTF-8"
230+
preferredLang := strings.TrimSpace(truncOut) + ".UTF-8"
231+
validLangs := listValidLangs(ctx)
232+
233+
if !utilfn.ContainsStr(validLangs, preferredLang) {
234+
log.Printf("unable to use desired lang %s, will use default 'en_US.UTF-8'\n", preferredLang)
235+
return defaultLang
236+
}
237+
238+
return preferredLang
217239
} else if runtime.GOOS == "win32" {
218240
out, err := exec.CommandContext(ctx, "Get-Culture", "|", "select", "-exp", "Name").CombinedOutput()
219241
if err != nil {

0 commit comments

Comments
 (0)