xmlet/HtmlFlow

Internationalization with locale

phinehas opened this issue · 5 comments

I am creating a dynamic html with dynamic content to send it by email.
I am using spring boot with Java 8 and I would like to add localize text from message.properties depending the language of the customer.
Is there a way to achieve that when I render dynamically the html?

I think you may achieve it through MessageSource, which provides access to message.properties.

Maybe you could get an instance of MessageSource through a Bean or injection. Some topics:

Then from the MesssageSource you may get the messages to use on the dynamic view.

Thanks for your quick response.
It makes sense to use MessageSource but your method is static [static void taskDetailsTemplate(DynamicHtml view, Task task)] and with spring static fields are not welcome. Because i need to declare MessageSource as static to get access inside your static method.
By declaring MessageSource as static, it turns that i have a nullPointerException inside your static method. And that is because static fields aren't under spring's control.

I see. In fact most documentation and unit tests examples use static methods. But you can also use them as instance methods. In https://github.com/xmlet/spring-petclinic you have such an example.

The CreateOrUpdatePetForm.java is a dynamic view defined with instance methods and annotated with spring @Component. Maybe it helps to build your use case.

Then you may access that view as an instance field in your controller that is injected by Spring, as happens with PetController with petsForm field.

I hope it helps to solve your problem.

Ho! I didn't know that i could use that dynamic view method with no static declaration as showed:
image

That solved my problem and everything works perfectly now.
Thank you for supporting me.

Btw please start this repo to increase its popularity.

I am far from being expert in Spring and I was not aware of that message.properties feature. If you would like to make a Pull Request to https://github.com/xmlet/spring-petclinic in one of its views, maybe the CreateOrUpdatePetForm.java, showing some labels with different internationalization that would be great and could help other developers.

Thanks