简体   繁体   English

Struts2中的全球资源包-粒度最佳实践?

[英]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 第1页标签标题名称:第1页:您好:您好

Page 2 tab title name: Page Two : Hello : Hi 第2页标签标题名称:第2页:您好:您好

Page 3 tab title name: Page Three : Hello : Hi 第3页标签标题名称:第3页:您好:您好

Which is better to do, implementation and best practice wise: 在执行,最佳实践和最佳实践方面哪个更好:


Solution #1 解决方案1

Inside global.properties 内部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 然后在jsp中每个页面的标题标签内

Page 1: 第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: 第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: 第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 解决方案#2

Inside global.properties 内部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: 然后在jsp中每个页面的标题标签内:

Page 1: 第1页:

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

Page 2: 第2页:

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

Page 3: 第3页:

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

My co-developer argues so that the Hello , Hi , : are reusable. 我的合作开发者认为,这样的HelloHi:是可重用的。 But it's only static text. 但这只是静态文本。 It's not changing. 它没有改变。 Which is better to use implementation, coding standard, performance-wise(LOL)? 使用实施,编码标准,性能方面(LOL)哪个更好? Thanks! 谢谢!

Here is my experience for a fully i18n application. 这是我的完整i18n应用程序的经验。 You should not define different keys for same values as much as you can . 您不应该为相同的值定义尽可能多的键

Consider you want to define a label for user name . 考虑您要为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. UserName在整个应用程序中只是User Name ,如果要更改,一个更改就足够了是很合逻辑的。

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. 它会使您的jsp或操作非常复杂,并且您不会获得很多收益。

So I generally agree with you, but your college approach should be consider for reusable values! 因此,我总体上同意您的观点,但是您的大学方法应该考虑可重用的价值!

Several thoughts too long to fit into a comment: 几个想法太久了,无法发表评论:

It doesn't make any sense to I18N a colon: will it change? 对冒号I18N来说没有任何意义:它会改变吗? 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, eg, use .greeting instead of .hello . 在密钥本身中使用非I18N文本.greeting ,例如,使用.greeting而不是.hello

page.number.one seems needlessly verbose, instead page.one or just page1 . page.number.one似乎不必要冗长,而是page.one或仅仅是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 . 或者,而不是直接将其固定在选项卡上,因为您可能希望将其重新用于实际的页面标题或标题,只需将page1.title

Nutshell: I'm all for reusability, but IMO property files are the wrong place for a lot of what you're showing. 简而言之:我全力支持可重用性,但是IMO属性文件对于显示的很多内容来说都是错误的位置。 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.) (我实际上是喜欢将这样的东西保留在I18N DB中而不是属性文件中的爱好者;使用琐碎的前端更容易处理它们,并且具有适当的缓存也一样快。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM