Skip to content

Commit ddf2a0d

Browse files
committed
get system language.
1 parent 40d195b commit ddf2a0d

File tree

2 files changed

+15
-28
lines changed

2 files changed

+15
-28
lines changed

examples/wowjump/utils.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package main
22

33
import (
4-
"fmt"
4+
"github.com/whtiehack/wingui/winapi"
55
"log"
66
"math/rand"
77
"net"
88
"net/url"
9-
"os"
10-
"os/exec"
119
"strconv"
12-
"strings"
1310
"time"
1411
"unsafe"
1512

@@ -118,24 +115,6 @@ func randomMoveMouse() {
118115

119116
}
120117

121-
func GetLocale() (string, error) {
122-
// Check the LANG environment variable, common on UNIX.
123-
// XXX: we can easily override as a nice feature/bug.
124-
envlang, ok := os.LookupEnv("LANG")
125-
if ok {
126-
return strings.Split(envlang, ".")[0], nil
127-
}
128-
129-
// Exec powershell Get-Culture on Windows.
130-
cmd := exec.Command("powershell", "Get-Culture | select -exp Name")
131-
output, err := cmd.Output()
132-
if err == nil {
133-
return strings.Trim(string(output), "\r\n"), nil
134-
}
135-
136-
return "", fmt.Errorf("cannot determine locale")
137-
}
138-
139118
type Statistics struct {
140119
prevTime time.Time
141120
si string
@@ -155,9 +134,8 @@ func NewStatistics(baseUrl string, si string) *Statistics {
155134
params.Add("et", "0")
156135
params.Add("ja", "0")
157136
params.Add("ln", "zh-cn")
158-
lang, err := GetLocale()
159-
if err != nil {
160-
log.Println("system lang:", lang)
137+
lang := winapi.GetSystemDefaultLocaleName()
138+
if lang != "" {
161139
params.Set("ln", lang)
162140
}
163141
params.Add("lo", "0")

winapi/kernel32.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@ import (
66
)
77

88
var (
9-
kernel32 = syscall.NewLazyDLL("kernel32.dll")
10-
procCreateMutex = kernel32.NewProc("CreateMutexW")
11-
procOpenMutex = kernel32.NewProc("OpenMutexW")
9+
kernel32 = syscall.NewLazyDLL("kernel32.dll")
10+
procCreateMutex = kernel32.NewProc("CreateMutexW")
11+
procOpenMutex = kernel32.NewProc("OpenMutexW")
12+
getSystemDefaultLocaleName = kernel32.NewProc("GetSystemDefaultLocaleName")
13+
getSystemDefaultLCID = kernel32.NewProc("GetSystemDefaultLCID")
1214
)
1315

16+
func GetSystemDefaultLocaleName() string {
17+
lcid, _, _ := getSystemDefaultLCID.Call()
18+
buf := make([]uint16, 128)
19+
_, _, _ = getSystemDefaultLocaleName.Call(uintptr(unsafe.Pointer(&buf[0])), lcid)
20+
return syscall.UTF16ToString(buf)
21+
}
22+
1423
//CreateMutex kernel32 API CreateMutex
1524
func CreateMutex(name string) (uintptr, error) {
1625
paramMutexName := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(name)))

0 commit comments

Comments
 (0)