diff --git a/conf/app.conf b/conf/app.conf index 3e6a0dc8..72d1e3ce 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -20,5 +20,4 @@ staticBaseUrl = "https://cdn.casbin.org" isDemoMode = false batchSize = 100 ldapServerPort = 389 -languages = en,zh,es,fr,de,id,ja,ko,ru,vi quota = {"organization": -1, "user": -1, "application": -1, "provider": -1} diff --git a/conf/conf.go b/conf/conf.go index 4b201b48..c06a68cb 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -112,13 +112,8 @@ func GetLanguage(language string) string { if len(language) < 2 { return "en" - } - - language = language[0:2] - if strings.Contains(GetConfigString("languages"), language) { - return language } else { - return "en" + return language[0:2] } } diff --git a/i18n/util.go b/i18n/util.go index 3ecc60c1..8b2d6b56 100644 --- a/i18n/util.go +++ b/i18n/util.go @@ -73,23 +73,27 @@ func applyData(data1 *I18nData, data2 *I18nData) { } } -func Translate(lang string, error string) string { - tokens := strings.SplitN(error, ":", 2) - if !strings.Contains(error, ":") || len(tokens) != 2 { - return "Translate Error: " + error +func Translate(language string, errorText string) string { + tokens := strings.SplitN(errorText, ":", 2) + if !strings.Contains(errorText, ":") || len(tokens) != 2 { + return fmt.Sprintf("Translate error: the error text doesn't contain \":\", errorText = %s", errorText) } - if langMap[lang] == nil { - file, _ := f.ReadFile("locales/" + lang + "/data.json") + if langMap[language] == nil { + file, err := f.ReadFile(fmt.Sprintf("locales/%s/data.json", language)) + if err != nil { + return fmt.Sprintf("Translate error: the language \"%s\" is not supported, err = %s", language, err.Error()) + } + data := I18nData{} - err := util.JsonToStruct(string(file), &data) + err = util.JsonToStruct(string(file), &data) if err != nil { panic(err) } - langMap[lang] = data + langMap[language] = data } - res := langMap[lang][tokens[0]][tokens[1]] + res := langMap[language][tokens[0]][tokens[1]] if res == "" { res = tokens[1] }