<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tips for Twits &#187; java</title>
	<atom:link href="http://t.wits.sg/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://t.wits.sg</link>
	<description>WARNING: excessive use of this site will increase MP at the expense of HP</description>
	<lastBuildDate>Mon, 16 Aug 2010 04:09:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>HOWTO: struts 2 i18n</title>
		<link>http://t.wits.sg/2008/06/23/howto-struts-2-i18n/</link>
		<comments>http://t.wits.sg/2008/06/23/howto-struts-2-i18n/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 09:18:29 +0000</pubDate>
		<dc:creator>gaweee</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[struts 2]]></category>

		<guid isPermaLink="false">http://t.wits.sg/?p=15</guid>
		<description><![CDATA[i18n in struts is really flexible if set up correctly. Unfortunately searching for a document to learn the &#8216;hidden&#8217; tricks of it is really frustrating&#8230; So in revenge, i&#8217;m writing my own! I hope someone out there finds this useful&#8230; Step 1: Edit your struts.properties file &#8211; Add/Edit the following line: struts.custom.i18n.resources=languages_actions,languages_views This definition instructs [...]]]></description>
			<content:encoded><![CDATA[<p>i18n in struts is really flexible if set up correctly. Unfortunately searching for a document to learn the &#8216;hidden&#8217; tricks of it is really frustrating&#8230; So in revenge, i&#8217;m writing my own! I hope someone out there finds this useful&#8230;</p>
<div class="contentblock"><strong>Step 1: Edit your struts.properties file &#8211; Add/Edit the following line:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">struts.custom.i18n.resources=languages_actions,languages_views</pre></div></div>

<p>This definition instructs struts to use the files <em>languages_actions.properties</em> and <em>languages_view.properties</em> for your language definitions. When there is a new request locale, such as zh_CN (chinese), struts will find for the file <em>languages_actions_zh_CN.properties</em>, or if defaults to en_US (english), struts will use the file <em>language_actions_en_US.properties</em>.<br />
On top of that, there can be many places where language definitions can be found. They will be searched in the following order:</p>
<ol>
<li> ActionClass.properties</li>
<li> BaseClass.properties (all the way to Object.properties)</li>
<li> Interface.properties (every interface and sub-interface)</li>
<li> ModelDriven&#8217;s model (if implements ModelDriven), for the model object repeat from 1</li>
<li> package.properties (of the directory where class is located and every parent directory all the way to the root directory)</li>
<li> search up the i18n message key hierarchy itself</li>
<li> global resource properties</li>
</ol>
<p>I put my files in the main <em>WEB-INF\classes</em> folder where <em>struts.properties</em> can be found<br />
Read <a href="http://docs.huihoo.com/apache/struts/apache-struts-2-document/localization.html" target="_blank">here</a> for more information on how struts 2 searches for i18n definitions.
</div>
<div class="contentblock"><strong>Step 2: Writing the language file itself</strong></p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">user.submit.image=/images/en_US/submit.gif
user.welcome=Welcome, {0}
user.link=&lt;a href=&quot;somelink.action?id={1}&amp;amp;token={2}&quot;&gt;{0}&lt;/a&gt;' document
user.validation.token.invalid={0} does not exists in the system</pre></div></div>

<p>the above is an example of a en_US language definition file. You can see how the image and welcome message would be different in a zh_CN language definition file. but just for the heck of it, here it is anyway</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">user.submit.image=/images/zh_CN/submit.gif
user.welcome={0}, 你好
user.link=&lt;a href=&quot;somelink.action?id={1}&amp;amp;token={2}&quot;&gt;{0}&lt;/a&gt; 的文件
user.validation.token.invalid={0} 不纯在于系统</pre></div></div>

<p>The key here in flexibility is the use of # parameters for the definitions (very printf-ish). This allows the dynamic data to be reformatted to any language structure (left to right, right to left, orientation of grammer, verbs, subject, topics, etc). Really smart way to do it!
</p></div>
<div class="contentblock"><strong>Step 3: Using the definitions:</strong><br />
in JSP,</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">&lt;%@ page contentType=&quot;text/html; charset=UTF-8&quot;%&gt;
&lt;%@ taglib prefix=&quot;s&quot; uri=&quot;/struts-tags&quot; %&gt;
&lt;h1&gt;&lt;s:text name=&quot;user.welcome&quot;&gt;
	&lt;s:param value=&quot;user.name&quot; /&gt;
&lt;/s:text&gt;&lt;/h1&gt;
&lt;s:text name=&quot;user.link&quot;&gt;
	&lt;s:param value=&quot;document.name&quot; /&gt;
	&lt;s:param value=&quot;document.id&quot; /&gt;
	&lt;s:param value=&quot;document.token&quot; /&gt;
&lt;/s:text&gt;
&lt;form input=&quot;&quot;&gt;
	&lt;s:submit type=&quot;image&quot; src=&quot;%{getText('user.submit.image')}&quot; cssClass=&quot;submit&quot; /&gt;
&lt;/form&gt;</pre></div></div>

<p>Or in Java,</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeActions <span style="color: #000000; font-weight: bold;">extends</span> ActionSupport <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> someMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>token.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;abcdefghi&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">String</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> messageargs <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> token <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			addActionError<span style="color: #009900;">&#40;</span>getText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user.validation.token.invlaid&quot;</span>, messageargs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> token<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getToken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 	<span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span>  token<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setToken<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#41;</span> 	<span style="color: #009900;">&#123;</span> token <span style="color: #339933;">=</span> s<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above examples goes to show how you can combine dynamic data with the i18n strings in a JSP or a Java file
</p></div>
<div style="padding-top: 15px; border-top: 1px dashed #ddd;"><strong>Tips</strong></p>
<ul>
<li> When using a utf8 language definition file (such as for zh_CN), you have to write out your files then run it through the native2ascii program. On windows it would look like this:

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">&quot;c:\Program Files\Java\jdk1.6.0_04\bin\native2ascii.exe&quot; -encoding UTF8 \path\to\zh_CN.locale \new\path\to\languages_actions_zh_CN.properties</pre></div></div>

<p>Change your jdk\bin directory to where appropriate</li>
<li>Flood HTML with s:text language definitions &#8211; If you&#8217;ve already decided to make your app internationalized, its a big step, so make sure everything is pulled off a dynamic language pack.</li>
<li>Always use the s:date taglib to represent date, save yourself all the time (and frustration) in the world</li>
<li>Set up your default language definition file first. Struts makes it such that if your definition doesn&#8217;t exists then it searches the default file for it. That is to say if your definition for user.link doesn&#8217;t exists in <em>languages_actions_zh_CN.properties</em>, struts will continue to find for it in <em>languages_actions.properties</em> so at least there will be a link there, even if its untranslated.</li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://t.wits.sg/2008/06/23/howto-struts-2-i18n/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
