diff --git a/controllers/template_functions.go b/controllers/template_functions.go index 74f57862..3aa06d99 100644 --- a/controllers/template_functions.go +++ b/controllers/template_functions.go @@ -141,6 +141,9 @@ func templateFunctions(vars jet.VarMap) jet.VarMap { }) vars.Set("Sukebei", config.IsSukebei) vars.Set("getDefaultLanguage", publicSettings.GetDefaultLanguage) + vars.Set("FlagCode", func(languageCode string) string { + return publicSettings.Flag(languageCode) + }) vars.Set("getAvatar", func(hash string, size int) string { return "https://www.gravatar.com/avatar/" + hash + "?s=" + strconv.Itoa(size) }) @@ -197,11 +200,22 @@ func templateFunctions(vars jet.VarMap) jet.VarMap { langs := strings.Split(lang.Name, ", ") tags := strings.Split(lang.Tag, ", ") for k := range langs { - langs[k] = publicSettings.Translate(tags[k], string(T("language_code"))) + langs[k] = strings.Title(publicSettings.Translate(tags[k], string(T("language_code")))) } return strings.Join(langs, ", ") } - return lang.Translate(T("language_code")) + return strings.Title(lang.Translate(T("language_code"))) + }) + vars.Set("LanguageNameFromCode", func(languageCode string, T publicSettings.TemplateTfunc) string { + if strings.Contains(languageCode, ",") { + tags := strings.Split(languageCode, ", ") + var langs []string + for k := range langs { + langs = append(langs, strings.Title(publicSettings.Translate(tags[k], string(T("language_code"))))) + } + return strings.Join(langs, ", ") + } + return strings.Title(publicSettings.Translate(languageCode, string(T("language_code")))) }) vars.Set("fileSize", func(filesize int64, T publicSettings.TemplateTfunc) template.HTML { if filesize == 0 { diff --git a/templates/site/torrents/listing.jet.html b/templates/site/torrents/listing.jet.html index 4601ed2b..df654e4b 100644 --- a/templates/site/torrents/listing.jet.html +++ b/templates/site/torrents/listing.jet.html @@ -49,7 +49,7 @@ {{if .Languages[0] != "" }} - {{ LanguageName(.Languages[0], T) }} + {{ LanguageNameFromCode(.Languages[0], T) }} {{end}} diff --git a/templates/site/torrents/view.jet.html b/templates/site/torrents/view.jet.html index 9d5a0301..11c2e0fc 100644 --- a/templates/site/torrents/view.jet.html +++ b/templates/site/torrents/view.jet.html @@ -36,7 +36,7 @@ {{ range _, language := Torrent.Languages}} {{ if language != "" }} - {{ LanguageName(language, T) }} {{ LanguageName(language, T) }} + {{ LanguageNameFromCode(language, T) }} {{ LanguageNameFromCode(language, T) }} {{end}} {{end}} diff --git a/utils/publicSettings/publicSettings.go b/utils/publicSettings/publicSettings.go index a04125ab..3285c623 100644 --- a/utils/publicSettings/publicSettings.go +++ b/utils/publicSettings/publicSettings.go @@ -233,17 +233,26 @@ func (lang *Language) Translate(languageCode template.HTML) string { // Translate accepts a languageCode in string and translate the language to the language from the language code in to func Translate(languageCode string, to string) string { langTranslate := display.Tags(getParentTag(to)) - return langTranslate.Name(languageCode) + translated := langTranslate.Name(glang.Make(languageCode)) + if translated == "Root" { + return "" + } + return translated } // Flag reads the language's country code and return the country's flag if national true or the international flag for the language func (lang *Language) Flag(national bool) string { if national { - languageSplit := strings.Split(lang.Tag, "-") - if len(languageSplit) > 1 { - return languageSplit[1] - } - return lang.Tag + return Flag(lang.Tag) } return lang.Code } + +// Flag reads the language's country code and return the country's flag if national true or the international flag for the language +func Flag(languageCode string) string { + languageSplit := strings.Split(languageCode, "-") + if len(languageSplit) > 1 { + return languageSplit[1] + } + return languageCode +} diff --git a/utils/publicSettings/publicSettings_test.go b/utils/publicSettings/publicSettings_test.go index 265a1eb8..d06ce9a6 100644 --- a/utils/publicSettings/publicSettings_test.go +++ b/utils/publicSettings/publicSettings_test.go @@ -59,3 +59,39 @@ func TestLanguages(t *testing.T) { fmt.Printf("Name of the language in %s: %s\n", displayLang.String(), n.Name(lang)) } } + +func TestTranslate(t *testing.T) { + conf := config.Get().I18n + conf.Directory = path.Join("..", "..", conf.Directory) + var retriever UserRetriever // not required during initialization + err := InitI18n(conf, retriever) + if err != nil { + t.Errorf("failed to initialize language translations: %v", err) + } + + T, _ := GetDefaultTfunc() + test := []map[string]string{ + { + "test": "", + "result": "", + }, + { + "test": "fr-fr", + "result": "French (France)", + }, + { + "test": "fr", + "result": "French", + }, + { + "test": "fredfef", + "result": "", + }, + } + for _, langTest := range test { + result := Translate(langTest["test"], T("language_code")) + if result != langTest["result"] { + t.Errorf("Result from Translate function different from the expected: have '%s', wants '%s'", result, langTest["result"]) + } + } +}