-
Notifications
You must be signed in to change notification settings - Fork 4.1k
TRUNK-6341: Intelligent Locale Matching using RFC 4647 #5524
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
base: master
Are you sure you want to change the base?
Conversation
web/src/main/java/org/openmrs/web/filter/initialization/InitializationFilter.java
Show resolved
Hide resolved
| availableLocales.add(new Locale("es")); // Spanish | ||
| availableLocales.add(new Locale("pt", "BR")); // Portuguese (Brazil) | ||
|
|
||
| // Use the protected constructor to inject test locales |
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.
Kindly as a best practice let’s remove comments that restate what the code already conveys. Clean code is easy to read and maintain.
| * | ||
| * @param availableLocales the set of locales to be used as available locales | ||
| */ | ||
| protected CustomResourceLoader(Set<Locale> availableLocales) { |
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.
Default visibility is probably the right thing here.
| protected CustomResourceLoader(Set<Locale> availableLocales) { | |
| CustomResourceLoader(Set<Locale> availableLocales) { |
| * @param availableLocales the set of locales to be used as available locales | ||
| */ | ||
| protected CustomResourceLoader(Set<Locale> availableLocales) { | ||
| this.resources = new HashMap<>(); |
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.
Since there's no sensible way to set this, just returning the empty map is probably best.
| this.resources = new HashMap<>(); | |
| this.resources = Collections.emptyMap(); |
| public Locale findBestMatchLocale(String acceptLanguageHeader) { | ||
| // Return default locale if header is null or empty | ||
| if (acceptLanguageHeader == null || acceptLanguageHeader.trim().isEmpty()) { | ||
| return Locale.ENGLISH; |
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.
| return Locale.ENGLISH; | |
| return LocaleUtility.getDefaultLocale(); |
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 may slightly disrupt the tests, but comparing assertEquals(Locale.ENGLISH.getLanguage(), findBestMatchLocale(null).getLanguage()); as suggested by the docs should work.
| Locale matchedLocale = Locale.lookup(languageRanges, availableLocalesList); | ||
|
|
||
| // Return matched locale or default to English if no match found | ||
| return matchedLocale != null ? matchedLocale : Locale.ENGLISH; |
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.
| return matchedLocale != null ? matchedLocale : Locale.ENGLISH; | |
| return matchedLocale != null ? matchedLocale : LocaleUtility.getDefaultLocale(); |
TRUNK-6341: Intelligent Locale Matching using RFC 4647
Description of what I changed
I implemented intelligent locale matching to replace the simple, exact-match-only logic in
InitializationFilter.Previously, if a user requested
fr-BE(Belgian French) but OpenMRS only hadfr(French), the system would default to English. This change uses RFC 4647 (java.util.Locale.lookup) to properly handle:fr-BEnow correctly falls back tofr.fr;q=0.9, en;q=0.8are respected.Specific Changes:
CustomResourceLoader.java:findBestMatchLocale(String header)to handle the parsing logic.protectedconstructor to allow injectingavailableLocales. This ensures the logic is testable without needing complex file system mocks (addressing previous review feedback).InitializationFilter.java:Accept-Languageheader and call the new loader method.CustomResourceLoaderTest.java:Issue I worked on
see https://issues.openmrs.org/browse/TRUNK-6341
Checklist: I completed these to help reviewers :)
mvn clean packageright before creating this pull request and added all formatting changes to my commit.