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 "en-EN": en
"es-ES": es "es-ES": es
"fr-FR": fr "fr-FR": fr
"nl-NL": nl
"pt-PT": pt
"ru-RU": ru "ru-RU": ru
"zh-CH": zh
... ...

View File

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
@ -71,8 +72,23 @@ func newLocalesEmbeddedHandler() (handler fasthttp.RequestHandler) {
entries, err := locales.ReadDir("locales") entries, err := locales.ReadDir("locales")
if err == nil { if err == nil {
for _, entry := range entries { for _, entry := range entries {
if entry.IsDir() && len(entry.Name()) == 2 { if entry.IsDir() {
languages = append(languages, entry.Name()) 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 var data []byte
if data, err = locales.ReadFile(fmt.Sprintf("locales/%s/%s.json", locale, namespace)); err != nil { 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("{}") data = []byte("{}")
} }

View File

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