对rails国际化

看来看去,挑来挑去,还是选了globalite,原因有几点:1.有中文,比较亲切,并且知道有中国人在关注,总是好事。2.看过几个推荐的,都说还不 错。3.试了一下,上手快,修改量还行,例子写得还可以。4.google code上的,svn速度挺快。

安装这个很简单,甚至都不叫安装,以至于我还翻了半天资料,确认了只需要svn下载就行了。
在vendor/plugins下
svn checkout http://globalite.googlecode.com/svn/trunk/ globalite

注:如果跟你自己的svn混了,那么有两个方法,一个是并入你自己的 svn,另一个是使用externals属性。介绍一下第二个方法:
在项目目录下打入  svn propedit svn:externals vendor/plugins ,在接下来的编辑窗口里输入一行
globalite http://globalite.googlecode.com/svn/trunk/
然后保存退出
再打入命令: svn ci -m "external" vendor/plugins  , 提交一下修改
再更新一下:svn up vendor/plugins/
就完成了,svn将不会理会globalite目录,而且下载的时候会自动去google code。

使用也很简单,如果你是一个完全国际化的应用,那么必然要使用一个filter来确定语言,这个filter可以从他的例子里抄来,非常简单。

例子的介绍:
http://www.railsontherun.com/2007/7/2/globalite-sample-application
下载
svn checkout http://globalite.googlecode.com/svn/sample/ui globalite-sample-app
找到application.rb,set_locale就是那个filter,不过似乎有点问题,因为FF发出的语言是en-us,后面两个是小写的,在使用前要转成大写才生效。

有了这个filter就可以根据浏览器发出的信息自动转语言,它也可以根据session里或提交参数来改变语言。

再 在项目目录 下建一个lang 目录,再下一级建一个ui目录,里面放上一些语言文件,放心不是po,只是一些最常见的yaml,完全的ruby风格。文件名要根据语言代码和国际代码, 比如:en-US.yml, zh-CN.yml 。里面的内容就是一对对key: value就可以了。注意key要一致。

然后在任何view里,可以写上 <%= :login_key.l("login") %> ,最前面是key,l是方法,后面的参数是找不到翻译内容的缺省显示。注意是英文小写l 而不是数字1。

这样就搞定了,接下去就是海量的翻译了。

附filter代码(从例子里抄来,加了一个format):
ruby 代码
 
  1. private  
  2. # Set the locale from the parameters, the session, or the navigator  
  3. # If none of these works, the Globalite default locale is set (en-*)  
  4. def set_locale  
  5.   # Get the current path and request method (useful in the layout for changing the language)  
  6.   @current_path = request.env['PATH_INFO']  
  7.   @request_method = request.env['REQUEST_METHOD']  
  8.   
  9.   # Try to get the locale from the parameters, from the session, and then from the navigator  
  10.   if params[:user_locale]  
  11.       locale_code = format_locale_code(params[:user_locale][:code]) 
  12.       #debug_log "[set_locale] #{locale_code} locale passed"  
  13.       # Store the locale in the session  
  14.       session[:locale] = locale_code  
  15.   elsif session[:locale]  
  16.       locale_code = session[:locale]  
  17.       #debug_log "[set_locale] loading locale: #{locale_code} from session"  
  18.   else  
  19.       locale_code=format_locale_code(get_valid_lang_from_accept_header)  
  20.       #debug_log "[set_locale] found a valid http header locale: #{locale_code}"  
  21.   end  
  22.     
  23.   Locale.code =locale_code  
  24.   #debug_log "[set_locale] Locale set to #{Locale.code}"  
  25.   # render the page  
  26.   yield  
  27.   
  28.   # reset the locale to its default value  
  29.   Locale.reset!  
  30. end  
  31.   
  32. def format_locale_code(locale_code)      
  33.   result=locale_code.to_s  
  34.   if result[0,2]  
  35.     lang = result[0,2].downcase  
  36.   end  
  37.   if result[3,5]  
  38.     country = result[3,5].upcase  
  39.     result="#{lang}-#{country}".to_sym  
  40.   else  
  41.     result="#{lang}-*".to_sym  
  42.   end  
  43.   return result  
  44. end  
  45.   
  46. # Get a sorted array of the navigator languages  
  47. def get_sorted_langs_from_accept_header  
  48.   accept_langs = request.env['HTTP_ACCEPT_LANGUAGE'].split(/,/) rescue nil  
  49.   return nil unless accept_langs  
  50.   
  51.   # Extract langs and sort by weight  
  52.   # Example HTTP_ACCEPT_LANGUAGE: "en-au,en-gb;q=0.8,en;q=0.5,ja;q=0.3"  
  53.   wl = {}  
  54.   accept_langs.each {|accept_lang|  
  55.       if (accept_lang + ';q=1') =~ /^(.+?);q=([^;]+).*/  
  56.           wl[($2.to_f rescue -1.0)]= $1  
  57.       end  
  58.   }  
  59.   return wl.sort{|a,b| b[0] <=> a[0] }.map{|a| a[1] }  
  60. end  
  61.   
  62. # Returns a valid language that best suits the HTTP_ACCEPT_LANGUAGE request header.  
  63. # If no valid language can be deduced, then nil is returned.  
  64. def get_valid_lang_from_accept_header  
  65.   # Get the sorted navigator languages and find the first one that matches our available languages  
  66.   get_sorted_langs_from_accept_header.detect{|l| get_matching_ui_locale(l) }  
  67. end  
  68.   
  69. # Returns the UI locale that best matches with the parameter  
  70. # or nil if not found  
  71. def get_matching_ui_locale(locale)  
  72.   lang = locale[0,2].downcase  
  73.   if locale[3,5]  
  74.     country = locale[3,5].upcase  
  75.     #debug_log "[globalite] trying to match locale: #{lang}-#{country}"  
  76.     locale_code = "#{lang}-#{country}".to_sym  
  77.   else  
  78.     #debug_log "[globalite] trying to match #{lang}-*"  
  79.     locale_code = "#{lang}-*".to_sym  
  80.   end  
  81.   
  82.   #locale_code=format_locale_code(locale_code)  
  83.     
  84.   # Check with exact matching  
  85.   if Globalite.ui_locales.values.include?(locale_code)  
  86.     debug_log "[globalite] Globalite does include #{locale_code}"  
  87.     locale_code  
  88.   end  
  89.   
  90.   # Check on the language only  
  91.   Globalite.ui_locales.values.each do |value|  
  92.     value.to_s =~ /#{lang}-*/ ? value : nil  
  93.   end  
  94. end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值