Globalize Getting Started

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")

你可能感兴趣的:(SVN,UP,ActiveRecord,Rails)