Skip to content

Commit

Permalink
get system language.
Browse files Browse the repository at this point in the history
  • Loading branch information
whtiehack committed Oct 14, 2019
1 parent 40d195b commit ddf2a0d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
28 changes: 3 additions & 25 deletions examples/wowjump/utils.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package main

import (
"fmt"
"github.com/whtiehack/wingui/winapi"
"log"
"math/rand"
"net"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
"time"
"unsafe"

Expand Down Expand Up @@ -118,24 +115,6 @@ func randomMoveMouse() {

}

func GetLocale() (string, error) {
// Check the LANG environment variable, common on UNIX.
// XXX: we can easily override as a nice feature/bug.
envlang, ok := os.LookupEnv("LANG")
if ok {
return strings.Split(envlang, ".")[0], nil
}

// Exec powershell Get-Culture on Windows.
cmd := exec.Command("powershell", "Get-Culture | select -exp Name")
output, err := cmd.Output()
if err == nil {
return strings.Trim(string(output), "\r\n"), nil
}

return "", fmt.Errorf("cannot determine locale")
}

type Statistics struct {
prevTime time.Time
si string
Expand All @@ -155,9 +134,8 @@ func NewStatistics(baseUrl string, si string) *Statistics {
params.Add("et", "0")
params.Add("ja", "0")
params.Add("ln", "zh-cn")
lang, err := GetLocale()
if err != nil {
log.Println("system lang:", lang)
lang := winapi.GetSystemDefaultLocaleName()
if lang != "" {
params.Set("ln", lang)
}
params.Add("lo", "0")
Expand Down
15 changes: 12 additions & 3 deletions winapi/kernel32.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ import (
)

var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procCreateMutex = kernel32.NewProc("CreateMutexW")
procOpenMutex = kernel32.NewProc("OpenMutexW")
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procCreateMutex = kernel32.NewProc("CreateMutexW")
procOpenMutex = kernel32.NewProc("OpenMutexW")
getSystemDefaultLocaleName = kernel32.NewProc("GetSystemDefaultLocaleName")
getSystemDefaultLCID = kernel32.NewProc("GetSystemDefaultLCID")
)

func GetSystemDefaultLocaleName() string {
lcid, _, _ := getSystemDefaultLCID.Call()
buf := make([]uint16, 128)
_, _, _ = getSystemDefaultLocaleName.Call(uintptr(unsafe.Pointer(&buf[0])), lcid)
return syscall.UTF16ToString(buf)
}

//CreateMutex kernel32 API CreateMutex
func CreateMutex(name string) (uintptr, error) {
paramMutexName := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(name)))
Expand Down

0 comments on commit ddf2a0d

Please sign in to comment.