-
Notifications
You must be signed in to change notification settings - Fork 325
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
Better localisation support #686
Comments
Diving into the code a bit more it seems that the main issue is how to pass certain preferences specific to a given validation run (the Locale in this case) and then access them from the
The question now becomes how to pass the expected |
Assuming that all validators share the same locale, we can put it in the ValidationConfig. I agree that we should use something other than static local across the entire JVM. Do we have another use case that needs to switch the locale between validators? |
My use case is a web app, localised in several languages, that shares a validation report produced (mostly) by this library. The workaround code I shared in my initial comment is what we currently do to allow the validation to produce locale-specific messages. In fact we even define our own resource bundle to tailor all messages to what we want plus add missing translations (messages are translated in all EU languages). Also, to be clear, I am not referring to changing the locale between validators (e.g. an EnumValidator or a FormatValidator) but for different validation runs (one user wants the report in English, and another one in French). Regarding the So, to summarise, I would propose the following for the current issue:
What do you think? If you agree with the above I can make a PR for point 1 (the current issue) and create a separate issue for point 2. |
I understand your use case now. It makes sense to have different locales for different validation sessions. I think your approach is very prudent. I am looking forward to your PR. Thanks. |
Hi @stevehu . Just prepared a PR for this issue for you to review and merge (took me some time as I had to jump into other tasks). |
Co-authored-by: simatosc <costas.simatos@sigmacubed.eu>
Created issue #744 for the discussed per-validation configuration. Inspecting more closely the caching code it seems that using a different configuration per validation was already considered, however the current implementation is prone to race conditions if validations are executed in parallel. I adapted the issue to reflect this. |
The approach currently used by this library to localise error messages is by setting
Locale.setDefault(aLocale)
. For example, as documented here, one is expected to do as follows:The problem with this approach is that this sets the default Locale for the entire JVM, which in any multi-threaded scenario like a web application is not what you would want. Moreover, checking the code of
I18nSupport
I see that the resource bundle is loaded in a static initialiser and stored in a static final variable:In following this approach, there can be no locale switch after the class is initially loaded.
A better approach would be to introduce a "context" of sorts for a given validation run that allows you to set it with the desired Locale. Using this you could then do a Locale-aware lookup of the resource bundle to use and return the localised message.
Note that this is not a blocking issue for use in a multilingual context given that you can do something like this:
Although this workaround exists it would be a better design to foresee this kind of multilingual support in the library itself. Note also that if this workaround is used any custom messages would be forcibly ignored (although anyway custom messages don't support localisation as they are currently defined).
The text was updated successfully, but these errors were encountered: