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

OBPIH-6410 Allow configuring country specific localization #4681

Merged
merged 2 commits into from
Jun 20, 2024

Conversation

awalkowiak
Copy link
Collaborator

@jmiranda
Copy link
Member

@awalkowiak Please give me a few days to get to this one.

@@ -26,9 +26,41 @@ class LocalizationUtil {
return localizationService.currentLocale
}

/**
Copy link
Collaborator

Choose a reason for hiding this comment

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

An alternative to this would be to extend the Locale class and give it another constructor but I don't know if there's much advantage of that over what you've done

@@ -1449,7 +1449,7 @@ class ProductService {

Product addSynonymToProduct(String productId, String synonymTypeCodeName, String synonymValue, String localeName) {
Product product = Product.get(productId)
Locale locale = localeName ? new Locale(localeName) : null
Locale locale = localeName ? LocalizationUtil.getLocale(localeName) : null
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not a question to be resolved here, but I'm wondering if there's a fancier way to do this so that we don't need to bake all this localization logic into our services.

It'd be awesome if we could mark a Domain field as "localized" and then whenever we do a look up on that field it'd automatically also search for the synonyms.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I guess this is a more Synonyms related topic suiting for a TD discovery or some OBIG

locale.setDefault(new Locale(Holders.config.openboxes.locale.defaultLocale ?: "en"))
return locale
}

static List<Locale> getSupportedLocales() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since locales are static, we could consider caching them in a map (maybe in the LocalizationService) so that we don't need to reconstruct them every time we do a lookup. Currently, every time we call this method we're re-initializing all of the Locale objects. That's not a big deal since it's not really a lot of computation work, but still a good practice IMO.

If we wanted to get really fancy, we could even build that map on app startup based on the locales we have defined in our properties.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is also potentially something that I would move to the separate "further improvements in the supported locale" ticket, but hold until more reviews

@@ -0,0 +1 @@
default.number.format=\# \# \#, \# \# \#, \# \# \#0.00
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this being used yet?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was an "initial" request from the ticket, so I just added it here to not leave it empty

@@ -69,51 +69,11 @@ class LocalizationService {
*/
Properties getMessagesProperties(Locale locale) {
Properties messagesProperties = new Properties()
def messagesPropertiesFilename = (locale && locale.language != "en" && locale.language != 'null') ? "messages_${locale.language}.properties" : "messages.properties"
def messagesPropertiesFilename = (locale && locale.language != "en" && locale.language != 'null') ? "messages_${locale.toString()}.properties" : "messages.properties"
Copy link
Collaborator

@EWaterman EWaterman Jun 18, 2024

Choose a reason for hiding this comment

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

in what situations do we want locale.toString() vs locale.getDisplayName()? I see we use both

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We want to find a message properties file corresponding to the current locale. toString() concatenates language code with country code together with an underscore (or returns just language code if no country code). So for Spanish, we are getting just es -> messages_es.properties and for Mexican Spanish, we will get es_MX -> messages_es_MX.properties


def resource = grailsResourceLocator.findResourceForURI('classpath:' + messagesPropertiesFilename)
messagesProperties.load(resource.inputStream)

return messagesProperties
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

unused code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yep

Copy link
Collaborator

@kchelstowski kchelstowski left a comment

Choose a reason for hiding this comment

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

  • I would make sure none adjustments have to be done in the InitializationInterceptor, but looking quickly at the code, I think none have to be done.

Locale locale

if (localeCode?.contains("_")) {
String[] localeCodeParts = localeCode.split("_")
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpicky: I'd add the "_" separator to the Constants.

}

locale = new Locale(localeCode)
locale.setDefault(new Locale(Holders.config.openboxes.locale.defaultLocale ?: "en"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would move the new Locale(...) ?: "en" to a separate variable for a better readability.

@@ -1,4 +1,4 @@
<%@ page import="org.pih.warehouse.core.RoleType" %>
<%@ page import="org.pih.warehouse.LocalizationUtil; org.pih.warehouse.core.RoleType" %>
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpicky: I know it was done automaticallty, but we used to separate the imports in gsp templates to be one import per line

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah this is my laziness since I was doing the automatic import and noticed it while creating the PR it's in one line, I'll fix that

@@ -1,5 +1,5 @@

<%@ page import="org.pih.warehouse.core.User" %>
<%@ page import="org.pih.warehouse.LocalizationUtil; org.pih.warehouse.core.User" %>
Copy link
Collaborator

Choose a reason for hiding this comment

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

same as above

@awalkowiak
Copy link
Collaborator Author

@kchelstowski

I would make sure none adjustments have to be done in the InitializationInterceptor, but looking quickly at the code, I think none have to be done.

Initially, I was making the changes there too, because I was pulling through this new util every "defaultLocale" as well, but I decided to skip that one for three reasons:

  • I just assumed localizationModeLocale is crowdin-specific and it will always be ach for us
  • I just assumed no one would have country-specific as the default for now (at least for now)
  • I want to improve the supportedLocales in some further (ideally) post-release ticket/changes

@kchelstowski
Copy link
Collaborator

kchelstowski commented Jun 19, 2024

@awalkowiak I think even if the users have country-specific language, this should be handled already by the:

session?.locale ?: session.user?.locale

assuming the session locale would be set correctly (which you've probably done)
so I think the only edge case I saw there is the last fallback:

new Locale(grailsApplication.config.openboxes.locale.defaultLocale ?: "en")

but as long as no one has their defaultLocale set to es (assuming someone has overwritten the config), we should be fine.

}

locale = new Locale(localeCode)
locale.setDefault(new Locale(Holders.config.openboxes.locale.defaultLocale ?: "en"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be better to use "en" from the Locale class instead of hardcoding it.

@awalkowiak
Copy link
Collaborator Author

@jmiranda I am merging this one, but feel free to leave a post-merge review

@awalkowiak awalkowiak merged commit ad787a2 into develop Jun 20, 2024
2 checks passed
@awalkowiak awalkowiak deleted the OBPIH-6410 branch June 20, 2024 13:48
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.

None yet

5 participants