user5497372
user5497372

Reputation:

Global resource bundle in Struts2 - Granularity best practices ?

I have three pages with three different tab titles.

Page 1 tab title name: Page One : Hello : Hi

Page 2 tab title name: Page Two : Hello : Hi

Page 3 tab title name: Page Three : Hello : Hi

Which is better to do, implementation and best practice wise:


Solution #1

Inside global.properties

tab.title.page.number.one   = Page One 
tab.title.page.number.two   = Page Two
tab.title.page.number.three = Page Three
tab.title.colon             = :
tab.title.hello             = Hello 
tab.title.hi                = Hi

Then inside the title tag for each page in jsp

Page 1:

<title>
    <s:text name="tab.title.page.number.one"/>
    <s:text name="tab.title.colon"/>
    <s:text name="tab.title.hello"/>
    <s:text name="tab.title.colon"/>
    <s:text name="tab.title.hi"/>
</title>

Page 2:

<title>
    <s:text name="tab.title.page.number.two"/>
    <s:text name="tab.title.colon"/>
    <s:text name="tab.title.hello"/>
    <s:text name="tab.title.colon"/>
    <s:text name="tab.title.hi"/>
</title> 

Page 3:

<title>
    <s:text name="tab.title.page.number.three"/>
    <s:text name="tab.title.colon"/>
    <s:text name="tab.title.hello"/>
    <s:text name="tab.title.colon"/>
    <s:text name="tab.title.hi"/>
</title>

OR


Solution #2

Inside global.properties

tab.title.page.one   = Page One : Hello : Hi  
tab.title.page.two   = Page Two : Hello : Hi
tab.title.page.three = Page Three : Hello : Hi

Then inside the title tag for each page in jsp:

Page 1:

<title><s:text name="tab.title.page.one"/></title>

Page 2:

<title><s:text name="tab.title.page.two"/></title>

Page 3:

<title><s:text name="tab.title.page.three"/></title>

My co-developer argues so that the Hello, Hi, : are reusable. But it's only static text. It's not changing. Which is better to use implementation, coding standard, performance-wise(LOL)? Thanks!

Upvotes: 2

Views: 128

Answers (2)

Dave Newton
Dave Newton

Reputation: 160191

Several thoughts too long to fit into a comment:

It doesn't make any sense to I18N a colon: will it change? Unlikely. If it might, consider using a custom tag/etc. to wrap up view-level static text.

Using non-I18N text in the key itself is a little weird, e.g., use .greeting instead of .hello.

page.number.one seems needlessly verbose, instead page.one or just page1. Or instead of pegging it directly to a tab, since you may want to re-use that for the actual page title or a heading, just page1.title.

Nutshell: I'm all for reusability, but IMO property files are the wrong place for a lot of what you're showing. Use a custom tag or utility method to access the actual properties that need to change.

(I'm actually a fan of keeping stuff like this in an I18N DB instead of property files anyway; they're easier to deal with using a trivial front end, and with appropriate caching are just as fast.)

Upvotes: 2

Alireza Fattahi
Alireza Fattahi

Reputation: 45505

Here is my experience for a fully i18n application. You should not define different keys for same values as much as you can.

Consider you want to define a label for user name.

You can do is as

lable.username=User Name

OR

lable.inputform.username=User Name
lable.registeration.form.username=User Name
lable.activate.form.username=User Name

The first approach is much better as you do less in resource bundle. UserName is only User Name in whole application, if you want to change it is quite logical that one change should be enough.

Please consider that huge resource bundle,is harder to maintain and needs more memory. Resource bundle will become huge in no time :)

I also recommend not to build sentences by adding keys:

For example

  lable.please+ lable.add + lable.your + lable.username 

Make it as:

form.message= Please add your user name

It make your jsp or action very complicated, and you will not gain a lot.

So I generally agree with you, but your college approach should be consider for reusable values!

Upvotes: 2

Related Questions