注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 [业内传闻]今天,7月25日..
 帮助

Ruby-GetText-Package 的使用


2007-04-03 02:30:47
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://blackanger.blog.51cto.com/140924/22398
Ruby-GetText-Package HOWTO for Ruby on Rails
 原文地址見我上篇blog里的鏈接
Ruby-GetText-Package是对ROR有力的支持。
这个指南通过一个包含了GetText的小的blog应用来说明如何在ROR中来使用Ruby-GetText-Package
 
让我们开始开发一个本地化的blog应用
 
这个指南中关于这个blog应用的信息如下:
Appliction name: blog
Textdomain name: blog
Version: 1.0.0
charset: UTF-8 - Almost of all cases, UTF-8 is recommanded.
它有一個名叫articles的表:
CREATE TABLE `articles` (
 `id` int(11) NOT NULL auto_increment,
 `title` varchar(255) NOT NULL default '',
 `description` text NOT NULL,
 `lastupdate` date default NULL,
 PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
然后讓我們創建這個database 并且 script/generate.命令來生成blog應用。
 
一.編輯文件
(一).config/environment.rb中如下設置
$KCODE=’u’
require ‘jcode’
.
.
require ‘gettext/rails’
設置字符集的語句放到起始位,然后require那句放到文件尾部。
(二).ApplicationController中如下設置:
整個應用系統都需要調用init_gettext,于是:
        class ApplicationController < ActionController::Base
               init_gettext "blog"
        end
 
到現在為止,你就能發現這個驗證消息已經被本地化了。
(三).init_gettext選項:
你也可以設置charsetcontent_type在這里。
綁定textdomainM/V/C
options:
:charset – 輸出字符集.默認是 "UTF-8"
:content_type – 文本類型。默認是 "text/html"
:locale_path - locale 目錄地址. 默認是 RAILS_ROOT 或者是plugin root 根目錄。
如果你想為每一個controller設置專門的textdomain,你需要在每一個controller里調用init_gettext
如:
        class BlogController < ApplicationController
               init_gettext "blog"
               :
               :
        end
 
注意Ruby-GetText設置了正確的字符集。所以你自己就沒必要再像下面的設置那樣多此一舉了。
        #Needless!(也不是不需要,我們處理中文亂碼的時候需要)
        before_filter :set_charset
        def set_charset
                headers['Content-Type'] = "text/html;charset=utf-8"
end
 
當我們處理中文亂碼的時候,把這個過濾器放到ApplicationController.rb中就可以了。注意Charset設置為GB2312.
 
() Controllers設置
      
       從這節開始,你可以在需要的時候用下面的這些function
       編輯controllers或是viewsGettext方法。下面的例子演示了編輯一個controller
       blog_controller.rb
   class BlogController < ApplicationController
         :
         :
         def create
            @article = Article.new(params[:article])
            if @article.save
   flash[:notice] = _('Article was successfully created.')  #Here!
             redirect_to :action => 'list'
            else
             render :action => 'new'
            end
         end
        :
        :
     end
下面是改view的一個例子:
<h1><%= _('Editing article') %></h1>
 
<%= start_form_tag :action => 'update', :id => @article %>
  <%= render_partial 'form' %>
  <p><%= submit_tag _('Edit') %></p>
<%= end_form_tag %>
<p>
<%= link_to _('Show'), :action => 'show', :id => @article %> |
<%= link_to _('Destroy'), {:action => 'destroy', :id => @article}, :confirm => _('Are you sure?') %> |
<%= link_to _('Back'), :action => 'list' %>
</p>
 
 
error_messages_for, error_message_on都被本地化了,你不需要做任何事情。
 
暫且跳過N段:)
(五)         .Rakefile
創建lib/tasks/gettext.rake
desc "Update pot/po files."
task :updatepo do
  require 'gettext/utils'
  GetText.update_pofiles("blog", Dir.glob("{app,lib,bin}/**/*.{rb,rhtml}"), "blog 1.0.0")
end
 
desc "Create mo-files"
task :makemo do
  require 'gettext/utils'
  GetText.create_mofiles(true, "po", "locale")
end
 
注意:po文件夾放到系統根目錄下,在po目錄下創建zh文件夾。然后運行命令:
  rake updatepo
會在po目錄下創建一個blog.pot文件。
在blog.pot文件中,如下
#: app/views/blog/edit.rhtml:1
msgid "Editing article"
msgstr ""
在msgstr里設置本地化語言信息。
然后運行:
  rake makemo
然后在運行一遍
  rake updatepo
  zh文件夾下會創建一個blog.po文件。
 
然后啟動服務器,嘗試吧。。。。
 
只翻譯其中一點點內容。。。翻譯的過程中,使自己的思維更加集中。。。是個不錯的學習方法。


上一篇 Ruby on Rails 国际化应用  下一篇 About ROR



    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: