pippo
pippo copied to clipboard
Load i18n messages from third-party libraries
I am currently investigating and thinking of, how to add translations to Pippo via third-party libraries. In my use case, I have my own library pippo-contact-route and it should include some strings (default error messages) in multiple languages. As far as I understand all translations are read when creating the Application instance and the default strings are read from the hard-coded default locations. Then the strings in the application resources folder are read. However, nothing is read when packages are discovered via the ServiceLocator.
At the moment I am exploring two approaches:
- Automatic discovery and loading of translations via the ServiceLocator. This means all third-party libraries must have a class, which inherits a special interface.
- Adding a method to the application class to add translations during runtime. I am not sure how this affects the template engines, because they include a translation mechanism and get the Messages object injected at creation, i.e. before the Application object is ready.
I appreciate any comments and suggestions. Once I have a prototype which is not too invasive of the current design I will create a test and PR.
First of all, sorry for delay. I think that your idea is good because in this mode you can come with encapsulated components in a Pippo application. For the moment I don't know the correct way to implement this feature. You described two approaches and I come with another possible approach
- use
ClasspathUtils#getResources()
instead ofClasspathUtils#locateOnClasspath
inMessages#loadMessages(String fileOrUrl)
https://github.com/pippo-java/pippo/blob/f143f3172933d35c50265e039323ff8dc3a03dce/pippo-core/src/main/java/ro/pippo/core/Messages.java#L344
We have multiple possibilities to implement this feature but the tricky part is to pick the most non intrusive and simple.
We can add a new X method in Messages (and a helper in Application that calls messages.X) and to call Messages#loadRegisteredMessageResources
lazy when someone call messages.get()
). In this mode we can use ApplicationInitializer
to inject new messages locations via ServiceLoader META-INF/services/.
In all approaches a problem is related to overriding the initial message text.
For example, the third party library with name X
comes with a builtin value for a key but I want to have the possibility to override the message text using my app's conf/messages.properties
file.
Thank you for your answer. I'm busy at the moment, but I will look into your suggestion.