github.com/richardwilkes/toolbox@v1.121.0/i18n/localization_windows.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  package i18n
    11  
    12  import (
    13  	"syscall"
    14  	"unsafe"
    15  )
    16  
    17  // Locale returns the locale set for the user. If that has not been set, then it falls back to the locale set for the
    18  // system. If that is also unset, then it return "en_US.UTF-8".
    19  func Locale() string {
    20  	kernel32 := syscall.NewLazyDLL("kernel32.dll")
    21  	proc := kernel32.NewProc("GetUserDefaultLocaleName")
    22  	buffer := make([]uint16, 128)
    23  	if ret, _, _ := proc.Call(uintptr(unsafe.Pointer(&buffer[0])), uintptr(len(buffer))); ret == 0 {
    24  		proc = kernel32.NewProc("GetSystemDefaultLocaleName")
    25  		if ret, _, _ = proc.Call(uintptr(unsafe.Pointer(&buffer[0])), uintptr(len(buffer))); ret == 0 {
    26  			return "en_US.UTF-8"
    27  		}
    28  	}
    29  	return syscall.UTF16ToString(buffer)
    30  }