Discussions

Web tier: servlets, JSP, Web frameworks: Generating Separate Sets of JSPs for Different Locales

  1. This is a generic question but asked with reference to a application

    I have 50 Screens to be displayed in 4 different languages.

    Now I create 50 JSPs, which use a Custom Tag library for internationlization (which at runtime determine the locale of the user and populate corresponding lable from the Properties file. Thanks to Jakarta!!).

    Or I use a seperate java application to generate now itself 200 JSPs with all the labels populated in corresponding languge and keep them ready(50 JSPs in 4 languages). I use a control servlet to direct to corresponding locale's JSP.

    Second approach gains the performance by little bit as it doesn't have to populate the lables at runtime. But It has the disadvantage of increasing the JSPs as the number of languages to support increases.

    Can someone throw light on the advantages/disadvantages these two approaches?

    regards,

    Sripada

  2. To have to create different sets of JSPs for each language is horrible.

    Write the JSPs once. Have your custom taglib detect the locale and alter its content based on the locale.

    This is the only sensible and truly _scalable_ approach.

    Regards,

    Rick
  3. Hi,

    Thanks for your reply. But that idea doesn't sound that horrible to me.

    By generating different sets of JSPs for different languages, we are eliminating the need for the JSP to populate its Labels with the corresponding language at runtime.

    Since Labels in a given language are constants only data part will be populated at runtime.

    Thinking that each JSP has 70 to 100 Labels this approach should give a performance gain I think.

    Waiting for your response.

    Regards,
    Sripada
  4. Hi,

    Thanks for your reply. But that idea doesn't sound that horrible to me.

    By generating different sets of JSPs for different languages, we are eliminating the need for the JSP to populate its Labels with the corresponding language at runtime.

    Since Labels in a given language are constants only data part will be populated at runtime.

    Thinking that each JSP has 70 to 100 Labels this approach should give a performance gain I think.

    Please note that I am talking only about Performance gain not about the problems with the maintanance

    Waiting for your response.

    Regards,
    Sripada

    PS: We will not be manually generating the JSPs. We will have a seperate application which will generate JSPs in different languages.
  5. Sripada,

    In my opinion, pregenerating these JSPs is a bad idea. Of course, it will give you some performance gain, but the design is poor.

    This is my recommendation. First of all, if you have 70-100 labels... then forget using a custom taglib. Because you will have to go the database for each label. Which is very performance inhibitive.

    At the top of the JSP, go to the database (one time) and get all of the labels that will be used in the page.

    The SQL would be something like:

    select LabelID, LabelText from Labels where LanguageID = ? and PageName = 'MyPage.jsp'

    That gets you everything at once. Really fast. Then, simply put them in the page as necessary -- using <%= label %>. This should be almost equally as fast as pre-generating the JSP pages... but it is much faster, and you don't have the overhead of having hundreds of JSPs sitting in memory doing nothing. Good luck!

    Regards,

    Rick


  6. Hi Rick,

    Thanks for your in-depth analysis.

    My labels are not in Database. They are in Property files.

    In the property file(for German) I will have entries for all the Labels like,

    Customer=Kunde
    Name=Heisse
    ..
    ..
    etc.

    I fetch the corresponding Key using
    <i18n:message key="Customer" />

    This utlimately boils down to what you have suggested -reading from DB and storing in memory. ( I am sure Resource Bundle in Java does that )

    But the question still remains the same. Which one gives better performance,

    1) One with many objects in the memory OR
    2) Few objects in the memory with some processing ( to fetch labels from Memory)?

    I can get away from Good Design if it assures me very good performance gain.

    But agian, Does the Performance gain justify the pain of maintance (will it be that much more than conventional Resource Bundle approach??)

    Waiting for your reply.
    Sripada
  7. Sripada,

    The only way you're going to get your answers concerning performance is to actually perform a small test yourself... using both approaches. Then we can see what the concrete results are.

    Write a single JSP... using both methods. And then test them... under a reasonable load. I'm certain that the approach using one JSP for each language will perform better. However, we may be talking about a difference between .75 seconds and 1.0 seconds. Which really... in web terms... is no great difference.

    However, I'm not certain that using properties files is a good idea. I think you should load these labels into a database and access them there as a resource bundle. Otherwise, performance I think will be far worse.

    Good luck!

    -- Rick



  8. Hi Rick,

    Thanks for your reply.

    I am sorry that I forgot to mention in my last posting that
    I have tested the both approaches.

    I tested for a JSP with 50 lables.

    In the Single JSP with i18n tag library scenario
    ------------------------------------------------
    It takes is about 10 milli seconds for reading 50 labels from the Memory (3 simultaneous Users).

    For multiple JSPs in different locales scenario
    -----------------------------------------------
    It is taking no time at all.(0 Seconds)

    This time difference is the difference between the two
    "System.out.println(System.currentTimeMillis())" placed before painting the lables and after painting the lables in the JSP.

    Like,

    <% System.out.println(System.currentTimeMillis()); %>
    Label-1 (for Mulitple JSPs)
    Label-2
    ...
    (OR for Single JSP using Different locales)
    <i18n:message key="Label-1" />
    <i18n:message key="Label-2" />

    <% System.out.println(System.currentTimeMillis()); %>

    I am not sure whether this is the correct measurement for the performance. Thats why I was hesitant to mention it.

    By the way, you mentioned that Properties is a bad idea. I am wondering why is it so. Instead of in DB, I am storing in Property files. I can't see how this approach makes the application slower.

    I will not be going to Property file for every request. Only first time it will access and store them in memory.
    It is the typical behaviour of PropertyResourceBundle in Java.

    Finally, If you think that my finding make some sense, then I think for the sake of 10 milli seconds performance gain, there is no point in going for seperate JSPs, which cause Loading of the Appserver and maintance nightmare.

    Hope I am making sense here.

    Awaiting your reply

    Sripada
  9. Sripada,

    If 10ms is the only difference, the I say don't use separate JSPs. The performance gain isn't worth it. 10ms is undetectable.

    As for using properties files. For your application I think it isn't the best approach for the following reasons.

    1) Database tables are easier to maintain. It is far easier to write a GUI to update a database table than to write one to update properties files.

    2) Properties files don't scale as nicely as you might like. If you ended up running multiple application servers to do load balancing, you'd have to share these properties files in a cluster (shared disk space). Which is unnecessary if you are storing properties in a database file.

    I think whereever possible, storing data in a database is far better than storing it in human readable files on an application and/or web server. Keep all application data in a database if possible -- in one place.

    Regards,

    Rick
  10. Hi Rick,

    Thanks for your comments.

    Yeah!! Now I see the advantage of storing them in DB.
    I will definitely do it.

    I think this ends our discussion on this thread. Oops!! I am feeling nostalgic ;-)

    Thanks again for your valueble comments on this issue and it is nice to see how internet can bring people who are thousand miles apart to share their views. (BTW, I am from Bangalore, India)

    Regards,
    Sripada
  11. Sripada,

    Thanks, and good luck to you.

    Regards,

    Rick