Thread: [Webwork-user] I18n and more
Brought to you by:
baldree,
rickardoberg
From: Rickard O. <ri...@jb...> - 2000-12-04 21:20:48
|
Hey Following Edwins advice I have begun converting the JavaWorld article on i18n to WebWork. It is pretty straightforward, aside from the fact that the original really is *JSP* with all that comes with that *shiver*. So, a bit of "shaping up" has been done, which made it easier to read, but also a little bigger in terms of number of classes. Nevertheless, it was fairly easy to do so, and I'm very glad I did it because it showed two things that are missing from WebWork. Two tags to be precise. First of all, currently generic components are now done as small jsp:included JSP's which are customized with parameters. They all have a "label" parameter which denotes the label of the component. The problem is that it is not possible to have dynamic content for the label in any easy way. The reason is the way it is supplied, which is as a jsp:param. jsp:param has an attribute "value" which has a runtimeexpression as value. This does indeed allow for extraction of dynamic content for the value, but it would be nicer to be able to use WebWork properties. I.e. instead of: <jsp:param name="label" value="<%=((SomeAction)pageContext.getAttribute("result")).getTexts().getStr ing("somefield.label")%>"/> I would want to do: <jsp:param name="label"><webwork:property name="texts:somefield.label"/></jsp:param> i.e. let the body of the param tag be its value. This is not how jsp:param works, but I have already created a webwork:param which works like the way I want it to for the URL tag. What I will do now is to adapt it to also work with a new webwork:include tag, which will be a replacement for jsp:include. By doing this we can i18n'alize components' labels much easier. I hope you see how by the above. The second thing that is needed is a "quick action" for formatting output. The Actions in WebWork are somewhat coarsegrained and are used for either page or component processing. However, in the i18n example there is a place where a currency is converted to another currency, and the currency name is appended. E.g. $1 is converted into 8.93 SEK. This kind of field-by-field processing is a bit cumbersome to do as Actions. I have actually done that with this particular example, and it looked something like this: <jsp:include page="i18n.ConvertPrice?result=price.jsp" flush="true> <jsp:param name="price" value="1"/> </jsp:param> where price.jsp is: <taglib ...> <webwork:property name="realPrice"/> So, all that happens in ConvertPrice is that a conversion to a given locales currency is done. To use actions and includes for this is somewhat awkward and obscure. Here's is a suggestion for an alternative solution. Introduce a tag "action" which takes an action name as parameter, and which is executed as usual, but its result from execute() is ignored and instead the action is made available (for use by the property tag, for example) during the execution of its body. Like this: <webwork:action name="i18n.ConvertPrice"> <webwork:param name="price"><webwork:property name="cd:price"/> <!-- at this point the ConvertPrice action has been instantiated, its setPrice method has been called, and execute() has also been called --> <webwork:property name="realPrice/> <!-- call getRealPrice on action to print out the converted price including currency type indication --> </webwork:action> This would allow the MVC paradigm to be preserved, but allow for much more lightweight usage than the usual action->jsp model. The above is somewhat flawed still (how to determine when to call execute(), what happens if execute() wants to show some error view, etc.) so if you have any ideas on the above, speak up :-) regards. Rickard |
From: Maurice C . P. <Ma...@Vi...> - 2000-12-05 22:34:28
|
Hello, Since we are on the subject of i18n, I have notice that I am hard coding a lot of English error messages in my actions. Of course I could use a resource bundle, but I was thinking that doing an i18n error message lookup by default in ActionSupport and ActionFormSupport would be more convenient. Later, Maurice |
From: Rickard <ri...@jb...> - 2000-12-06 07:41:18
|
Hi! "Maurice C . Parker" wrote: > Since we are on the subject of i18n, I have notice that I am hard coding a lot of English error messages in my actions. Of course I could use a resource bundle, but I was thinking that doing an i18n error message lookup by default in ActionSupport and ActionFormSupport would be more convenient. Hm, well, I don't think this needs to be in the base support classes. It's very simple to do in an application base class, or in each action if you want to. protected ResourceBundle getMessages() { return ResourceBundle.getBundle("myaction", getLocale()); } public object execute() { // Validation if (.. error detected ..) { addErrorMessage(messages.getString("error.occurred")); // Get localized message } } Or what did you have in mind? /Rickard -- Rickard Öberg Email: ri...@jb... |
From: Maurice C . P. <Ma...@Vi...> - 2000-12-06 12:27:30
|
Hey, * Rickard Öberg (ri...@jb...) wrote: > Hi! > > "Maurice C . Parker" wrote: > > Since we are on the subject of i18n, I have notice that I am hard coding a lot of English error messages in my actions. Of course I could use a resource bundle, but I was thinking that doing an i18n error message lookup by default in ActionSupport and ActionFormSupport would be more convenient. > > Hm, well, I don't think this needs to be in the base support classes. > It's very simple to do in an application base class, or in each action > if you want to. > > protected ResourceBundle getMessages() > { > return ResourceBundle.getBundle("myaction", getLocale()); > } > > public object execute() > { > // Validation > if (.. error detected ..) > { > addErrorMessage(messages.getString("error.occurred")); // Get > localized message > } > } > > Or what did you have in mind? > Right, after thinking it over some more I came to the same conclusion. My original line of thinking had been that since it was so trivial to implement i18n error messages in the Support classes, maybe it should be the default behavior for Webwork error messages. Then I slept on it and realized that this may be a little confusing to a newer programmer trying to learn Webwork. "How do these crazy error messages work? I don't need i18n for my corporate intranet application!" A better solution would be to include subclasses of the Support classes in the i18n example, just to show i18n error messages being used. Then put a note in the Best Practices that it is often desirable to create a personalized version of the Support classes for more feature rich applications. Later, Maurice |
From: Rickard <ri...@jb...> - 2000-12-06 12:40:15
|
"Maurice C . Parker" wrote: > > Or what did you have in mind? > > > Right, after thinking it over some more I came to the same conclusion. > > My original line of thinking had been that since it was so trivial to implement i18n error messages in the Support classes, maybe it should be the default behavior for Webwork error messages. > > Then I slept on it and realized that this may be a little confusing to a newer programmer trying to learn Webwork. "How do these crazy error messages work? I don't need i18n for my corporate intranet application!" > > A better solution would be to include subclasses of the Support classes in the i18n example, just to show i18n error messages being used. Then put a note in the Best Practices that it is often desirable to create a personalized version of the Support classes for more feature rich applications. Indeed. The problem is just that in the i18n there is only one Action, which does everything (the "Shop"). Now, this is not how I would have designed this functionality, but in order to keep it as similar to the original article as possible I kept it this way. And with only one action it is a bit silly to do support classes. So, should I do the right thing and split it up into several distinct actions, or keep it this way to be more similar to the original article (which makes it easier to compare too)? /Rickard -- Rickard Öberg Email: ri...@jb... |
From: Rickard <ri...@jb...> - 2000-12-06 15:03:28
|
Hi! "Maurice C . Parker" wrote: > I think a lot of people are going to see the i18n example out of the context of the article and not understand why the Shop action being used as a dispatcher. It through me off for a while, because I didn't read the article. > > To include the i18n example in its current form with the Webwork distribution could be seen as an endorsement for this style of programming. Slap a disclaimer on it, do it the "Webwork way", or do both and distribute both. Hm.. very true..I'll probably change it. "Don't show people how *not* to do things" is a good rule. > BTW in checkout.jsp: > > <body bgcolor='<webwork:property name="texts:checkout.bgcolor"/>%>'> > ^ > | > oops Fixed. > Did the original article use blue as the background? The first thing I did was change the background color of the demo so that I could try it without damaging my retinas. %-) Try a neutral color like white, light gray, or beige. I have changed nothing in the way it looks or works. I agree with you. But will keep it like that, for reference. /Rickard -- Rickard Öberg Email: ri...@jb... |