From e5f29381eca3f28ef5792225516bbea7a3773898 Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Mon, 11 Nov 2019 18:13:11 +0100 Subject: [PATCH] Fix user preferences selecting the wrong preferred locale. Due to a wrong comparison, when loading the preferred locale in the user preferences page, in cases like `zh_CN` or `de_DE` the wrong locale would be chosen. As with too many things, the code is duplicated on the `UserPage` and the `EditUserPage`. And they differ. So extract the choosing of the preferred language for display into a method in the (more up-to-date) `UserPage` and call that from the `EditUserPage`. --- .../gitblit/wicket/pages/EditUserPage.java | 25 ++-------- .../com/gitblit/wicket/pages/UserPage.java | 49 ++++++++++--------- 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/gitblit/wicket/pages/EditUserPage.java b/src/main/java/com/gitblit/wicket/pages/EditUserPage.java index 5b0b0147..c6014e8f 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditUserPage.java +++ b/src/main/java/com/gitblit/wicket/pages/EditUserPage.java @@ -112,30 +112,11 @@ public class EditUserPage extends RootSubPage { final Palette teams = new Palette("teams", new ListModel( new ArrayList(userTeams)), new CollectionModel(app().users() .getAllTeamNames()), new StringChoiceRenderer(), 10, false); - Locale locale = userModel.getPreferences().getLocale(); - if (locale == null) { - locale = Locale.ENGLISH; - } + Locale locale = userModel.getPreferences().getLocale(); List languages = UserPage.getLanguages(); - Language preferredLanguage = null; - if (locale != null) { - String localeCode = locale.getLanguage(); - if (!StringUtils.isEmpty(locale.getCountry())) { - localeCode += "_" + locale.getCountry(); - } - - for (Language lang : languages) { - if (lang.code.equals(localeCode)) { - // language_COUNTRY match - preferredLanguage = lang; - } else if (preferredLanguage != null && lang.code.startsWith(locale.getLanguage())) { - // language match - preferredLanguage = lang; - } - } - } - final IModel language = Model.of(preferredLanguage); + Language preferredLanguage = UserPage.getPreferredLanguage(locale, languages); + final IModel language = Model.of(preferredLanguage); Form form = new Form("editForm", model) { private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/gitblit/wicket/pages/UserPage.java b/src/main/java/com/gitblit/wicket/pages/UserPage.java index b457304b..4fc778a6 100644 --- a/src/main/java/com/gitblit/wicket/pages/UserPage.java +++ b/src/main/java/com/gitblit/wicket/pages/UserPage.java @@ -15,7 +15,6 @@ */ package com.gitblit.wicket.pages; -import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -167,7 +166,7 @@ public class UserPage extends RootPage { navLinks.add(menu); } - public static List getLanguages(){ + static List getLanguages(){ return Arrays.asList( new Language("Česky","cs"), new Language("Deutsch","de"), @@ -184,13 +183,32 @@ public class UserPage extends RootPage { new Language("簡體中文", "zh_CN"), new Language("正體中文", "zh_TW")); } - + + static Language getPreferredLanguage(Locale locale, List languages) { + Language preferredLanguage = null; + if (locale != null) { + String localeCode = locale.getLanguage(); + if (!StringUtils.isEmpty(locale.getCountry())) { + localeCode += "_" + locale.getCountry(); + } + + for (Language language : languages) { + if (language.code.equals(localeCode)) { + // language_COUNTRY match + preferredLanguage = language; + } else if (preferredLanguage == null && language.code.startsWith(locale.getLanguage())) { + // language match + preferredLanguage = language; + } + } + } + return preferredLanguage; + } + private void addPreferences(UserModel user) { // add preferences Form prefs = new Form("prefsForm"); - List languages = getLanguages(); - Locale locale = user.getPreferences().getLocale(); if (locale == null) { // user has not specified language preference @@ -203,28 +221,11 @@ public class UserPage extends RootPage { if (sessionLocale != null) { locale = sessionLocale; } - } else { - } } - Language preferredLanguage = null; - if (locale != null) { - String localeCode = locale.getLanguage(); - if (!StringUtils.isEmpty(locale.getCountry())) { - localeCode += "_" + locale.getCountry(); - } - - for (Language language : languages) { - if (language.code.equals(localeCode)) { - // language_COUNTRY match - preferredLanguage = language; - } else if (preferredLanguage != null && language.code.startsWith(locale.getLanguage())) { - // language match - preferredLanguage = language; - } - } - } + List languages = getLanguages(); + Language preferredLanguage = getPreferredLanguage(locale, languages); final IModel displayName = Model.of(user.getDisplayName()); final IModel emailAddress = Model.of(user.emailAddress == null ? "" : user.emailAddress); -- 2.39.5