<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>weblog.masukomi.org - java</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/java/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/java/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>
    <entry xml:lang="en">
        <title>The best argument for compiled languages</title>
        <published>2007-08-19T00:00:00+00:00</published>
        <updated>2007-08-19T00:00:00+00:00</updated>
        
        <author>
          <name>Kay Rhodes</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://weblog.masukomi.org/2007/08/19/the-best-argument-for-compiled-languages/"/>
        <id>https://weblog.masukomi.org/2007/08/19/the-best-argument-for-compiled-languages/</id>
        
        <content type="html" xml:base="https://weblog.masukomi.org/2007/08/19/the-best-argument-for-compiled-languages/">&lt;p&gt;I keep thinking back to a short comment at BarCamp Manchester
in the Unit Testing talk. When asked if anyone had written an app with
100% code coverage the guy beside me raised his hand. Now I’ve been
advocating for a while now that it’s essentially wasted time to bother
testing your getters and setters, and when I mentioned something to this
effect he said, “How do you know you haven’t made a typo in a variable
name?”&lt;/p&gt;
&lt;p&gt;…&lt;/p&gt;
&lt;p&gt;My mind just kind of spun for a second on that one for a second. I guess
I’ve been in Java land for so long that the concept of having such a
thing exist for more than a few seconds just doesn’t enter my brain. I
mean first off, the IDE will bitch at you, because it can’t compile it,
and even if you’re not using an IDE the compiler will bitch at you. I
just responded that “I use a compiled language so that’s never something
I have to worry about.”&lt;/p&gt;
&lt;p&gt;Now, as many of you know I’m still a huge fan of languages like
&lt;a rel=&quot;external&quot; href=&quot;https://weblog.masukomi.org/2007/7/31/you-treat-her-like-a-sex-toy&quot;&gt;Perl&lt;/a&gt;
and Ruby so I work in them fairly often. And I still say that it’s not
worth your time writing tests for your getters and setters even in them
because unlike Java these languages don’t have IDEs that happily spit
out a getter and setter for every variable in your class regardless of
if you need it or not. As a result, you only create the ones you need.
And if you need them, then they’re going to be used, and if they’re
used, you can pretty much guarantee that you’ll see the results of any
typos show up really quickly when you test the methods that actually do
something noteworthy, because they’ll use the getters and setters
(otherwise why did you write them?) and either blow up when the compiler
hits them or not work as expected, which is why you’re writing a test.&lt;/p&gt;
</content>
        
    </entry>
</feed>
