Skip to content

Commit e9d68ff

Browse files
Address Copilot review: remove unused printer field and add cross-platform fallback
1 parent 73aea97 commit e9d68ff

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

pkg/sqlcmd/regional.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import (
99
"time"
1010

1111
"golang.org/x/text/language"
12-
"golang.org/x/text/message"
1312
)
1413

1514
// RegionalSettings provides locale-aware formatting for output when -R is used
1615
type RegionalSettings struct {
1716
enabled bool
18-
printer *message.Printer
1917
tag language.Tag
2018
dateFmt string
2119
timeFmt string
@@ -27,7 +25,6 @@ func NewRegionalSettings(enabled bool) *RegionalSettings {
2725
r := &RegionalSettings{enabled: enabled}
2826
if enabled {
2927
r.tag = detectUserLocale()
30-
r.printer = message.NewPrinter(r.tag)
3128
r.dateFmt, r.timeFmt = getLocaleDateTimeFormats(r.tag)
3229
}
3330
return r

pkg/sqlcmd/regional_other.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
//go:build !windows && !linux && !darwin
5+
6+
package sqlcmd
7+
8+
import (
9+
"os"
10+
"strings"
11+
12+
"golang.org/x/text/language"
13+
)
14+
15+
// detectUserLocale returns the user's locale from environment variables.
16+
// This is a fallback implementation for platforms other than Windows, Linux, and Darwin.
17+
// It uses the same environment variable approach as Linux.
18+
func detectUserLocale() language.Tag {
19+
// Check standard locale environment variables in order of precedence
20+
for _, envVar := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} {
21+
if locale := os.Getenv(envVar); locale != "" {
22+
tag := parseUnixLocale(locale)
23+
if tag != language.Und {
24+
return tag
25+
}
26+
}
27+
}
28+
return language.English
29+
}
30+
31+
// parseUnixLocale converts a Unix locale string to a language.Tag
32+
// Examples: "en_US.UTF-8", "de_DE", "fr_FR.utf8", "C", "POSIX"
33+
func parseUnixLocale(locale string) language.Tag {
34+
// Handle special cases
35+
if locale == "C" || locale == "POSIX" || locale == "" {
36+
return language.English
37+
}
38+
39+
// Remove encoding suffix (e.g., ".UTF-8")
40+
if idx := strings.Index(locale, "."); idx != -1 {
41+
locale = locale[:idx]
42+
}
43+
44+
// Remove modifier (e.g., "@euro")
45+
if idx := strings.Index(locale, "@"); idx != -1 {
46+
locale = locale[:idx]
47+
}
48+
49+
// Convert underscore to hyphen for BCP 47 format
50+
locale = strings.Replace(locale, "_", "-", -1)
51+
52+
if tag, err := language.Parse(locale); err == nil {
53+
return tag
54+
}
55+
56+
// Try with just the language part
57+
if idx := strings.Index(locale, "-"); idx != -1 {
58+
if tag, err := language.Parse(locale[:idx]); err == nil {
59+
return tag
60+
}
61+
}
62+
63+
return language.Und
64+
}

0 commit comments

Comments
 (0)