mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 21:28:11 +09:00 
			
		
		
		
	Use index of the supported tags to choose user lang (#15452)
Fix #14793. The previous implementation used the first return value of matcher.Match, which is the chosen language tag but may contain extensions such as de-DE-u-rg-chzzzz. As mentioned in the documentation of language package, matcher.Match also returns the index of the supported tags, so I think it is better to use it rather than manipulate the returned language tag.
This commit is contained in:
		| @@ -25,8 +25,9 @@ type LangType struct { | |||||||
| } | } | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| 	matcher  language.Matcher | 	matcher       language.Matcher | ||||||
| 	allLangs []LangType | 	allLangs      []LangType | ||||||
|  | 	supportedTags []language.Tag | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // AllLangs returns all supported langauages | // AllLangs returns all supported langauages | ||||||
| @@ -50,12 +51,12 @@ func InitLocales() { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	tags := make([]language.Tag, len(setting.Langs)) | 	supportedTags = make([]language.Tag, len(setting.Langs)) | ||||||
| 	for i, lang := range setting.Langs { | 	for i, lang := range setting.Langs { | ||||||
| 		tags[i] = language.Raw.Make(lang) | 		supportedTags[i] = language.Raw.Make(lang) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	matcher = language.NewMatcher(tags) | 	matcher = language.NewMatcher(supportedTags) | ||||||
| 	for i := range setting.Names { | 	for i := range setting.Names { | ||||||
| 		key := "locale_" + setting.Langs[i] + ".ini" | 		key := "locale_" + setting.Langs[i] + ".ini" | ||||||
| 		if err = i18n.SetMessageWithDesc(setting.Langs[i], setting.Names[i], localFiles[key]); err != nil { | 		if err = i18n.SetMessageWithDesc(setting.Langs[i], setting.Names[i], localFiles[key]); err != nil { | ||||||
| @@ -73,8 +74,9 @@ func InitLocales() { | |||||||
| } | } | ||||||
|  |  | ||||||
| // Match matches accept languages | // Match matches accept languages | ||||||
| func Match(tags ...language.Tag) (tag language.Tag, index int, c language.Confidence) { | func Match(tags ...language.Tag) language.Tag { | ||||||
| 	return matcher.Match(tags...) | 	_, i, _ := matcher.Match(tags...) | ||||||
|  | 	return supportedTags[i] | ||||||
| } | } | ||||||
|  |  | ||||||
| // locale represents the information of localization. | // locale represents the information of localization. | ||||||
|   | |||||||
| @@ -38,7 +38,7 @@ func Locale(resp http.ResponseWriter, req *http.Request) translation.Locale { | |||||||
| 	// The first element in the list is chosen to be the default language automatically. | 	// The first element in the list is chosen to be the default language automatically. | ||||||
| 	if len(lang) == 0 { | 	if len(lang) == 0 { | ||||||
| 		tags, _, _ := language.ParseAcceptLanguage(req.Header.Get("Accept-Language")) | 		tags, _, _ := language.ParseAcceptLanguage(req.Header.Get("Accept-Language")) | ||||||
| 		tag, _, _ := translation.Match(tags...) | 		tag := translation.Match(tags...) | ||||||
| 		lang = tag.String() | 		lang = tag.String() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user