fix(web): i18n explicit lang ignored (#3961)

This fixes an issue where explicit languages such as nl-NL, pt-PT, zh-CH, and zh-TW are ignored even though they're supported.
pull/3967/head^2
James Elliott 2022-09-06 09:09:33 +10:00 committed by GitHub
parent 9c5a8b77aa
commit cd9bfe3f90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 8 deletions

View File

@ -12,5 +12,8 @@ files:
"en-EN": en
"es-ES": es
"fr-FR": fr
"nl-NL": nl
"pt-PT": pt
"ru-RU": ru
"zh-CH": zh
...

View File

@ -11,6 +11,7 @@ import (
"net/http"
"path"
"path/filepath"
"strings"
"github.com/valyala/fasthttp"
@ -71,8 +72,23 @@ func newLocalesEmbeddedHandler() (handler fasthttp.RequestHandler) {
entries, err := locales.ReadDir("locales")
if err == nil {
for _, entry := range entries {
if entry.IsDir() && len(entry.Name()) == 2 {
languages = append(languages, entry.Name())
if entry.IsDir() {
var lng string
switch len(entry.Name()) {
case 2:
lng = entry.Name()
case 0:
continue
default:
lng = strings.SplitN(entry.Name(), "-", 2)[0]
}
if utils.IsStringInSlice(lng, languages) {
continue
}
languages = append(languages, lng)
}
}
}
@ -94,7 +110,7 @@ func newLocalesEmbeddedHandler() (handler fasthttp.RequestHandler) {
var data []byte
if data, err = locales.ReadFile(fmt.Sprintf("locales/%s/%s.json", locale, namespace)); err != nil {
if variant != "" && utils.IsStringInSliceFold(language, languages) {
if utils.IsStringInSliceFold(language, languages) {
data = []byte("{}")
}

View File

@ -26,15 +26,16 @@ i18n.use(Backend)
de: ["en"],
es: ["en"],
fr: ["en"],
"nl-NL": ["en"],
"pt-PT": ["en"],
nl: ["en"],
pt: ["en"],
ru: ["en"],
sv: ["en"],
"sv-SE": ["sv", "en"],
"zh-CN": ["en"],
"zh-TW": ["en"],
zh: ["en"],
"zh-CN": ["zh", "en"],
"zh-TW": ["zh", "en"],
},
supportedLngs: ["en", "de", "es", "fr", "nl-NL", "pt-PT", "ru", "sv", "sv-SE", "zh-CN", "zh-TW"],
supportedLngs: ["en", "de", "es", "fr", "nl", "pt", "ru", "sv", "sv-SE", "zh", "zh-CN", "zh-TW"],
lowerCaseLng: false,
nonExplicitSupportedLngs: true,
interpolation: {