shitou's blog 

  • Home
  • About
  • Feed
  • 十人族
  • MIX BOY
  • 塌客

Tags "gettext"

10
Nov

rails国际化工具gettext的使用

其实一直觉得国际化的工具都没必要用的, 把所有要国际化的文字全部定义成变量放在一个文件中,每种语言对应一个文件,然后再结合JS来调用, 我觉得这样的方法就可以很方便的解决国际化的问题了, yahoo有很多应用也是这样做的, 不过,从项目开发的角度说使用国际化工具显得更专业点,其他我还倒真的没看出有什么优势,

 

说归说,现在项目要用了, 还得用, 现在主要的国际化工具就是gettext, 当然也有对应的ruby的了, 也可以用在rails中

 

1. 首先安装

#gem install gettext


2. 然后在config/environment.rb中加入

  require 'gettext'
  require 'gettext/rails'

 

3. 在项目的跟目录的Rakefile中加入

desc "Update pot/po files."
task :updatepo do
  require 'gettext/utils'
  GetText.update_pofiles("cal_point", Dir.glob("{app,lib,bin}/**/*.{rb,erb,rjs,rhtml}"), "cal_point_0.9")
end

desc "Create mo-files"
task :makemo do
  require 'gettext/utils'
  GetText.create_mofiles(true, "po", "locale")
end

 

4. 在application.rb中加入

class ApplicationController < ActionController::Base
  init_gettext 'cal_point'

end

cal_point是你项目的名字, 生成的po, mo文件也将以此命名

 

5. 建立目录po/zh, zh是语言目录, 可以根据自己的需要建立, 尊不遵循语言命名的标准都行


6. 在需要国际化的地方用"_()"方法来调用就行了, 比如: _("hello world");


7. 执行rake updatepo, 将会在po下生成文件cal_point.pot, 复制cal_point.pot到zh目录下为cal_point.po(我就在这里浪费了好多时间, 官方的文档中没有提到这一点), 然后编辑cal_point.po文件就行了, 注意文件编码一定要是UTF-8


8. 执行rake makemo, 将会自动生成/locale/zh/LC_MESSAGES/cal_point.mo, 这个文件是二进制文件不用管, gettext将自动调用,


9. gettext判断显示语言的方法有:

A. cookies中的lang设置

B. request中的lang变量

C. 也可以强制本地设置:

<% set_locale "en" %>
<%=_("Hello world") %>

关于使用一种方法是:在application.rb中:

  before_init_gettext :default_locale
  def default_locale
    if params[:lang].nil? or params[:lang].empty?
      set_locale "zh"
    else
      set_locale params[:lang]
    end
  end

 

就不多说了,资料比较多

参考资料:

Ruby-GetText-Package HOWTO for Ruby on Rails

Ruby-GetText-Package HOWTO maintain po/mo files


Tags: gettext

2008-11-10 17:29:44, 1242 reviews, comment

send to mailbox

Your email:

Tags

json Impactjs Canvas fun gen_server superfly PS3 Webgame HTML5 jquery SSH tenerer MooseFS gearman-ruby Gearman MongoDB MochiChat TCP U-ka saegusa IN db command Mai Kuraki Norah Jones log iPhoneException ACG Mac Safari objective-c CouchDB LVS AJAX debian 推荐 AMQP google mail bug gettext Erlang 北京 iptables 架构 tips mysql backup function 我看 postfix 监控 SEO cache Etag memcache thread 进程 线程 无锡 yield file column mixboy xml rss gems ruby shitou shell lighttpd 安全 csrf 公司 nginx linux 模块 apache webserver 朋友 大学 生活 尼古拉斯凯奇 movie 文件同步 笑笑 歌词 auto complete plugin rails music ubuntu blog

Category

  • HTML5[3]
  • iPhone[17]
  • Erlang[11]
  • google[13]
  • 生活[45]
  • 音乐[13]
  • 电影[11]
  • linux[29]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[9]
  • ruby[20]
  • ruby on rails[28]
  • 开源[4]

Episode

  • MongoDB
  • MochiChat
  • iPhoneException
  • shell
  • thread
  • memcache


Popular Posts

  • 准备开始学习Erlang了(恶狼, 二郎..)
  • Ruby遍历MemCached的key
  • 服务器监控小脚本
  • MySQL Innodb备份
  • [转载]5个有用的ruby gems

Recommended Posts

  • Tri-survive - HTML5 Game
  • Cut the rope - HTML5版
  • json_formatter
  • 在gen_server中spawn新的进程
  • Superfly - Wildflowers
  • 继续凸墙 for Mac OS
  • MongoDB Beijing 2011
  • MongoDB删除map_reduce生成的tmp collection
  • Mai Kuraki - Future Kiss
  • Erlang OOP
  • 用SSH tunnel凸墙
  • Google的语法高亮工具包
  • 大量数据的批量操作
  • 再次被和谐-_-
  • 十人族: 上线了

Friends' blogs

  • levy
  • sphance
  • andreas
  • yangkunlun
  • {:dev=>:wxianfeng}
  • bheye
  • joeydarko

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 316243
  • 今天访问: 38
  • 日志: 213
  • 评论: 4967
  • 音乐: 9
  • 用户: 1758


少年,不点下广告吗!

 

all by shitou

blog comments powered by Disqus

close