Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the computer's current language in Go?

Tags:

go

locale

culture

How do I get the computer's current language in Go?

e.g. en-US for USA or es-es for Spain.

like image 412
Gordon Truslove Avatar asked Sep 06 '25 15:09

Gordon Truslove


2 Answers

Note: Windows doesn't not rely on LANG environment variable (or LC_* variables: none are defined on my Windows 8)

The locale is stored in HKCU/ControlPanel/International/LocalName (as mentioned in this thread)

localname

So you are better off using a project accessing the registry, like:

  • registry/registry.go
  • gowin
like image 83
VonC Avatar answered Sep 11 '25 03:09

VonC


On *nix based systems you can simply use os.Getenv("LANG"), I'm not sure if that applies to windows.

//edit

@JimB mentioned that there are other variables to check too, for more details check gettext

on my system:

➜ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
like image 29
OneOfOne Avatar answered Sep 11 '25 04:09

OneOfOne