pebble icon indicating copy to clipboard operation
pebble copied to clipboard

ResourceBundles aren't honoring pebble.cache=false

Open nkavian opened this issue 5 years ago • 4 comments

application.properties has pebble.cache=false Load the application and view a template page which renders content via i18n and a properties file name FOO.properties. Now change the value of a property, allow spring boot to hot reload, and refresh the page. You'll see the old content displayed.

I'm using version 3.1.0 with Spring Boot.

i18nFunction or UTF8Control doesn't seem to have any logical related to caching or pebble.cache=false...

nkavian avatar Aug 29 '20 00:08 nkavian

Do you have a code sample that I can look at ?

ebussieres avatar Aug 29 '20 03:08 ebussieres

Sure.

Load this project and run it within eclipse: https://github.com/nkavian/pebble-example-spring/tree/master/pebble-sb

View the home page in the browser.

Then change the string in this file: https://github.com/nkavian/pebble-example-spring/blob/master/pebble-sb/src/main/resources/i18n/Words_en.properties

And refresh the browser page and notice the string did not change. Feel free to change text in the pebble template and you'll see that it does change text as expected, just not i18n content.

nkavian avatar Aug 30 '20 06:08 nkavian

You need to provide a ReloadableResourceBundleMessageSource bean for that. By default spring-boot does not.

Also, since you're using pebble-spring-boot-starter, you should use the message function

<h2>{{ message('welcome') }}</h2>

You can take a look here

  • https://github.com/PebbleTemplates/spring-petclinic
  • https://pebbletemplates.io/wiki/guide/spring-boot-integration (see section spring boot integration)

ebussieres avatar Aug 30 '20 14:08 ebussieres

Thanks for the quick response. I have several questions and responses.

  • The documentation for Message function is vague. It says It achieves the **same** thing as the i18n function. It's more accurate to say it achieves almost the same thing and then clarify in that section the scenario that a person should use message instead of i18n. Because the paragraph is short and vague, and it uses the were same, I glossed over it.

  • The documentation for Message function should also mention usage of ReloadableResourceBundleMessageSource and how it affects caching. Otherwise it's too difficult to piece together that i18n caching is somehow related to a ReloadableResourceBundleMessageSource.

A) At minimum, can this section of the documentation be updated to clarify these points?

B) Can an example also be added or linked to so that it's clear how a person needs to wire this together. I had to google it and find something like this to help me: https://github.com/asaas/pebble-spring-translate/blob/master/src/test/java/com/github/asaas/pebble/spring/translate/conf/TestConfig.java

  • If the project pebble-spring-boot-starter did not exist, I could understand how it falls on the developer to wire ReloadableResourceBundleMessageSource into their own project. But this project does exist and I have to assume it exists because it's intended to autowire common things together and make things work as transparently as possible.

C) Can you add code to pebble-spring-boot-starter so it automatically provides a default ReloadableResourceBundleMessageSource when pebble.cache=false and use application.properties to gather any optional parameters as needed. With the intent that i18n should work as is. Otherwise, (sarcasm here), i18n should be documented as functionally useless when used in a spring-boot project and message is it's replacement.

D) If (C) is not acceptable, can you clearly document this limitation with/near the documentation for pebble.cache. It's only fair to mention to the developer they need to perform extra work to get caching=false to work for translation strings.

  • As a developer, I try to write code that is self descriptive. I appreciate the existence of i18n to clearly indicate to any developer it is performing a translation. Another reason I glossed over the section Message function is because what is a message really? I'm not sending messages into a queue and I'm not reading messages into a gui, etc.. The message method is also longer than i18n. So if I want to fix this in my code based on existing libraries, I need to refactor hundreds of web pages in my application from the shorter i18n to message.

E) It feels like this breaks object oriented programming in some form. If I add pebble-spring-boot-starter I must use message but if I decide to remove the project, I would have to refactor again to switch back to i18n. The context here is that at some point I had pebble working with my same web content across Spark, VertxWeb, and SpringBoot. I'm trying to make the case here that i18n should be the main method and perhaps message should be deprecated. Both methods have even the same interface.

F) I'm realizing to phrase my problem statement this way as well. If I wanted to just use Pebble by itself with pebble.cache=false and not use pebble-spring-boot-starter, what would your solution then be? I think that's why I still pointed to a problem (or opportunity) to fix this in i18nFunction / UTF8Control.

nkavian avatar Aug 30 '20 19:08 nkavian