Skip to content
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

TRUNK-6216: LocaleUtility#fromSpecification should be updated to support BCP-47 language tags. #4523

Merged
merged 2 commits into from
Mar 1, 2024

Conversation

mherman22
Copy link
Member

@mherman22 mherman22 commented Jan 1, 2024

Description of what I changed

LocaleUtility#fromSpecification parses language tags in the format en_USinto Java locales. This is the format used for Locales in versions of Java up to Java 6. With Java 7, Java began supporting standard BCP-47 language tags (en-US) as well as the underscore variant.

Issue I worked on

see https://openmrs.atlassian.net/browse/TRUNK-6216

Checklist: I completed these to help reviewers :)

  • My IDE is configured to follow the code style of this project.

    No? Unsure? -> configure your IDE, format the code and add the changes with git add . && git commit --amend

  • I have added tests to cover my changes. (If you refactored
    existing code that was well tested you do not have to add tests)

    No? -> write tests and add them to this commit git add . && git commit --amend

  • I ran mvn clean package right before creating this pull request and
    added all formatting changes to my commit.

    No? -> execute above command

  • All new and existing tests passed.

    No? -> figure out why and add the fix to your commit. It is your responsibility to make sure your code works.

  • My pull request is based on the latest changes of the master branch.

    No? Unsure? -> execute command git pull --rebase upstream master

Copy link
Member

@ibacher ibacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mherman22 I missed this PR! Looks good. The only thing I'll say is that the reason the ticket was written as "return null if an IllegalArgumentException is thrown" is because LocaleUtility.toLocale() should already handle everything we do and more.

Unless was a test failing or something?

@mherman22
Copy link
Member Author

mherman22 commented Feb 23, 2024

@ibacher, yes, using Apache common-Lang’s LocaleUtil#toLocale() and throwing a null for an IllegalArgumentException, we are going to fail this test:

public void getDefaultLocale_shouldNotFailWithBogusGlobalPropertyValue() {
Context.getAdministrationService().saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, asdfasdf"));
Context.getAdministrationService().saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "asdfasdf"));
// check for nonnullness
assertNotNull(LocaleUtility.getDefaultLocale());
Context.getAdministrationService().saveGlobalProperty(
new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, ""));
}
with this snippet, the cause →
if (!localeList.contains(LocaleUtility.fromSpecification(gp.getPropertyValue().trim()))) {
String value = gp.getPropertyValue();
gp.setPropertyValue(LocaleUtility.getDefaultLocale().toString());
throw new APIException((Context.getMessageSourceService().getMessage(
"general.locale.defaultNotInAllowedLocalesList", new Object[] { value }, null)));
}

@mherman22
Copy link
Member Author

mherman22 commented Feb 26, 2024

@mherman22 I missed this PR! Looks good. The only thing I'll say is that the reason the ticket was written as "return null if an IllegalArgumentException is thrown" is because LocaleUtility.toLocale() should already handle everything we do and more.

Unless was a test failing or something?

thanks @ibacher i think it is now good. I decided to add an if-else check after the IllegalArgumentException has been caught that returns null if it is an invalidBCP-47 format like en-USA or en_USA and return the old implementation if it is something like asdfasdf.

@mherman22 mherman22 requested a review from ibacher February 27, 2024 10:04
@ibacher ibacher merged commit 8ed43b6 into openmrs:master Mar 1, 2024
6 checks passed
ibacher pushed a commit that referenced this pull request Mar 1, 2024
Seremba pushed a commit to Seremba/openmrs-core that referenced this pull request Mar 13, 2024
Wandji69 pushed a commit to Wandji69/openmrs-core that referenced this pull request Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants