How can I fetch the default locale (server locale not from the request) in Golang

587 views
Skip to first unread message

prabhakara...@gmail.com

unread,
Feb 14, 2018, 8:40:47 AM2/14/18
to golang-nuts
Hello All,
  I'm a newbee to Golang and am trying to fetch the default server locale. Can you help me how I can fetch the default server locale in golang?

In other words, I'm looking for the Java equivalent code in golang i.e 'Locale.getDefault()'

Thanks
Anish


matthe...@gmail.com

unread,
Feb 14, 2018, 9:40:32 AM2/14/18
to golang-nuts
Serving to a locale may be helped with this blog: https://blog.golang.org/matchlang

This library looks like it has implementations for finding the OS locale: https://github.com/cloudfoundry-attic/jibber_jabber

Matt

Ian Lance Taylor

unread,
Feb 14, 2018, 9:51:05 AM2/14/18
to prabhakara...@gmail.com, golang-nuts
Go doesn't use locales in the same way that Java does. If you tell us
what you want to do with that information, we may be able to help.
You may want to look at https://godoc.org/golang.org/x/text .

Ian

prabhakara...@gmail.com

unread,
Feb 16, 2018, 10:46:59 AM2/16/18
to golang-nuts
The intention is basically to send out translations from the deployed shard. Hence,we are trying to find the locale of the server locale. Because of some reason, we cannot extract this as an environment variable on the shard or get it from the client.

Best
Anish  

Ian Lance Taylor

unread,
Feb 16, 2018, 11:11:07 AM2/16/18
to prabhakara...@gmail.com, golang-nuts
On Fri, Feb 16, 2018 at 7:46 AM, <prabhakara...@gmail.com> wrote:
>
> The intention is basically to send out translations from the deployed shard.
> Hence,we are trying to find the locale of the server locale. Because of some
> reason, we cannot extract this as an environment variable on the shard or
> get it from the client.

What operating system?

What format do you want for the locale?

Ian


> On Wednesday, 14 February 2018 20:21:05 UTC+5:30, Ian Lance Taylor wrote:
>>
>> On Tue, Feb 13, 2018 at 11:19 PM, <prabhakara...@gmail.com> wrote:
>> >
>> > I'm a newbee to Golang and am trying to fetch the default server
>> > locale.
>> > Can you help me how I can fetch the default server locale in golang?
>> >
>> > In other words, I'm looking for the Java equivalent code in golang i.e
>> > 'Locale.getDefault()'
>>
>> Go doesn't use locales in the same way that Java does. If you tell us
>> what you want to do with that information, we may be able to help.
>> You may want to look at https://godoc.org/golang.org/x/text .
>>
>> Ian
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Anish P

unread,
Feb 16, 2018, 11:22:44 AM2/16/18
to Ian Lance Taylor, golang-nuts
I'm looking to fetch the IANA timezone. 

Cheers
Anish


Thanks and Regards
Anish Prabhakaran

On Fri, Feb 16, 2018 at 9:40 PM, Ian Lance Taylor <ia...@golang.org> wrote:

Ian Lance Taylor

unread,
Feb 16, 2018, 11:39:03 AM2/16/18
to Anish P, golang-nuts
On Fri, Feb 16, 2018 at 8:21 AM, Anish P
<prabhakara...@gmail.com> wrote:
>
> I'm looking to fetch the IANA timezone.

For what it's worth, I don't usually call the timezone a "locale". In
programming, "locale" usually refers to language related issues. See
https://en.wikipedia.org/wiki/Locale_(computer_software) .

To get the timezone name, see https://golang.org/pkg/time/#Time.Zone .

Ian

Anish P

unread,
Feb 16, 2018, 11:43:40 AM2/16/18
to Ian Lance Taylor, golang-nuts
My bad. 

I was referring to the same.Something which is packaged in go giving a standard locale ID as q_AL, ar_DZ

Cheers
Anish

uwe.dau...@betalo.se

unread,
Feb 16, 2018, 11:35:04 PM2/16/18
to golang-nuts
Java’s Locale.getDefault returns (if not overridden) the values from the system property “user.language” and “user.region”. If these are not set, it simply returns “en”. See http://hg.openjdk.java.net/jdk8u/jdk8u60/jdk/file/935758609767/src/share/classes/java/util/Locale.java

For Linux: These system properties (if not overridden) are set on Linux by interpreting the environment variable “LANG”. See https://docs.oracle.com/javame/config/cdc/cdc-opt-impl/ojmeec/1.0/runtime/html/localization.htm

Thus the equivalent code in Go would be to access this environment variable. Thus a simple `locale = os.Getenv("LANG")` or as Jibber-Jabber does it:

```
locale = os.Getenv("LC_ALL")
if locale == "" {
locale = os.Getenv("LANG")
}
```

If it is correct that, as you stated, that you have no access to environment variables, you won’t be able to conclude more than “en” to be compatible with Java’s behavior.

Anish P

unread,
Feb 17, 2018, 9:36:44 AM2/17/18
to uwe.dau...@betalo.se, golang-nuts
Thanks for the details. Had a look earlier at the code mentioned by Jibber-Jabber as you mentioned. Will post the status as soon as I try this out.

Cheers
Anish


--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/8xC2c_VcSAQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.

Anish P

unread,
Feb 26, 2018, 1:16:29 AM2/26/18
to uwe.dau...@betalo.se, golang-nuts
Just an update on the same. Even the library from "Jibber-Jabber" seems to be returning only empty string values. 

Forced to set this as an ENVIRONMENT_VARIABLE for the time being and moving on. 

Cheers
Anish
Reply all
Reply to author
Forward
0 new messages