-
Notifications
You must be signed in to change notification settings - Fork 17
Support user language preference #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -48,13 +49,14 @@ | |||
<type>pom</type> | |||
<scope>import</scope> | |||
</dependency> | |||
<dependency> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed from what I can tell.
<dependencies> | ||
<dependency> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be removed once we migrate to Java 21 (see TODO in hudson.plugins.locale.PluginImpl#parse)
PluginImpl plugin = PluginImpl.get(); | ||
if (plugin != null && plugin.isIgnoreAcceptLanguage()) { | ||
request = new HttpServletRequestWrapper((HttpServletRequest) request) { | ||
final Locale locale; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took over the implementation from #8
@@ -68,4 +86,17 @@ public Locale nextElement() { | |||
public void destroy() { | |||
// nop | |||
} | |||
|
|||
@CheckForNull |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took over the implementation from #8
|
||
public static final String USE_BROWSER_LOCALE = "USE_BROWSER_LOCALE"; | ||
|
||
// Set of allowed locales | ||
private static final Set<String> ALLOWED_LOCALES = new HashSet<>(Arrays.asList( | ||
public static final Set<String> ALLOWED_LOCALES = Set.of( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed visibility as it is reused in UserLocaleProperty
.
@@ -119,38 +133,34 @@ public void setSystemLocale(String systemLocale) { | |||
} | |||
} | |||
|
|||
public String getUseBrowserLocale() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused, thus removed.
throw new IllegalArgumentException(s + " is not a valid locale"); | ||
} | ||
// TODO: Migrate to Locale.of() once we upgrade to Java 21 | ||
return LocaleUtils.toLocale(s.trim()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simpler implementation using commons-lang3
.
String originalLocaleDisplay = String.format( | ||
"Use Default Locale - %s (%s)", originalLocale.getDisplayName(), originalLocale.toString()); | ||
String originalLocaleDisplay = | ||
String.format("Use Default Locale - %s (%s)", originalLocale.getDisplayName(), originalLocale); | ||
items.add(new ListBoxModel.Option(originalLocaleDisplay, USE_BROWSER_LOCALE)); | ||
|
||
Locale[] availableLocales = Locale.getAvailableLocales(); | ||
List<Locale> sortedLocales = Arrays.stream(availableLocales) | ||
.filter(locale -> ALLOWED_LOCALES.contains(locale.toString())) // Ensure no empty or null locale strings | ||
.sorted((locale1, locale2) -> locale1.getDisplayName().compareTo(locale2.getDisplayName())) | ||
.collect(Collectors.toList()); | ||
.sorted(Comparator.comparing(Locale::getDisplayName)) | ||
.toList(); | ||
|
||
for (Locale locale : sortedLocales) { | ||
String displayText = String.format("%s - %s", locale.getDisplayName(), locale.toString()); | ||
String displayText = String.format("%s - %s", locale.getDisplayName(), locale); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some cleanup while at it.
@@ -59,6 +76,7 @@ public Locale nextElement() { | |||
}; | |||
} | |||
}; | |||
((HttpServletResponse) response).addHeader("X-Jenkins-Language", locale.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understood correctly it's only used by test to know which language was set?
Thanks. Looks good to me |
This reverts commit 41dc8d4.
I've tested locally this morning but not able (yet) to reproduce the index out of bound However when trying to change my locale on my profile I cannot. I always get reverted to the default locale When I check the XML I get an empty string after saving whatever locale on my user profile <hudson.plugins.locale.user.UserLocaleProperty plugin="locale@584.v41dc8d4f9377">
<localeCode></localeCode>
</hudson.plugins.locale.user.UserLocaleProperty> |
That's odd. I'll look into it. Should have been |
Cause also #310 |
Picking up the work from #8 to solve https://issues.jenkins.io/browse/JENKINS-57456 and #234
I'll comment the code below to explain some of the details.
Testing done
Added unit tests and did some manual testing to verify the new user property works as expected.
Submitter checklist