»
S
I
D
E
B
A
R
«
Localization for Struts Freemarker users
March 12th, 2009 by masukomi

Because it took me freaking forever to find instructions on how to do this...
  1. You do NOT need a message-resource tag in your struts configuration files. Those are outdated instructions for old versions of Struts.You do not need to edit ANY xml at all.
  2. Your Action needs to implement Freemarker's TemplateMethodModel interface
  3. You need a package.properties file (the default locale) and a then another one for each other locale / language you want to support (ex. package_en_US.properties). These should be located in the same package as the Struts action that will be needing them. You can also do ClassName.properties if you want to tie some to a particular class.
  4. Add the code snippet below to your Action. There's a backup here if it's borked below.
  5. Insert localized text into the page with a call like this ${text("property.name")} Struts will take care of figuring out the default language from the user's browser. If you have a better way of determining this there is documentation out there on how to programatically specify the locale you want to be used
public class MyAction extends ActionSupport implements TemplateMethodModel {
  /*Your functionality here */


/** 
	 * when you need to display a localized piece of text in 
	 * the ftl just use  ${text("some.property.name")}
	 * there should be a package.properties or ClassName.properties 
	 * file in the same source package directory as the Action that 
	 * will be using them. There *is* a way to set a global properties file
	 * but I don't know the details. 
	 * Obviously the text for other languages would be specified in files like 
	 * package_en_US.properties 
	 * @return an implementation of TemplateMethodModel which takes a 
	 * one parameter method, the parameter is the name of the localized property
	 * you want to access.
	 */
	public TemplateMethodModel getText(){
		return this;
	}
	public TemplateModel exec(List args) throws TemplateModelException {
        if (args.size() != 1) {
            throw new TemplateModelException("Wrong arguments");
        }
        return new SimpleScalar(getText((String)args.get(0))); 
    }

}

Keywords / Google Food: localization, internationalization, i18n, freemarker, struts


2 Responses  
  • ecgn writes:
    May 14th, 2009 at 2:11 am

    Brilliant! I have also been looking for a good solution for this for a while, but without finding good documentation anywhere.

    There is *one* little problem, though: there has to be an action class involved in the Struts/Freemarker action.

    Example – the following simple page view won’t work:
    <action name=”ShowLogin”>
       <result type=”freemarker”>Login.ftl</result>
    </action>

    To get it to work, I have to insert a “dummy” action into the process:

    <action name=”ShowLogin” class=”actions.DummyAction”>
    <result type=”freemarker”>Login.ftl</result>
    </action>

    Once I did that, it worked just fine! (The DummyAction class inherits the code you have specified above.)

    Thank you very much!!!

  • masukomi writes:
    May 14th, 2009 at 7:05 am

    Glad it helped. The thought of NOT including an action never occurred to me. Kind of a “what’s the point of an ftl without some code to back it up?” thought. But, obviously, there are some exceptions where it’s not really needed, although if i were doing a login page i’d have it capable of handling error messages from failed logins, rather than sending them to a different “failed login” page… in both cases an action is required at some point.


Leave a Reply

»  Substance: WordPress   »  Style: Ahren Ahimsa
© Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.