Permalink
Browse files

use gls to track client locale

  • Loading branch information...
1 parent c47439a commit 029f38be38ff9c634cb21d10e30ff6dba094ca34 @mvo5 committed Sep 12, 2017
Showing with 25 additions and 8 deletions.
  1. +2 −0 client/client.go
  2. +0 −1 daemon/api.go
  3. +3 −1 daemon/daemon.go
  4. +20 −6 i18n/i18n.go
View
@@ -33,6 +33,7 @@ import (
"time"
"github.com/snapcore/snapd/dirs"
+ "github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/jsonutil"
)
@@ -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)
View
@@ -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)
View
@@ -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"
@@ -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.
@@ -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)
View
@@ -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"
@@ -38,6 +39,7 @@ import (
var (
TEXTDOMAIN = "snappy"
locale gettext.Catalog
+ localeStr string
translations gettext.Translations
)
@@ -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.