]> source.dussan.org Git - gitblit.git/commitdiff
Fix user preferences selecting the wrong preferred locale. 1324/head
authorFlorian Zschocke <florian.zschocke@devolo.de>
Mon, 11 Nov 2019 17:13:11 +0000 (18:13 +0100)
committerFlorian Zschocke <florian.zschocke@devolo.de>
Mon, 11 Nov 2019 17:13:11 +0000 (18:13 +0100)
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`.

src/main/java/com/gitblit/wicket/pages/EditUserPage.java
src/main/java/com/gitblit/wicket/pages/UserPage.java

index 5b0b0147829950ff9a3b6ff5162cf0d235870548..c6014e8feea405adbfd8fdc29b5a2eb4fe45155c 100644 (file)
@@ -112,30 +112,11 @@ public class EditUserPage extends RootSubPage {
                final Palette<String> teams = new Palette<String>("teams", new ListModel<String>(\r
                                new ArrayList<String>(userTeams)), new CollectionModel<String>(app().users()\r
                                .getAllTeamNames()), new StringChoiceRenderer(), 10, false);\r
-               Locale locale = userModel.getPreferences().getLocale();\r
-               if (locale == null) {\r
-                       locale = Locale.ENGLISH;\r
-               }\r
 \r
+               Locale locale = userModel.getPreferences().getLocale();\r
                List<Language> languages = UserPage.getLanguages();\r
-               Language preferredLanguage = null;\r
-               if (locale != null) {\r
-                       String localeCode = locale.getLanguage();\r
-                       if (!StringUtils.isEmpty(locale.getCountry())) {\r
-                               localeCode += "_" + locale.getCountry();\r
-                       }\r
-\r
-                       for (Language lang : languages) {\r
-                               if (lang.code.equals(localeCode)) {\r
-                                       // language_COUNTRY match\r
-                                       preferredLanguage = lang;\r
-                               } else if (preferredLanguage != null && lang.code.startsWith(locale.getLanguage())) {\r
-                                       // language match\r
-                                       preferredLanguage = lang;\r
-                               }\r
-                       }\r
-               }\r
-               final IModel<Language> language = Model.of(preferredLanguage);          \r
+               Language preferredLanguage = UserPage.getPreferredLanguage(locale, languages);\r
+               final IModel<Language> language = Model.of(preferredLanguage);\r
                Form<UserModel> form = new Form<UserModel>("editForm", model) {\r
 \r
                        private static final long serialVersionUID = 1L;\r
index b457304bc12f71314ae28a3f780059f318f7b534..4fc778a64cedc5683765e60a4df1fde330263ca6 100644 (file)
@@ -15,7 +15,6 @@
  */\r
 package com.gitblit.wicket.pages;\r
 \r
-import java.io.Serializable;\r
 import java.util.ArrayList;\r
 import java.util.Arrays;\r
 import java.util.Collections;\r
@@ -167,7 +166,7 @@ public class UserPage extends RootPage {
                navLinks.add(menu);\r
        }\r
        \r
-       public static List<Language> getLanguages(){\r
+       static List<Language> getLanguages(){\r
                return  Arrays.asList(\r
                                new Language("Česky","cs"),\r
                                new Language("Deutsch","de"),\r
@@ -184,13 +183,32 @@ public class UserPage extends RootPage {
                                new Language("簡體中文", "zh_CN"),\r
                                new Language("正體中文", "zh_TW"));\r
        }\r
-       \r
+\r
+       static Language getPreferredLanguage(Locale locale, List<Language> languages) {\r
+               Language preferredLanguage = null;\r
+               if (locale != null) {\r
+                       String localeCode = locale.getLanguage();\r
+                       if (!StringUtils.isEmpty(locale.getCountry())) {\r
+                               localeCode += "_" + locale.getCountry();\r
+                       }\r
+\r
+                       for (Language language : languages) {\r
+                               if (language.code.equals(localeCode)) {\r
+                                       // language_COUNTRY match\r
+                                       preferredLanguage = language;\r
+                               } else if (preferredLanguage == null && language.code.startsWith(locale.getLanguage())) {\r
+                                       // language match\r
+                                       preferredLanguage = language;\r
+                               }\r
+                       }\r
+               }\r
+               return preferredLanguage;\r
+       }\r
+\r
        private void addPreferences(UserModel user) {\r
                // add preferences\r
                Form<Void> prefs = new Form<Void>("prefsForm");\r
 \r
-               List<Language> languages = getLanguages();\r
-\r
                Locale locale = user.getPreferences().getLocale();\r
                if (locale == null) {\r
                        // user has not specified language preference\r
@@ -203,28 +221,11 @@ public class UserPage extends RootPage {
                                if (sessionLocale != null) {\r
                                        locale = sessionLocale;\r
                                }\r
-                       } else {\r
-\r
                        }\r
                }\r
 \r
-               Language preferredLanguage = null;\r
-               if (locale != null) {\r
-                       String localeCode = locale.getLanguage();\r
-                       if (!StringUtils.isEmpty(locale.getCountry())) {\r
-                               localeCode += "_" + locale.getCountry();\r
-                       }\r
-\r
-                       for (Language language : languages) {\r
-                               if (language.code.equals(localeCode)) {\r
-                                       // language_COUNTRY match\r
-                                       preferredLanguage = language;\r
-                               } else if (preferredLanguage != null && language.code.startsWith(locale.getLanguage())) {\r
-                                       // language match\r
-                                       preferredLanguage = language;\r
-                               }\r
-                       }\r
-               }\r
+               List<Language> languages = getLanguages();\r
+               Language preferredLanguage = getPreferredLanguage(locale, languages);\r
 \r
                final IModel<String> displayName = Model.of(user.getDisplayName());\r
                final IModel<String> emailAddress = Model.of(user.emailAddress == null ? "" : user.emailAddress);\r