Skip to content

Conversation

strangelookingnerd
Copy link
Contributor

@strangelookingnerd strangelookingnerd commented May 20, 2025

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

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests that demonstrate the feature works or the issue is fixed

@strangelookingnerd strangelookingnerd requested a review from a team as a code owner May 20, 2025 12:54
@@ -48,13 +49,14 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
Copy link
Contributor Author

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>
Copy link
Contributor Author

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;
Copy link
Contributor Author

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
Copy link
Contributor Author

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(
Copy link
Contributor Author

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() {
Copy link
Contributor Author

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());
Copy link
Contributor Author

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.

Comment on lines -174 to +195
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);
Copy link
Contributor Author

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());
Copy link
Contributor

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?

@jonesbusy
Copy link
Contributor

Thanks. Looks good to me

@jonesbusy jonesbusy merged commit 41dc8d4 into jenkinsci:main May 25, 2025
17 checks passed
@strangelookingnerd strangelookingnerd deleted the JENKINS-57456 branch May 25, 2025 17:52
jonesbusy added a commit that referenced this pull request May 26, 2025
@jonesbusy
Copy link
Contributor

Cause #307

Will probably revert on #308

jonesbusy added a commit that referenced this pull request May 26, 2025
@jonesbusy
Copy link
Contributor

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

test

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>

@strangelookingnerd
Copy link
Contributor Author

strangelookingnerd commented May 26, 2025

That's odd. I'll look into it.

Edit: https://github.com/jenkinsci/locale-plugin/pull/306/files#diff-7cc57c064ed9f1a223e0fbb172e68fbdf536de8208a59c5b99056201b72659ffR74

Should have been localeCode - I'll update the PR.

@jonesbusy
Copy link
Contributor

Cause also #310

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants