Skip to content

Commit

Permalink
CONSOLE-2535: Internationalize login page
Browse files Browse the repository at this point in the history
  • Loading branch information
jhadvig committed Mar 31, 2021
1 parent c7787fb commit 298cd4c
Show file tree
Hide file tree
Showing 29 changed files with 9,602 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/spf13/pflag v1.0.5
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6
golang.org/x/text v0.3.3
gopkg.in/ldap.v2 v2.5.1
k8s.io/api v0.19.2
k8s.io/apimachinery v0.19.2
Expand Down
53 changes: 53 additions & 0 deletions pkg/server/login/locales.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package login

import "golang.org/x/text/language"

func getLocale(langBase string) map[string]string {
switch langBase {
case language.English.String():
return locale_en
case language.Chinese.String():
return locale_zh
case language.Japanese.String():
return locale_jp
case language.Korean.String():
return locale_ko
default:
return locale_en
}
}

func getPrefferedLang(acceptLangHeader string) string {
matcher := language.NewMatcher(supportedLangs)
userPrefs, _, err := language.ParseAcceptLanguage(acceptLangHeader)
if err != nil {
// if error occurs, fallback to English
return language.English.String()
}
tag, _, _ := matcher.Match(userPrefs...)
base, _ := tag.Base()
return base.String()
}

var supportedLangs = []language.Tag{
language.English, // en - first language is fallback
language.Chinese, // zh
language.Japanese, // jp
language.Korean, // ko
}

var locale_en = map[string]string{
"": "",
}

var locale_zh = map[string]string{
"": "",
}

var locale_jp = map[string]string{
"": "",
}

var locale_ko = map[string]string{
"": "",
}
4 changes: 4 additions & 0 deletions pkg/server/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type LoginForm struct {

Names LoginFormFields
Values LoginFormFields

Locale map[string]string
}

type LoginFormFields struct {
Expand Down Expand Up @@ -128,6 +130,8 @@ func (l *Login) handleLoginForm(w http.ResponseWriter, req *http.Request) {
return
}

form.Locale = getLocale(getPrefferedLang(req.Header.Get("Accept-Language")))

form.ErrorCode = req.URL.Query().Get(reasonParam)
if len(form.ErrorCode) > 0 {
if msg, hasMsg := errorMessages[form.ErrorCode]; hasMsg {
Expand Down
16 changes: 16 additions & 0 deletions vendor/golang.org/x/text/internal/language/common.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions vendor/golang.org/x/text/internal/language/compact.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions vendor/golang.org/x/text/internal/language/compact/compact.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 298cd4c

Please sign in to comment.