From: william Date: Wed, 8 Feb 2017 00:35:01 +0000 (+0800) Subject: New Setting "Default Language" when creating user. X-Git-Tag: r1.9.0~58^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2e68cb6eb7f943379aa4f8a40854bc9dfd8d9bc4;p=gitblit.git New Setting "Default Language" when creating user. --- diff --git a/src/main/java/com/gitblit/wicket/pages/EditUserPage.html b/src/main/java/com/gitblit/wicket/pages/EditUserPage.html index 3bccdd59..48e5339f 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditUserPage.html +++ b/src/main/java/com/gitblit/wicket/pages/EditUserPage.html @@ -31,11 +31,12 @@ - - - - - + + + + + + diff --git a/src/main/java/com/gitblit/wicket/pages/EditUserPage.java b/src/main/java/com/gitblit/wicket/pages/EditUserPage.java index 72dee6b6..0a6981b3 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditUserPage.java +++ b/src/main/java/com/gitblit/wicket/pages/EditUserPage.java @@ -20,15 +20,18 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Locale; import org.apache.wicket.PageParameters; import org.apache.wicket.behavior.SimpleAttributeModifier; import org.apache.wicket.extensions.markup.html.form.palette.Palette; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.CheckBox; +import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.model.CompoundPropertyModel; +import org.apache.wicket.model.IModel; import org.apache.wicket.model.Model; import org.apache.wicket.model.util.CollectionModel; import org.apache.wicket.model.util.ListModel; @@ -108,6 +111,30 @@ 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; + } + + 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); Form form = new Form("editForm", model) { private static final long serialVersionUID = 1L; @@ -123,6 +150,10 @@ public class EditUserPage extends RootSubPage { error(getString("gb.pleaseSetUsername")); return; } + Language lang = language.getObject(); + if (lang != null) { + userModel.getPreferences().setLocale(lang.code); + } // force username to lower-case userModel.username = userModel.username.toLowerCase(); String username = userModel.username; @@ -251,7 +282,10 @@ public class EditUserPage extends RootSubPage { form.add(confirmPasswordField.setEnabled(editCredentials)); form.add(new TextField("displayName").setEnabled(editDisplayName)); form.add(new TextField("emailAddress").setEnabled(editEmailAddress)); + + DropDownChoice choice = new DropDownChoice("language",language,languages ); + form.add( choice.setEnabled(languages.size()>0) ); if (userModel.canAdmin() && !userModel.canAdmin) { // user inherits Admin permission // display a disabled-yet-checked checkbox diff --git a/src/main/java/com/gitblit/wicket/pages/Language.java b/src/main/java/com/gitblit/wicket/pages/Language.java new file mode 100644 index 00000000..6eafb4dc --- /dev/null +++ b/src/main/java/com/gitblit/wicket/pages/Language.java @@ -0,0 +1,21 @@ +package com.gitblit.wicket.pages; + +import java.io.Serializable; + +public class Language implements Serializable { + + private static final long serialVersionUID = 1L; + + final String name; + final String code; + + public Language(String name, String code) { + this.name = name; + this.code = code; + } + + @Override + public String toString() { + return name + " (" + code + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/com/gitblit/wicket/pages/UserPage.java b/src/main/java/com/gitblit/wicket/pages/UserPage.java index ea68f25b..25a2249b 100644 --- a/src/main/java/com/gitblit/wicket/pages/UserPage.java +++ b/src/main/java/com/gitblit/wicket/pages/UserPage.java @@ -166,12 +166,9 @@ public class UserPage extends RootPage { navLinks.add(menu); } - - private void addPreferences(UserModel user) { - // add preferences - Form prefs = new Form("prefsForm"); - - List languages = Arrays.asList( + + public static List getLanguages(){ + return Arrays.asList( new Language("Deutsch","de"), new Language("English","en"), new Language("Español", "es"), @@ -185,6 +182,13 @@ public class UserPage extends RootPage { new Language("Português", "pt_BR"), new Language("簡體中文", "zh_CN"), new Language("正體中文", "zh_TW")); + } + + private void addPreferences(UserModel user) { + // add preferences + Form prefs = new Form("prefsForm"); + + List languages = getLanguages(); Locale locale = user.getPreferences().getLocale(); if (locale == null) { @@ -315,22 +319,4 @@ public class UserPage extends RootPage { add(new Fragment("sshKeysLink", "sshKeysLinkFragment", this).setRenderBodyOnly(true)); add(keysTab.setRenderBodyOnly(true)); } - - private class Language implements Serializable { - - private static final long serialVersionUID = 1L; - - final String name; - final String code; - - public Language(String name, String code) { - this.name = name; - this.code = code; - } - - @Override - public String toString() { - return name + " (" + code +")"; - } - } }