Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang get system language

Is there a function for getting the user's system language I can't find it anywhere on the Go site.

To clarify my question i want the current user's system language that means when everything is for example dutch on the system i want to get "dutch" or "nl"

note: the program is made to run on linux, mac and windows

like image 370
mark kopenga Avatar asked Aug 13 '18 19:08

mark kopenga


People also ask

Why is Golang fast?

One of the main things that make Go compile fast is its dependency management. To say simply, Go imports only those packages that are directly required for the program other than C/C++ compilers. There are also no templates which amounts to less preprocessing overhead.

What is Golang written in?

Golang was formerly written in C but is now written in Go itself. As of December 2013, the Go team announced their transitioning of the compiler to Go. Since February 2015 the implementation of C has been deleted and the compiler is now self-hosting, with the new compiler first introduced in Go 1.5.

What is Golang good for?

What is Golang Useful for? Golang is useful for carrying out programming for scalable servers and large software systems. The Golang programming language was built to fill in the gaps of C++ and Java that Google came across while working with its servers and distributed systems.

Is Golang open source?

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.


1 Answers

There is a nice cross-platform library called jibber_jabber which looks up for $LC_ALL or $LANG env variables in Unix systems and, for Windows, loads Kernel32 and calls some procedures like GetUserDefaultLocaleName or GetSystemDefaultLocaleName inside. For Windows, all is done in the getWindowsLocale() function. Be advised that Kernel32 library comes built-in in Windows OS kernel.

By the way, should you need to convert locale-format'd language strings like en_US and fr_FR, you can utilize the x/text/language and x/text/language/display packages to get fully qualified name of the language.

lang := "ru_RU"
tag := language.MustParse(lang)

inEng := display.English.Languages()
inTur := display.Turkish.Languages()
inSelf := display.Self

fmt.Println(inEng.Name(tag))
fmt.Println(inSelf.Name(tag))
fmt.Println(inTur.Name(tag))

   // Output:
   // Russian
   // русский
   // Rusça
like image 153
Berkant Ipek Avatar answered Sep 29 '22 23:09

Berkant Ipek