Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows: Add env support #22

Merged
merged 3 commits into from Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 70 additions & 0 deletions locale.go
Expand Up @@ -2,6 +2,8 @@ package locale

import (
"errors"
"os"
"strings"

"golang.org/x/text/language"
)
Expand Down Expand Up @@ -51,3 +53,71 @@ func detect() (lang []string, err error) {
}

type detector func() ([]string, error)

// Unless we call LookupEnv more than 9 times, we should not use Environ.
//
// goos: linux
// goarch: amd64
// pkg: github.com/Xuanwo/go-locale
// BenchmarkLookupEnv
// BenchmarkLookupEnv-8 37024654 32.4 ns/op
// BenchmarkEnviron
// BenchmarkEnviron-8 4275735 281 ns/op
// PASS

// envs is the env to be checked.
//
// LC_ALL will overwrite all LC_* options.
// FIXME: LC_ALL=C should overwrite $LANGUAGE env
//
// LC_MESSAGES is the config for messages.
// FIXME: LC_MESSAGES=C should overwrite $LANGUAGE env
//
// LANG is the default locale.
var envs = []string{"LC_ALL", "LC_MESSAGES", "LANG"}

// detectViaEnvLanguage checks env LANGUAGE
//
// Program use gettext will respect LANGUAGE env
func detectViaEnvLanguage() ([]string, error) {
s, ok := os.LookupEnv("LANGUAGE")
if !ok || s == "" {
return nil, &Error{"detect via env language", ErrNotDetected}
}
return parseEnvLanguage(s), nil
}

// detectViaEnvLc checks LC_* in order which decided by
// unix convention
//
// ref:
// - http://man7.org/linux/man-pages/man7/locale.7.html
// - https://linux.die.net/man/3/gettext
// - https://wiki.archlinux.org/index.php/Locale
func detectViaEnvLc() ([]string, error) {
for _, v := range envs {
s, ok := os.LookupEnv(v)
if ok && s != "" {
return []string{parseEnvLc(s)}, nil
}
}
return nil, &Error{"detect via env lc", ErrNotDetected}
}

// parseEnvLanguage will parse LANGUAGE env.
// Input could be: "en_AU:en_GB:en"
func parseEnvLanguage(s string) []string {
return strings.Split(s, ":")
}

// parseEnvLc will parse LC_* env.
// Input could be: "en_US.UTF-8"
func parseEnvLc(s string) string {
x := strings.Split(s, ".")
// "C" means "ANSI-C" and "POSIX", if locale set to C, we can simple
// set returned language to "en_US"
if x[0] == "C" {
return "en_US"
}
return x[0]
}
121 changes: 121 additions & 0 deletions locale_test.go
Expand Up @@ -4,8 +4,10 @@ package locale

import (
"errors"
"os"
"testing"

. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -137,3 +139,122 @@ func TestDetectAll(t *testing.T) {
})
}
}

func TestDetectViaEnvLanguage(t *testing.T) {
Convey("detect via env language", t, func() {
// Make sure env has clear before current test.
setupEnv()

Reset(func() {
// Reset all env after every Convey.
setupEnv()
})

Convey("When LANGUAGE has valid value", func() {
err := os.Setenv("LANGUAGE", "en_US")
if err != nil {
t.Error(err)
}

lang, err := detectViaEnvLanguage()

Convey("The error should not be nil", func() {
So(err, ShouldBeNil)
})
Convey("The lang should not be equal", func() {
So(lang, ShouldResemble, []string{"en_US"})
})
})

Convey("When LANGUAGE has multiple value", func() {
err := os.Setenv("LANGUAGE", "en_US:zh_CN")
if err != nil {
t.Error(err)
}

lang, err := detectViaEnvLanguage()

Convey("The error should not be nil", func() {
So(err, ShouldBeNil)
})
Convey("The lang should not be equal", func() {
So(lang, ShouldResemble, []string{"en_US", "zh_CN"})
})
})

Convey("When LANGUAGE is empty", func() {
err := os.Setenv("LANGUAGE", "")
if err != nil {
t.Error(err)
}

lang, err := detectViaEnvLanguage()

Convey("The error should be ErrNotDetected", func() {
So(errors.Is(err, ErrNotDetected), ShouldBeTrue)
})
Convey("The lang should be empty", func() {
So(lang, ShouldBeEmpty)
})
})
})
}

func TestDetectViaEnvLc(t *testing.T) {
Convey("detect via env language", t, func() {
// Make sure env has clear before current test.
setupEnv()

Reset(func() {
// Reset all env after every Convey.
setupEnv()
})

Convey("When LC_ALL has been set", func() {
err := os.Setenv("LC_ALL", "en_US.UTF-8")
if err != nil {
t.Error(err)
}

lang, err := detectViaEnvLc()

Convey("The error should not be nil", func() {
So(err, ShouldBeNil)
})
Convey("The lang should not be equal", func() {
So(lang, ShouldResemble, []string{"en_US"})
})
})

Convey("When no LC env has been set", func() {
lang, err := detectViaEnvLc()

Convey("The error should be ErrNotDetected", func() {
So(errors.Is(err, ErrNotDetected), ShouldBeTrue)
})
Convey("The lang should be empty", func() {
So(lang, ShouldBeEmpty)
})
})
})
}

func TestParseEnvLc(t *testing.T) {
Convey("parse env lc", t, func() {
Convey("When input en_US.UTF-8", func() {
x := parseEnvLc("en_US.UTF-8")

Convey("The lang should be en_US", func() {
So(x, ShouldEqual, "en_US")
})
})

Convey("When input C.UTF-8", func() {
x := parseEnvLc("C.UTF-8")

Convey("The lang should be en_US", func() {
So(x, ShouldEqual, "en_US")
})
})
})
}
68 changes: 0 additions & 68 deletions locale_unix.go
Expand Up @@ -10,56 +10,6 @@ import (
"strings"
)

// Unless we call LookupEnv more than 9 times, we should not use Environ.
//
// goos: linux
// goarch: amd64
// pkg: github.com/Xuanwo/go-locale
// BenchmarkLookupEnv
// BenchmarkLookupEnv-8 37024654 32.4 ns/op
// BenchmarkEnviron
// BenchmarkEnviron-8 4275735 281 ns/op
// PASS

// envs is the env to be checked.
//
// LC_ALL will overwrite all LC_* options.
// FIXME: LC_ALL=C should overwrite $LANGUAGE env
//
// LC_MESSAGES is the config for messages.
// FIXME: LC_MESSAGES=C should overwrite $LANGUAGE env
//
// LANG is the default locale.
var envs = []string{"LC_ALL", "LC_MESSAGES", "LANG"}

// detectViaEnvLanguage checks env LANGUAGE
//
// Program use gettext will respect LANGUAGE env
func detectViaEnvLanguage() ([]string, error) {
s, ok := os.LookupEnv("LANGUAGE")
if !ok || s == "" {
return nil, &Error{"detect via env language", ErrNotDetected}
}
return parseEnvLanguage(s), nil
}

// detectViaEnvLc checks LC_* in order which decided by
// unix convention
//
// ref:
// - http://man7.org/linux/man-pages/man7/locale.7.html
// - https://linux.die.net/man/3/gettext
// - https://wiki.archlinux.org/index.php/Locale
func detectViaEnvLc() ([]string, error) {
for _, v := range envs {
s, ok := os.LookupEnv(v)
if ok && s != "" {
return []string{parseEnvLc(s)}, nil
}
}
return nil, &Error{"detect via env lc", ErrNotDetected}
}

func detectViaLocaleConf() (_ []string, err error) {
defer func() {
if err != nil {
Expand Down Expand Up @@ -154,21 +104,3 @@ func getLocaleConfPath() string {

return ""
}

// parseEnvLanguage will parse LANGUAGE env.
// Input could be: "en_AU:en_GB:en"
func parseEnvLanguage(s string) []string {
return strings.Split(s, ":")
}

// parseEnvLc will parse LC_* env.
// Input could be: "en_US.UTF-8"
func parseEnvLc(s string) string {
x := strings.Split(s, ".")
// "C" means "ANSI-C" and "POSIX", if locale set to C, we can simple
// set returned language to "en_US"
if x[0] == "C" {
return "en_US"
}
return x[0]
}