List files to be compressed & bundled known only at runtime

48 views
Skip to first unread message

chera...@gmail.com

unread,
May 19, 2015, 4:49:32 PM5/19/15
to wr...@googlegroups.com
Hi,

In my web applications, all the localizable messages files (js file contains data in json) should be bundled and minimized based on the current user locale. Could you advise me how to achieve this using wro4j.

Note: All other js/css files are bundled by configuring in wro.xml.

Regards & Thanks
Cheran

Alex Objelean

unread,
May 19, 2015, 5:01:14 PM5/19/15
to wr...@googlegroups.com
I assume you are using runtime solution (filter). If that is true, there is an example which serve resources based on request locale:  

The idea is to customize the cacheKeyFactory, so that locale is used to compute the key used by cache. Also, it is assumed that there is a processor which uses user locale to process a resource differently. 

If the above description is not helpful, please provide more details about your use-case.

Cheers,
Alex

--
You received this message because you are subscribed to the Google Groups "wro4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wro4j+un...@googlegroups.com.
To post to this group, send email to wr...@googlegroups.com.
Visit this group at http://groups.google.com/group/wro4j.
For more options, visit https://groups.google.com/d/optout.

Cheranthian Muthuvinayagam

unread,
May 20, 2015, 12:37:28 AM5/20/15
to wr...@googlegroups.com
Thanks Alex.

Yes I am using the runtime solution (filter). The example (https://github.com/wro4j/wro4j-examples/blob/master/locale-support) assumes that language packs (bundle-nl, bundle-de) are installed. I assume that group is mapped to the language bundle. Correct me if I am wrong. My web application installed with default language pack(English). Language packs are released separately (normally a month after the release) and a customer could install one or more language packs. Language pack install will not update the wro.xml.

Our current implementation generates the temporary file. Temporary file contains the combined java script message generated from the list java properties files which is served to the client. Temporary file is not minimized. I like to to resue wro for this use case.

Do I need to generate wroModel at runtime ?. It will be nice to server the language specific bundled & minified without afting wro.xml.

Alex Objelean

unread,
May 20, 2015, 3:25:15 AM5/20/15
to wr...@googlegroups.com
One possible solution is to generate the model programmatically during the runtime. You have all the required mechanisms for that (implement WroModelFactory interface). The custom WroModelFactory would create a group per locale using a naming pattern which would encode the locale (ex: group_en, group_fr, etc).

On the other hand, I would suggest a different approach: keep the model simple (no need to create new groups for each supported locale). 
Lets suppose there is a request coming for group "G1" using locale "L1". What kind of content should be served? How is it different for a request coming for group "G1" and locale "L2"? 
What exactly do you mean by language pack? How it is installed? 

Once you answer to the above questions and elaborate on your use-case, I could give more details on the alternative approach.

Cheers,
Alex

Cheranthian Muthuvinayagam

unread,
May 22, 2015, 12:58:38 PM5/22/15
to wr...@googlegroups.com
Sorry Alex for my late reply.

There is definitive list of java properties contains the message for the java script client. Web application transforms the the java properties files to javascript files (message in json) and send it to java script client as part of the initial request.

For example, I have the following definitive list of properties that contains message for my web client.

com.acme.web.culturefiles.panels.panela.properties
com.acme.web.culturefiles.panels.panelb.properties

Language Pack for UK will install the following files.
com.acme.web.culturefiles.panels.panela_en_GB.properties
com.acme.web.culturefiles.panels.panelb_en_GB.properties

Language Pack for CA will install the following files.
com.acme.web.culturefiles.panels.panela_CA_GB.properties
com.acme.web.culturefiles.panels.panelb_CA_GB.properties

Web application doesn't know the language pack installed in the system. So I like to avoid defining all possible group in my wro.xml upfront. In other words, I like to define a single group as below. At run time, group name will resolved with the current request's locale. i.e {languageCode} will be substituted with en-CA and the generated group name will be webClientMessagesen-CA

<group name=webClientMessages{languageCode}>
<js>com.acme.web.culturefiles.panels.panela.properties</js>
<js>com.acme.web.culturefiles.panels.panela.properties</js>
</group>

Please let me if you need additional details.

Alex Objelean

unread,
May 23, 2015, 4:16:56 AM5/23/15
to wr...@googlegroups.com
Hi Cheranthian,

I have pushed a small example on wro4j-examples, which is a proof of concept for your use-case.

The assumption is that you have a servlet responsible for serving translated resources in JSON format, based on defined list of properties. In the example project, there is a TranslationServlet which mocks your implementation (it just returns a json which differs based on resolved locale).

The most important part is the customization of the WroManagerFactory, which extends the CacheKeyFactory. This implementation ensure that the wro generated resources are not cached only by its name, but also will take into account the request resolved locale.

When requesting the same url (/wro/g1.js) for different locales (it can be simulated by adding ?locale=FR to the request path), you'll notice that the served response is different.This approach is similar to defining new group for each locale, but the same result is achieved using a single group. You will probably want to limit the number of supported locales, to avoid explosion of cached groups.

I hope this example can help you to solve your problem.

If you have any questions, don't hesitate to ask.

Cheers,
Alex
Reply all
Reply to author
Forward
0 new messages