Struts 2 automagic – language switching

I am looking into Struts 2 and finding its multi-layered defaults somewhat confusing. Specifically, it took forever to fully understand blank demo application that comes bundled with it. Specifically, I could not understand how the example switches between the languages.

From the jsp page, it was clear that it was using the parameter _requestlocale to indicate the language. But then the parameter just disappears. It actually is not mentioned anywhere else in the source code. Tutorial that is supposed to explain how the application works, tells you to use the parameter but never explains why. Its section on localization is particularly obtuse and just tells you to RTFM.

Eventually, I give up on reading documentation and start reverse-engineering. Grepping for _requestlocale gets me to the I18 Interceptor. That’s a good start, but it’s example shows the interceptor needing to be a part of action mapping, which in my example, it is not. Back to grepping again.

Turns out that the interceptor is a part of standard stack that is defined as part of struts-default.xml file that is hiding inside struts.jar. Things are a bit clearer.

Of course, it is still not obvious how I18N Interceptor is connected to my action. Following the inheritance of the HelloWorld action (not quite the POJO setup I thought it would be), I discover the mysterious com.opensymphony.xwork2.ActionSupport . Are we still in Struts? How optional to Struts experience is this ActionSupport class?

In any case, it is fairly clear that ActionSupport.getText() method is doing the localization magic, but neither ActionSupport, nor the interfaces it implements actually talks about the connection to I18 Interceptor.

Struts 2 is a complex framework; I think it would benefit from a tutorial that will do a depth first explanation of how all pieces actually fit together. At the moment, its documentation feels like many unconnected dots floating in the WIKI soup. I know it is not possible to explain everything to everybody, but surely a blank application should be a good goal for complete documentation.