Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Fixing issues with language name translation

Cette révision appartient à :
akuma06 2017-07-11 23:34:59 +02:00 révisé par ewhal
Parent 21767877b1
révision fbb578b800
5 fichiers modifiés avec 69 ajouts et 10 suppressions

Voir le fichier

@ -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 {

Voir le fichier

@ -49,7 +49,7 @@
<a href="{{ URL.Parse("/search?c="+.Category+"_"+ .SubCategory) }}" title="{{ T(CategoryName(.Category, .SubCategory)) }}" class="category">
{{if .Languages[0] != "" }}
<a href="{{ URL.Parse("/search?c="+.Category+"_"+ .SubCategory +"&lang=") }}{{ range key, language := .Languages }}{{ language }}{{ if len(.Languages) > 1 && (key+1) < len(.Languages) }},{{ end }}{{ end }}">
<img src="/img/blank.gif" alt="{{ LanguageName(.Languages[0], T) }}" class="flag flag-{{ (len(.Languages) == 1) ? .Languages[0].Flag(false) : "multiple" }}" title=" {{ range key, language := .Languages }}{{ LanguageName(language, T) }}{{ if len(.Languages) > 1 && (key+1) < len(.Languages) }},{{ end }}{{ end }}">
<img src="/img/blank.gif" alt="{{ LanguageNameFromCode(.Languages[0], T) }}" class="flag flag-{{ (len(.Languages) == 1) ? FlagCode(.Languages[0]) : "multiple" }}" title="{{ range key, language := .Languages }}{{ LanguageNameFromCode(language, T) }}{{ if len(.Languages) > 1 && (key+1) < len(.Languages) }},{{ end }}{{ end }}">
</a>
{{end}}
</a>

Voir le fichier

@ -36,7 +36,7 @@
<td class="tr-flag torrent-view-td torrent-info-data">
{{ range _, language := Torrent.Languages}}
{{ if language != "" }}
<span><img src="/img/blank.gif" alt="{{ LanguageName(language, T) }}" class="flag flag-{{ language.Flag(false) }}" title="{{ LanguageName(language, T) }}"></img> {{ LanguageName(language, T) }}</span>
<span><img src="/img/blank.gif" alt="{{ LanguageNameFromCode(language, T) }}" class="flag flag-{{ FlagCode(language) }}" title="{{ LanguageNameFromCode(language, T) }}"></img> {{ LanguageNameFromCode(language, T) }}</span>
{{end}}
{{end}}
</td>

Voir le fichier

@ -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
}

Voir le fichier

@ -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"])
}
}
}