<?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>Floorplanner Tech Blog &#187; i18n</title>
	<atom:link href="http://techblog.floorplanner.com/tag/i18n/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.floorplanner.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 19 Nov 2009 04:00:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Converting a Rails application from Gettext to I18n</title>
		<link>http://techblog.floorplanner.com/2008/09/16/converting-a-rails-application-from-gettext-to-i18n/</link>
		<comments>http://techblog.floorplanner.com/2008/09/16/converting-a-rails-application-from-gettext-to-i18n/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 18:06:23 +0000</pubDate>
		<dc:creator>jaap</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=178</guid>
		<description><![CDATA[Last week we had to convert our existing Rails application, which uses Gettext to the new I18n API in combination with the SimpleBackend. I personally never liked Gettext, there was simply not enough control over translations as PO/MO files are not native ruby or at least can be easily accessed by Ruby (like YAML files).
We [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2008%2F09%2F16%2Fconverting-a-rails-application-from-gettext-to-i18n%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2008%2F09%2F16%2Fconverting-a-rails-application-from-gettext-to-i18n%2F" height="61" width="51" /></a></div><p>Last week we had to convert our existing Rails application, which uses Gettext to the <a href="http://www.artweb-design.de/2008/7/18/the-ruby-on-rails-i18n-core-api">new I18n API in combination with the SimpleBackend</a>. I personally never liked Gettext, there was simply not enough control over translations as PO/MO files are not native ruby or at least can be easily accessed by Ruby (like YAML files).</p>
<p>We therefore decided to switch to the brand new, not even released, I18n API. But now we had a serious problem, our code base isn&#8217;t small and all that code had to be converted in some way. We could do it by hand, but hey, that&#8217;s a lot of work and especially very error-prone. Some convertor had to be written. Here it is as a rails plugin: GettextToI18n. </p>
<h3>What does the I18n convertor do?</h3>
<p>It scrapes your whole application and searches for gettext calls, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">_<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;to be translated&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>It will convert this gettext call to the newly I18n format: </p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">I18n.<span style="color:#9900CC;">t</span> <span style="color:#ff3333; font-weight:bold;">:message_id</span></pre></div></div>

<p>Then it builds up a big hash containing all the the translations. We decided it was handy to use the scopes that are introduced in the new I18n api. So it stores the translations in the following format: </p>
<p><strong>For models:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;model&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;model_name&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#006600; font-weight:bold;">&#123;</span>:message_1 =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;to be translated&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p><strong>For controller:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;controller&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;controller_name&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#006600; font-weight:bold;">&#123;</span>:message_1 =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;to be translated&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>After this hash of translations has been built up, the convertor writes it as a YAML file to:  <code>config/locales/template.yml</code>.That&#8217;s all!</p>
<h3>What&#8217;s supported?</h3>
<p>It supports basic gettext calls. We have run it over our code base and it converts all gettext calls we use without any problem. </p>
<p><strong>A normal gettext call</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">_<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;to be translated&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>converts to:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">I18n.<span style="color:#9900CC;">t</span> <span style="color:#ff3333; font-weight:bold;">:message_0</span>, <span style="color:#ff3333; font-weight:bold;">:scope</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:txt</span>, <span style="color:#ff3333; font-weight:bold;">:controller</span>, <span style="color:#ff3333; font-weight:bold;">:controller_name</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p> </p>
<h4>A gettext call with variables</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">_<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;My name is %{name}&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:name =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;Jaap&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>converts to:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">I18n.<span style="color:#9900CC;">t</span> <span style="color:#ff3333; font-weight:bold;">:message_0</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;Jaap&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:scope</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:txt</span>, <span style="color:#ff3333; font-weight:bold;">:controller</span>, <span style="color:#ff3333; font-weight:bold;">:controller_name</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p> </p>
<p><strong>A gettext call with variables that contain gettext calls</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">_<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Click %{link} to go to the homepage&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:link =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; link_to<span style="color:#006600; font-weight:bold;">&#40;</span>_<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Here&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, root_path<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>converts to:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">I18n.<span style="color:#9900CC;">t</span> <span style="color:#ff3333; font-weight:bold;">:message_0</span>, <span style="color:#ff3333; font-weight:bold;">:link</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; link_to<span style="color:#006600; font-weight:bold;">&#40;</span>I18n.<span style="color:#9900CC;">t</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:message_1</span>, <span style="color:#ff3333; font-weight:bold;">:scope</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:txt</span>, <span style="color:#ff3333; font-weight:bold;">:controller</span>, <span style="color:#ff3333; font-weight:bold;">:controller_name</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, root_path<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:scope</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:txt</span>, <span style="color:#ff3333; font-weight:bold;">:controller</span>, <span style="color:#ff3333; font-weight:bold;">:controller_name</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p> </p>
<h3>Installation</h3>
<p><code>./script/plugin install git://github.com/japetheape/gettext_to_i18n.git</code></p>
<h3>Usage</h3>
<p>To convert your application:<br />
<code>rake gettext_to_i18n:transform</code></p>
<p>Please make sure you backup your complete application as it can screw things up.</p>
<h3>Contribution</h3>
<p>Please contribute to this plugin and make it better, as I won&#8217;t use it anymore, cause we are not going to convert another time (I think <img src='http://techblog.floorplanner.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ). Things that has to be done are:</p>
<p>unnamed variables:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">_<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;I play the %s&quot;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#996600;">&quot;saxophone&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Go to the development location at <a title="Github" href="http://github.com/japetheape/gettext_to_i18n/tree/master">github</a> and fork this plugin!</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2008/09/16/converting-a-rails-application-from-gettext-to-i18n/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
