Skip to content

Commit

Permalink
use gls to track client locale
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Sep 12, 2017
1 parent c47439a commit 029f38b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
2 changes: 2 additions & 0 deletions client/client.go
Expand Up @@ -33,6 +33,7 @@ import (
"time"

"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/jsonutil"
)

Expand Down Expand Up @@ -174,6 +175,7 @@ func (client *Client) raw(method, urlpath string, query url.Values, headers map[
if err != nil {
return nil, RequestError{err}
}
req.Header.Set("X-Snapd-LcMessages", i18n.LcMessages())

for key, value := range headers {
req.Header.Set(key, value)
Expand Down
1 change: 0 additions & 1 deletion daemon/api.go
Expand Up @@ -715,7 +715,6 @@ func sendStorePackages(route *mux.Route, meta *Meta, found []*snap.Info) Respons

// plural!
func getSnapsInfo(c *Command, r *http.Request, user *auth.UserState) Response {

if shouldSearchStore(r) {
logger.Noticef("Jumping to \"find\" to better support legacy request %q", r.URL)
return searchStore(c, r, user)
Expand Down
4 changes: 3 additions & 1 deletion daemon/daemon.go
Expand Up @@ -34,6 +34,7 @@ import (

"github.com/coreos/go-systemd/activation"
"github.com/gorilla/mux"
"github.com/tylerb/gls"
"gopkg.in/tomb.v2"

"github.com/snapcore/snapd/client"
Expand Down Expand Up @@ -147,6 +148,8 @@ func (c *Command) canAccess(r *http.Request, user *auth.UserState) bool {
}

func (c *Command) ServeHTTP(w http.ResponseWriter, r *http.Request) {
gls.Set("LcMessages", r.Header.Get("X-Snapd-LcMessages"))

state := c.d.overlord.State()
state.Lock()
// TODO Look at the error and fail if there's an attempt to authenticate with invalid data.
Expand All @@ -157,7 +160,6 @@ func (c *Command) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Unauthorized("access denied").ServeHTTP(w, r)
return
}

var rspf ResponseFunc
var rsp = MethodNotAllowed("method %q not allowed", r.Method)

Expand Down
26 changes: 20 additions & 6 deletions i18n/i18n.go
Expand Up @@ -28,6 +28,7 @@ import (
"strings"

"github.com/ojii/gettext.go"
"github.com/tylerb/gls"

"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
Expand All @@ -38,6 +39,7 @@ import (
var (
TEXTDOMAIN = "snappy"
locale gettext.Catalog
localeStr string
translations gettext.Translations
)

Expand Down Expand Up @@ -82,25 +84,37 @@ func bindTextDomain(domain, dir string) {
}

func setLocale(loc string) {
locale = translations.Locale(LcMessages())
}

func LcMessages() string {
loc := os.Getenv("LC_MESSAGES")
if loc == "" {
loc = os.Getenv("LC_MESSAGES")
if loc == "" {
loc = os.Getenv("LANG")
}
loc = os.Getenv("LANG")
}

// de_DE.UTF-8, de_DE@euro all need to get simplified
loc = strings.Split(loc, "@")[0]
loc = strings.Split(loc, ".")[0]

locale = translations.Locale(loc)
return loc
}

// G is the shorthand for Gettext
func G(msgid string) string {
lcMessage := gls.Get("LcMessages")
if s, ok := lcMessage.(string); ok {
setLocale(s)
}

return locale.Gettext(msgid)
}

// NG is the shorthand for NGettext
func NG(msgid string, msgidPlural string, n uint32) string {
lcMessage := gls.Get("LcMessages")
if s, ok := lcMessage.(string); ok {
setLocale(s)
}

return locale.NGettext(msgid, msgidPlural, n)
}

0 comments on commit 029f38b

Please sign in to comment.