Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions pkg/wavebase/wavebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"strings"
"sync"
"time"

"github.com/wavetermdev/waveterm/pkg/util/utilfn"
)

// set by main-server.go
Expand Down Expand Up @@ -199,28 +201,41 @@ func TryMkdirs(dirName string, perm os.FileMode, dirDesc string) error {
return nil
}

func listValidLangs(ctx context.Context) []string {
out, err := exec.CommandContext(ctx, "locale", "-a").CombinedOutput()
if err != nil {
log.Printf("error running 'locale -a': %s\n", err)
return []string{}
}
// don't bother with CRLF line endings
// this command doesn't work on windows
return strings.Split(string(out), "\n")
}

var osLangOnce = &sync.Once{}
var osLang string

func determineLang() string {
defaultLang := "en_US.UTF-8"
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFn()
if runtime.GOOS == "darwin" {
out, err := exec.CommandContext(ctx, "defaults", "read", "-g", "AppleLocale").CombinedOutput()
if err != nil {
log.Printf("error executing 'defaults read -g AppleLocale': %v\n", err)
return ""
log.Printf("error executing 'defaults read -g AppleLocale', will use default 'en_US.UTF-8': %v\n", err)
return defaultLang
Comment on lines +219 to +226
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling and validation.

A few suggestions to enhance the robustness of the language determination:

  1. The error message could be more informative by including the actual error output.
  2. The default language should also be validated against available locales.
 func determineLang() string {
     defaultLang := "en_US.UTF-8"
     ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
     defer cancelFn()
+    validLangs := listValidLangs(ctx)
+    
+    // Validate that default language is available
+    if !utilfn.ContainsStr(validLangs, defaultLang) {
+        log.Printf("warning: default language %s is not available, falling back to C.UTF-8\n", defaultLang)
+        defaultLang = "C.UTF-8"
+    }
+
     if runtime.GOOS == "darwin" {
         out, err := exec.CommandContext(ctx, "defaults", "read", "-g", "AppleLocale").CombinedOutput()
         if err != nil {
-            log.Printf("error executing 'defaults read -g AppleLocale', will use default 'en_US.UTF-8': %v\n", err)
+            log.Printf("error executing 'defaults read -g AppleLocale', will use %s: %v (output: %s)\n",
+                defaultLang, err, string(out))
             return defaultLang
         }

Committable suggestion skipped: line range outside the PR's diff.

}
strOut := string(out)
truncOut := strings.Split(strOut, "@")[0]
return strings.TrimSpace(truncOut) + ".UTF-8"
} else if runtime.GOOS == "win32" {
out, err := exec.CommandContext(ctx, "Get-Culture", "|", "select", "-exp", "Name").CombinedOutput()
if err != nil {
log.Printf("error executing 'Get-Culture | select -exp Name': %v\n", err)
return ""
preferredLang := strings.TrimSpace(truncOut) + ".UTF-8"
validLangs := listValidLangs(ctx)

if !utilfn.ContainsStr(validLangs, preferredLang) {
log.Printf("unable to use desired lang %s, will use default 'en_US.UTF-8'\n", preferredLang)
return defaultLang
}
return strings.TrimSpace(string(out)) + ".UTF-8"

return preferredLang
} else {
// this is specifically to get the wavesrv LANG so waveshell
// on a remote uses the same LANG
Expand Down
Loading