`
hideto
  • 浏览: 2679556 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Globalize Getting Started

    博客分类:
  • Ruby
阅读更多
1, install
script/plugin install svn://svn.globalize-rails.org/globalize/trunk


2, set up tables and initialize data
rake globalize:setup


3, set base language
# environment.rb

include Globalize
Locale.set_base_language 'en-US'


4, set before_filter
# application.rb

before_filter :set_locale

def set_locale
  default_locale = 'en-US'
  request_language = request.env['HTTP_ACCEPT_LANGUAGE']
  request_language = request_language.nil? ? nil : request_language[/[^,;]+/]
  @locale = params[:locale] || session[:locale] || request_language || default_locale
  session[:locale] = @locale
  begin    
    Locale.set @locale  
  rescue    
    Locale.set default_locale  
  end
end


*****************************************************************

1) Translate Model
def Page < ActiveRecord::Base  
    translates :title
end

Locale.set('en-US')
page = Page.create!(:title => 'Welcome to Globalize!')
Locale.set('de-DE')page.reload # we'd get a Globalize::WrongLanguageError here w/o this
page.title = 'Willkommen bei Globalize!'
page.save


2) Translate View
Locale.set("de-DE")
Locale.set_translation('Welcome', 'Willkommen')

lang = Language.pick('de-DE')
Locale.set_translation('Welcome', lang, 'Willkommen')

>> Locale.set("de-DE")
>> "Welcome".t
"Willkommen" 


3) Translate Date
Product.find(:first).created_on.loc("%A %d-%B-%Y")
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics