<?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>Dmitri's Blog &#187; code</title>
	<atom:link href="http://www.dmitri.me/blog/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dmitri.me/blog</link>
	<description>Dmitri Smirnov. Web development in Estonia.</description>
	<lastBuildDate>Tue, 10 Aug 2010 12:07:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Internationalization in Rails 2.2</title>
		<link>http://www.dmitri.me/blog/internationalization-in-rails-22/</link>
		<comments>http://www.dmitri.me/blog/internationalization-in-rails-22/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 06:41:38 +0000</pubDate>
		<dc:creator>Dmitri</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[web-development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Internationalization]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.dmitri.me/blog/?p=274</guid>
		<description><![CDATA[This tutorials is about Internationalization in Ruby on Rails 2.2 and later. You don&#8217;t need to invent a wheel from scratch all stuff for internationalization and localization of your application is available in standard Rails package since version 2.2.

Configuration
So let&#8217;s start with empty Rails application.
$rails test_locale
In directory APP_ROOT/config/locales/ you will find file en.yml this is [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorials is about Internationalization in <a href="http://www.rubyonrails.org/">Ruby on Rails 2.2</a> and later. You don&#8217;t need to invent a wheel from scratch all stuff for internationalization and localization of your application is available in standard Rails package since version 2.2.</p>
<p><span id="more-274"></span></p>
<h3>Configuration</h3>
<p>So let&#8217;s start with empty Rails application.<br />
<code>$rails test_locale</code></p>
<p>In directory APP_ROOT/config/locales/ you will find file <strong>en.yml</strong> this is file which contains all texts for English version of your application. Here in Estonia we usually make applications for 3 or more languages. So I copy en.yml and name files as locale.</p>
<p><strong>en.yml</strong> &#8211; for English<br />
<code>en:<br />
&nbsp;&nbsp;hello: "Hello world"<br />
</code></p>
<p><strong>ru.yml</strong> &#8211; for Russian<br />
<code>ru:<br />
&nbsp;&nbsp;hello: "Привет мир!"<br />
</code></p>
<p><strong>ee.yml</strong> &#8211; for Estonian<br />
<code>ee:<br />
&nbsp;&nbsp;hello: "Tere maailm!"<br />
</code></p>
<p>Now lets do a controller for test.<br />
<code>test_locale$ ruby script/generate controller SayHello index</code></p>
<p>Now open view file in APP_ROOT/app/views/say_hello/index.html.erb and edit like:<br />
<code>&lt;%=t :hello %&gt;</code><br />
<strong>t</strong> &#8211; function is a helper that translates the text for current locale. If locale text is missing, it will generate message that &#8220;translation is missing&#8221; it doesn&#8217;t halt your application.</p>
<p>Locale will pass the parameter as query_string ?locale=ru and you need to add this manually for every URL in you application, of course you don&#8217;t like to do this manually. If you want that our app looks like <a href="http://www.example.com/ru/say_hello">http://www.example.com/ru/say_hello</a> go to APP_ROOT/config/routes.rb end add following line<br />
<code>map.connect ':locale/:controller/:action/:id', :controller =&gt; 'say_hello', :action =&gt; 'index', :requirements =&gt; {:locale =&gt; /ee|ru|en/}<br />
</code><br />
This will set for ROOT controller too. In requirement we set all available locales to avoid people enter in URL shitty things. After you set this route, Rails will generate pretty nice URLs with <strong>link_to, link_to_remote, etc&#8230;</strong></p>
<p>Now we need to a function to set locale in every controller of you application. Open APP_ROOT/app/controllers/application_controller.rb and edit:<br />
<code>class ApplicationController &lt; ActionController::Base<br />
&nbsp;&nbsp;helper :all # include all helpers, all the time<br />
&nbsp;&nbsp;protect_from_forgery<br />
&nbsp;&nbsp;<strong>before_filter :set_locale<br />
&nbsp;&nbsp;def set_locale<br />
&nbsp;&nbsp;&nbsp;&nbsp;locale = params[:locale]<br />
&nbsp;&nbsp;&nbsp;&nbsp;I18n.locale = locale<br />
&nbsp;&nbsp;&nbsp;&nbsp;I18n.load_path += Dir[ File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}') ]<br />
&nbsp;&nbsp;end</strong><br />
end<br />
</code></p>
<p>If you want to setup default locale different than English(en). Open file APP_ROOT/config/environment.rb and commentate following lines (usually at the end of file) and set up default locale. For example it will be Russian:<br />
<code>config.i18n.load_path += Dir[Rails.root.join('config','locales', '*.{rb,yml}')]<br />
config.i18n.default_locale = :ru<br />
</code></p>
<h3>Result</h3>
<p>Lets see what we got:<br />
<code>$ruby script/server</code></p>
<p>Open the URL in your browser <a href="http://localhost:3000/say_hello">http://localhost:3000/say_hello</a></p>
<p>Default (we set by default Russian[ru] locale)<br />
<img src="/misc/ri-ru.jpg" alt="" /></p>
<p>English<br />
<img src="/misc/ri-en.jpg" alt="" /></p>
<p>Estonian<br />
<img src="/misc/ri-ee.jpg" alt="" /></p>
<p>Thats all! Really isn&#8217;t hard?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dmitri.me/blog/internationalization-in-rails-22/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
