<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>weblog.masukomi.org - i18n</title>
    <subtitle>Autistic, ADHD, Geek. The weblog of Kay Rhodes (masukomi).</subtitle>
    <link rel="self" type="application/atom+xml" href="https://weblog.masukomi.org/tags/i18n/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://weblog.masukomi.org"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2009-03-12T00:00:00+00:00</updated>
    <id>https://weblog.masukomi.org/tags/i18n/atom.xml</id>
    <entry xml:lang="en">
        <title>Localization for Struts Freemarker users</title>
        <published>2009-03-12T00:00:00+00:00</published>
        <updated>2009-03-12T00:00:00+00:00</updated>
        
        <author>
          <name>Kay Rhodes</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://weblog.masukomi.org/2009/03/12/localization-for-struts-freemarker-users/"/>
        <id>https://weblog.masukomi.org/2009/03/12/localization-for-struts-freemarker-users/</id>
        
        <content type="html" xml:base="https://weblog.masukomi.org/2009/03/12/localization-for-struts-freemarker-users/">&lt;p&gt;Because it took me freaking forever to find instructions on how to do
this…&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’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’s a &lt;a rel=&quot;external&quot; href=&quot;http://gist.github.com/78303&quot;&gt;backup
here&lt;/a&gt; if it’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(“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&lt;/p&gt;
&lt;p&gt;public class MyAction extends ActionSupport implements TemplateMethodModel {
/*Your functionality here */&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; /**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * when you need to display a localized piece of text in&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * the ftl just use  ${text(&amp;quot;some.property.name&amp;quot;)}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * there should be a package.properties or ClassName.properties&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * file in the same source package directory as the Action that&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * will be using them. There *is* a way to set a global properties file&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * but I don&amp;#39;t know the details.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * Obviously the text for other languages would be specified in files like&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * package_en_US.properties&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * @return an implementation of TemplateMethodModel which takes a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * one parameter method, the parameter is the name of the localized property&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; * you want to access.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;public TemplateMethodModel getText(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return this;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;public TemplateModel exec(List args) throws TemplateModelException {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    if (args.size() != 1) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        throw new TemplateModelException(&amp;quot;Wrong arguments&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return new SimpleScalar(getText((String)args.get(0)));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&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 class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;action name=”ShowLogin”&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   &amp;lt;result type=”freemarker”&amp;gt;Login.ftl&amp;lt;/result&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;/action&amp;gt;&lt;/span&gt;&lt;/span&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 class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;action name=”ShowLogin” class=”actions.DummyAction”&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;result type=”freemarker”&amp;gt;Login.ftl&amp;lt;/result&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;/action&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Keywords / Google Food: localization, internationalization, i18n,
freemarker, struts&lt;/p&gt;
</content>
        
    </entry>
</feed>
