<?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>Voland.com.cn &#187; Ruby</title>
	<atom:link href="http://www.voland.com.cn/category/coding/ruby/feed" rel="self" type="application/rss+xml" />
	<link>http://www.voland.com.cn</link>
	<description>人生百年,诸事记之</description>
	<lastBuildDate>Thu, 02 Feb 2012 23:09:02 +0000</lastBuildDate>
	<language>zh</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>ruby中的word2007文件修改操作</title>
		<link>http://www.voland.com.cn/modify-word2007-on-ruby-how-to</link>
		<comments>http://www.voland.com.cn/modify-word2007-on-ruby-how-to#comments</comments>
		<pubDate>Mon, 10 May 2010 01:17:27 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[软件开发]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rails教程]]></category>

		<guid isPermaLink="false">http://www.voland.com.cn/?p=473</guid>
		<description><![CDATA[使用ruby修改word2007，到目前并没有好的库，当然，rails也一样，那么如何才能用ruby操作word2007文件呢？word2007文件，也就是docx文件，实际就是一个zip压缩的压缩包，它里面由一系列xml及其它与文件内容相关的资源文件组成，所以，最简单的办法就是解压这个docx文件，然后修改xml文件，再把他打包，完成修改。首先表一下本人的开发环境： ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9] gem 1.3.6 zipruby (0.3.6) nokogiri (1.4.1) libxml-ruby (1.1.3) Rails 2.3.4(这个非必须，本人是在rails下开发的) 刚才说了，修改的思路分为三步： 第一步：解压docx文件。 第二步：查找并修改document.xml文件中的内容 第三步：重新生成docx文件 根据以上三步，model代码对应如下： #office_open_xml.rb require 'zipruby' require 'nokogiri' &#160; class OfficeOpenXml def initialize&#40;template,newdoc&#41; #Store the instance variables @template, @newdoc = template, newdoc end &#160; def parse&#40;&#41; existing_xml = get_xml_from_template&#40;&#41; body_node = existing_xml.root.xpath&#40;&#34;w:body&#34;, &#123;&#34;w&#34; =&#62; &#34;http://schemas.openxmlformats.org/wordprocessingml/2006/main&#34;&#125;&#41;.first #修改文件操作 [...]]]></description>
			<content:encoded><![CDATA[<p>使用ruby修改word2007，到目前并没有好的库，当然，rails也一样，那么如何才能用ruby操作word2007文件呢？word2007文件，也就是docx文件，实际就是一个zip压缩的压缩包，它里面由一系列xml及其它与文件内容相关的资源文件组成，所以，最简单的办法就是解压这个docx文件，然后修改xml文件，再把他打包，完成修改。首先表一下本人的开发环境：<span id="more-473"></span><br />
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9]<br />
gem 1.3.6<br />
zipruby (0.3.6)<br />
nokogiri (1.4.1)<br />
libxml-ruby (1.1.3)<br />
Rails 2.3.4(这个非必须，本人是在rails下开发的)<br />
<br/><br />
刚才说了，修改的思路分为三步：<br />
第一步：解压docx文件。<br />
第二步：查找并修改document.xml文件中的内容<br />
第三步：重新生成docx文件</p>
<p></br><br />
根据以上三步，model代码对应如下：</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#office_open_xml.rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'zipruby'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'nokogiri'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> OfficeOpenXml  
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>template,newdoc<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;">#Store the instance variables</span>
    <span style="color:#0066ff; font-weight:bold;">@template</span>, <span style="color:#0066ff; font-weight:bold;">@newdoc</span> = template, newdoc
  <span style="color:#9966CC; font-weight:bold;">end</span>  
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> parse<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    existing_xml = get_xml_from_template<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    body_node = existing_xml.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">xpath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;w:body&quot;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;w&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;http://schemas.openxmlformats.org/wordprocessingml/2006/main&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span>
    <span style="color:#008000; font-style:italic;">#修改文件操作</span>
    body_node.<span style="color:#9900CC;">xpath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;//w:r//w:t&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>child<span style="color:#006600; font-weight:bold;">|</span>
      child.<span style="color:#9900CC;">content</span> = child.<span style="color:#9900CC;">content</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;{name}&quot;</span>, <span style="color:#996600;">&quot;replace string&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>    
&nbsp;
   compress<span style="color:#006600; font-weight:bold;">&#40;</span>existing_xml<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#解压文件</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> get_xml_from_template<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    filename = <span style="color:#996600;">'word/document.xml'</span>;
    <span style="color:#008000; font-style:italic;">#retrieve the document from the template doc</span>
    xml = <span style="color:#6666ff; font-weight:bold;">Zip::Archive</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>@template<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>zipfile<span style="color:#006600; font-weight:bold;">|</span>
      zipfile.<span style="color:#9900CC;">fopen</span><span style="color:#006600; font-weight:bold;">&#40;</span>filename<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#008000; font-style:italic;">#parse the resulting file into the Nokogiri xml doc</span>
    <span style="color:#6666ff; font-weight:bold;">Nokogiri::XML</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>xml<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#把修改过的文件重新生成docx</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> compress<span style="color:#006600; font-weight:bold;">&#40;</span>newXML<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;">#Copy the template to the new document</span>
    <span style="color:#CC00FF; font-weight:bold;">FileUtils</span>.<span style="color:#9900CC;">copy</span><span style="color:#006600; font-weight:bold;">&#40;</span>@template, <span style="color:#0066ff; font-weight:bold;">@newdoc</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;">#Open the zip archive</span>
    <span style="color:#6666ff; font-weight:bold;">Zip::Archive</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>@newdoc, <span style="color:#6666ff; font-weight:bold;">Zip::CREATE</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>zipfile<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#008000; font-style:italic;">#Replace the document.xml with our new xml</span>
      zipfile.<span style="color:#9900CC;">add_or_replace_buffer</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'word/document.xml'</span>, newXML.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>  
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Controller的代码对应如下：</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#office_controller.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> OfficeController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    office = OfficeOpenXml.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{RAILS_ROOT}/public/template/test.docx&quot;</span>,<span style="color:#996600;">&quot;#{RAILS_ROOT}/public/template/test333.docx&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    render <span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> office.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>  
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>如果有兴趣，大家也可以参看这篇文章：<br />
<a href="http://msdn.microsoft.com/en-us/library/ee895050.aspx" target="_blank">Using Ruby on Rails and XSLT to Create a Word 2007 Document</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/modify-word2007-on-ruby-how-to/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>在Rails构建的网站中生成SiteMap</title>
		<link>http://www.voland.com.cn/rails-apps-auto-generate-sitemap</link>
		<comments>http://www.voland.com.cn/rails-apps-auto-generate-sitemap#comments</comments>
		<pubDate>Thu, 15 Apr 2010 02:26:49 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[软件开发]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rails Sitemap Plugin]]></category>
		<category><![CDATA[rails sitemap生成器]]></category>
		<category><![CDATA[sitemap generator]]></category>
		<category><![CDATA[生成sitemap]]></category>

		<guid isPermaLink="false">http://www.voland.com.cn/?p=361</guid>
		<description><![CDATA[Rails构建网站非常快捷方便，特别适合大型网站的敏捷开发，国内这样的网站也很多，比如软件开发交流社区javaeye等，对于用Rails构建的网部，怎么快捷的生成sitemap呢？那就是使用rails sitemap Plugin,这样的插件在github关注度比较高的有queso / sitemap及adamsalter / sitemap_generator，由于前者很久没有更新了，我们主要介绍使用后者。 Sitemap_generator 这个插件能快捷通过rake任务读取数据库信息等等生成sitemap,充许生成Gzip格式的sitemap文件，充许带参与及SSL的链接，如mypage_list?pageid=4及https等等。它也支持sitemap的index索引方式。共可以支持2.5个亿链接的网站URL生成，所以，一般情况下是够用了。当rake生成任务完成时，它同时会向google、yahoo等主流的搜索引擎发通知。下面我们来使用它 一、Sitemap_generator的安装 Sitemap_generator提供插件方式安装与gem两种方式安装 1、插件方式安装，直接在控制台运行如下命令，注意这需要你的环境中安装了git： $ ./script/plugin install git://github.com/adamsalter/sitemap_generator.git 2、gem方式安装 首先修改config/environment.rb，增加如下代码： config.gem 'sitemap_generator', :lib =&#62; false, :source =&#62; 'http://gemcutter.org' 然后运行 $ rake gems:install 然后再增加如下行到你的RAILS_ROOT/Rakefile文件 require 'sitemap_generator/tasks' rescue LoadError 再运行 $ rake sitemap:install 二、使用与简单的例子 生成sitemap需要一个rb文件，这就是config/sitemap.rb，我们可以运行如下命令自动生成这个文件： rake sitemap:install 然后修改这个文件,首先把SitemapGenerator::Sitemap.default_host = “http://www.example.com”中的url改成你的网站地址， 而每生成一个url，都是通过类似sitemap.add articles_path, :priority =&#62; 0.7, :changefreq =&#62; &#8216;daily&#8217;的代码，所以你可以查询数据库，然后通过循环来动态生成。 三、生成与更新你的sitemap [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.voland.com.cn/rails-apps-auto-generate-sitemap"><img class="aligncenter size-full wp-image-377" title="sitemap" src="http://www.voland.com.cn/wp-content/uploads/2010/04/sitemap.gif" alt="sitemap 生成" width="580" height="200" /></a></p>
<p>Rails构建网站非常快捷方便，特别适合大型网站的敏捷开发，国内这样的网站也很多，比如软件开发交流社区javaeye等，对于用Rails构建的网部，怎么快捷的生成sitemap呢？那就是使用rails sitemap Plugin,这样的插件在github关注度比较高的有queso / sitemap及adamsalter / sitemap_generator，由于前者很久没有更新了，我们主要介绍使用后者。</p>
<p>Sitemap_generator 这个插件能快捷通过rake任务读取数据库信息等等生成sitemap,充许生成Gzip格式的sitemap文件，充许带参与及SSL的链接，如mypage_list?pageid=4及https等等。它也支持sitemap的index索引方式。共可以支持2.5个亿链接的网站URL生成，所以，一般情况下是够用了。当rake生成任务完成时，它同时会向google、yahoo等主流的搜索引擎发通知。下面我们<span id="more-361"></span>来使用它</p>
<p><strong>一、Sitemap_generator的安装</strong><br />
Sitemap_generator提供插件方式安装与gem两种方式安装<br />
1、插件方式安装，直接在控制台运行如下命令，注意这需要你的环境中安装了git：<br />
<code>$ ./script/plugin install git://github.com/adamsalter/sitemap_generator.git</code><br />
2、gem方式安装<br />
首先修改config/environment.rb，增加如下代码：<br />
<code>config.gem 'sitemap_generator', :lib =&gt; false, :source =&gt; 'http://gemcutter.org'</code><br />
然后运行<br />
<code>$ rake gems:install</code><br />
然后再增加如下行到你的RAILS_ROOT/Rakefile文件<br />
<code>require 'sitemap_generator/tasks' rescue LoadError</code><br />
再运行<br />
<code>$ rake sitemap:install</code></p>
<p><strong>二、使用与简单的例子</strong><br />
生成sitemap需要一个rb文件，这就是config/sitemap.rb，我们可以运行如下命令自动生成这个文件：<br />
<code>rake sitemap:install</code><br />
然后修改这个文件,首先把SitemapGenerator::Sitemap.default_host = “http://www.example.com”中的url改成你的网站地址，<br />
而每生成一个url，都是通过类似sitemap.add articles_path, :priority =&gt; 0.7, :changefreq =&gt; &#8216;daily&#8217;的代码，所以你可以查询数据库，然后通过循环来动态生成。</p>
<p><strong>三、生成与更新你的sitemap</strong><br />
代码完成后，运行<br />
<code>rake sitemap:refres</code><br />
来生成或者更新你的sitemap,当然，他生成后，会自动去通知搜索引擎，如果你不需要这个功能，可以为这条命令增加一个-s参数，如下：<br />
<code>rake -s sitemap:refresh</code><br />
如果你的网站的链接超过10万条，生成文件需要一定的时间，那么我建议使用系统的cron任务来更新你的sitemap。文件生成后，放在public目录了，最后你需要做的，就是在robots.txt增加一条记录：<br />
Sitemap: /sitemap_index.xml.gz<br />
什么意思不用问我了吧：）</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/rails-apps-auto-generate-sitemap/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>使用Gettext实现rails中的多语言版教程</title>
		<link>http://www.voland.com.cn/using-the-gettext-implementation-multilanguage-for-rails</link>
		<comments>http://www.voland.com.cn/using-the-gettext-implementation-multilanguage-for-rails#comments</comments>
		<pubDate>Fri, 02 Apr 2010 03:29:01 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gettext]]></category>
		<category><![CDATA[Rails多语言]]></category>
		<category><![CDATA[Rails教程]]></category>

		<guid isPermaLink="false">http://www.voland.com.cn/?p=280</guid>
		<description><![CDATA[使用Gettext实现rails中的多语言版教程]]></description>
			<content:encoded><![CDATA[<p>Gettext 用于系统的国际化(I18N)和本地化(L10N),可以在编译程序的时候使用本国语言支持(Native Language Support(NLS)),其可以使程序的输出使用用户设置的语言而不是英文.安装Gettext，可以下载gem包或者在线安装。 直接安装的方法是：</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">gem install gettext</pre></div></div>

<p>完成后配置你的项目： </p>
<p><strong>a. 在application.rb加入 </strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"> <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'gettext/rails'</span>   <span style="color:#008000; font-style:italic;"># 这句一定要加  </span>
&nbsp;
 <span style="color:#9966CC; font-weight:bold;">def</span> cookie_lang<span style="color:#006600; font-weight:bold;">&#40;</span>my_lang<span style="color:#006600; font-weight:bold;">&#41;</span>          
 cookies<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;lang&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = my_lang  
 <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><span id="more-280"></span><br />
<strong>b. 在Rakefile文件增加包含文件并增加两个任务</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"> <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'gettext/utils'</span>  
&nbsp;
desc <span style="color:#996600;">&quot;Update pot/po files to match new version.&quot;</span>   
task <span style="color:#ff3333; font-weight:bold;">:updatepo</span> <span style="color:#9966CC; font-weight:bold;">do</span>  
   MY_APP_TEXT_DOMAIN = <span style="color:#996600;">&quot;taito&quot;</span>   
   MY_APP_VERSION     = <span style="color:#996600;">&quot;taito 0.0.1&quot;</span>      
   <span style="color:#6666ff; font-weight:bold;">GetText::ActiveRecordParser</span>.<span style="color:#9900CC;">init</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:activerecord_classes</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'ActiveRecord::Base'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>  
   GetText.<span style="color:#9900CC;">update_pofiles</span><span style="color:#006600; font-weight:bold;">&#40;</span>MY_APP_TEXT_DOMAIN,   
   <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">glob</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;{app,lib}/**/*.{rb,rhtml,html.erb,rjs,rxml}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>,   
      MY_APP_VERSION<span style="color:#006600; font-weight:bold;">&#41;</span>  
   <span style="color:#9966CC; font-weight:bold;">end</span>  
&nbsp;
   desc <span style="color:#996600;">&quot;Create mo-files for L10n&quot;</span>   
     task <span style="color:#ff3333; font-weight:bold;">:makemo</span> <span style="color:#9966CC; font-weight:bold;">do</span>  
     GetText.<span style="color:#9900CC;">create_mofiles</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#996600;">&quot;po&quot;</span>, <span style="color:#996600;">&quot;locale&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>  
   <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>c.在enviornment.rb 中加入 </strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'gettext/rails'</span></pre></div></div>

<p><strong>d.在根目录下建立po目录，并增加几种语言目录 en , zh, jp等。</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">   po
   <span style="color:#006600; font-weight:bold;">|-</span> en
   <span style="color:#006600; font-weight:bold;">|-</span> zh
   <span style="color:#006600; font-weight:bold;">|-</span> jp</pre></div></div>

<p><strong>e.如果是在windows下安装，可能还要装一个二进制版本的gettext </strong><br />
下载地址：<a href="http://download.csdn.net/source/461970">http://download.csdn.net/source/461970</a><br />
如果不装可能会出现如下情况:<br />
No such file or directory &#8211; msgmerge po/xx.pot tmp.pot<br />
装好后，在windows下配好相应bin目录的path。（包括gem的gettext/bin 也要配好）</p>
<p><strong>f.在根目录下运行rake updatepo</strong><br />
  在项目的po目录下会生成pot文件。将这个文件改名为po后，拷贝到en ,zh ,jp 下。</p>
<p><strong>g.在根目录下运行rake makemo</strong><br />
  这个命令会自动在 locale 下创建对应的语言版本以及 .mo 文件，翻译工作完成</p>
<p><strong>h.在要显示多语言的地方</strong><br />
   如.rb文件，.rhtml文件用_(&#8216;square&#8217;) ，来表示，每加一个这样的符号。在rake updatepo时会自动对应出相关文件。</p>
<p><strong>j.切换语言的方法即是改变cookie</strong>  如：</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"> Views:
<span style="color:#006600; font-weight:bold;">&lt;%</span>=link_to _<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Chinese'</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:controller</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'/admins'</span>, <span style="color:#ff3333; font-weight:bold;">:action</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'select_lang'</span>,:lang<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'zh'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>  
<span style="color:#006600; font-weight:bold;">&lt;%</span>=link_to _<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'English'</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:controller</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'/admins'</span>, <span style="color:#ff3333; font-weight:bold;">:action</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'select_lang'</span>,:lang<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'en'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>  
&nbsp;
   Controller:  # 例子，根据自己的需要改变  
   def select_lang     
      lang = params[:lang]  
      lang = 'zh' unless lang =~ /(en)|(zh)|(jp)/  
      cookies['lang'] = lang  
      redirect_to home_path  
   end</pre></div></div>

<p>本文章转摘：<a href="http://taito.javaeye.com/blog/195148">http://taito.javaeye.com/blog/195148</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/using-the-gettext-implementation-multilanguage-for-rails/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

