<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Struts on weblog.masukomi.org</title>
    <link>https://weblog.masukomi.org/tags/struts/</link>
    <description>Recent content in Struts on weblog.masukomi.org</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <copyright>&amp;copy Kay Rhodes (masukomi.org) 2022</copyright>
    <lastBuildDate>Thu, 12 Mar 2009 00:00:00 +0000</lastBuildDate><atom:link href="https://weblog.masukomi.org/tags/struts/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Localization for Struts Freemarker users</title>
      <link>https://weblog.masukomi.org/2009/03/12/localization-for-struts-freemarker-users/</link>
      <pubDate>Thu, 12 Mar 2009 00:00:00 +0000</pubDate>
      
      <guid>https://weblog.masukomi.org/2009/03/12/localization-for-struts-freemarker-users/</guid>
      <description>&lt;p&gt;Because it took me freaking forever to find instructions on how to do
this&amp;hellip;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Your Action needs to implement Freemarker&amp;rsquo;s TemplateMethodModel
interface&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the code snippet below to your Action. There&amp;rsquo;s a &lt;a href=&#34;http://gist.github.com/78303&#34;&gt;backup
here&lt;/a&gt; if it&amp;rsquo;s borked below.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Insert localized text into the page with a call like this
${text(&amp;ldquo;property.name&amp;rdquo;)} Struts will take care of figuring out the
default language from the user&amp;rsquo;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&lt;/p&gt;
&lt;p&gt;public class MyAction extends ActionSupport implements TemplateMethodModel {
/*Your functionality here */&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; /**
 * when you need to display a localized piece of text in
 * the ftl just use  ${text(&amp;quot;some.property.name&amp;quot;)}
 * 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&#39;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(&amp;quot;Wrong arguments&amp;quot;);
    }
    return new SimpleScalar(getText((String)args.get(0)));
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;ecgn adds the following notes about actions:&lt;br&gt;
There has to be an action class involved in the Struts/Freemarker action.&lt;/p&gt;
&lt;p&gt;Example – the following simple page view &lt;em&gt;won’t work&lt;/em&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;action name=”ShowLogin”&amp;gt;
   &amp;lt;result type=”freemarker”&amp;gt;Login.ftl&amp;lt;/result&amp;gt;
&amp;lt;/action&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To get it to work, I have to insert a “dummy” action into the process:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;action name=”ShowLogin” class=”actions.DummyAction”&amp;gt;
&amp;lt;result type=”freemarker”&amp;gt;Login.ftl&amp;lt;/result&amp;gt;
&amp;lt;/action&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Keywords / Google Food: localization, internationalization, i18n,
freemarker, struts&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
