railsの国際化


見てみて、選んで選んで、やはりglobaliteを選んで、原因はいくつかあります:1.中国語があって、比較的に親切で、しかも中国人が関心を持っていることを知っていて、いつも良いことです.2.いくつかのおすすめを見たことがありますが、悪くないと言われています.3.試してみると、手が速くて、修正量はまあまあで、例はまあまあです.4.google codeのsvn速度は速いです.
これをインストールするのは簡単で、インストールと呼ばないほどで、長い間資料をめくって、svnダウンロードだけでいいことを確認しました.
vendor/pluginsでsvn checkouthttp://globalite.googlecode.com/svn/trunk/globalite注:自分のsvnと混在している場合は、2つの方法があります.1つは自分のsvnに組み込まれ、もう1つはexternalsプロパティを使用します.2つ目の方法を紹介します.プロジェクトディレクトリの下に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
アプリケーションが見つかりました.rb,set_localeはそのfilterですが、ちょっと問題があるようです.FFが発する言語はen-usで、後ろの2つは小文字なので、使う前に大文字に変えて有効になります.
このfilterがあればブラウザからの情報に基づいて自動的に言語を転送することができ、sessionやコミットパラメータに基づいて言語を変更することもできます.
プロジェクトディレクトリの下にlangディレクトリを建てて、次のレベルにuiディレクトリを建てて、中にいくつかの言語ファイルを置いて、安心してpoではありませんて、ただいくつかの最もよくあるyaml、完全なrubyスタイルです.ファイル名は言語コードと国際コード、例えばen-US.yml, zh-CN.yml .中身は一対のkey:valueでいいです.キーが一致することに注意してください.
そしてどんなviewにも<%=:login_と書くことができますkey.l("login")%>,一番前はkey,lはメソッド,後ろのパラメータは翻訳内容のデフォルト表示が見つからない.数字1ではなく英語の小文字lに注意してください.
これで終わりです.次は大量の翻訳です.
filterコードを添付します(例から写し、formatを追加しました):
rubyコード
 
private  
# Set the locale from the parameters, the session, or the navigator  
# If none of these works, the Globalite default locale is set (en-*)  
def set_locale  
  # Get the current path and request method (useful in the layout for changing the language)  
  @current_path = request.env['PATH_INFO']  
  @request_method = request.env['REQUEST_METHOD']  
  
  # Try to get the locale from the parameters, from the session, and then from the navigator  
  if params[:user_locale]  
      locale_code = format_locale_code(params[:user_locale][:code]) 
      #debug_log "[set_locale] #{locale_code} locale passed"  
      # Store the locale in the session  
      session[:locale] = locale_code  
  elsif session[:locale]  
      locale_code = session[:locale]  
      #debug_log "[set_locale] loading locale: #{locale_code} from session"  
  else  
      locale_code=format_locale_code(get_valid_lang_from_accept_header)  
      #debug_log "[set_locale] found a valid http header locale: #{locale_code}"  
  end  
    
  Locale.code =locale_code  
  #debug_log "[set_locale] Locale set to #{Locale.code}"  
  # render the page  
  yield  
  
  # reset the locale to its default value  
  Locale.reset!  
end  
  
def format_locale_code(locale_code)      
  result=locale_code.to_s  
  if result[0,2]  
    lang = result[0,2].downcase  
  end  
  if result[3,5]  
    country = result[3,5].upcase  
    result="#{lang}-#{country}".to_sym  
  else  
    result="#{lang}-*".to_sym  
  end  
  return result  
end  
  
# Get a sorted array of the navigator languages  
def get_sorted_langs_from_accept_header  
  accept_langs = request.env['HTTP_ACCEPT_LANGUAGE'].split(/,/) rescue nil  
  return nil unless accept_langs  
  
  # Extract langs and sort by weight  
  # Example HTTP_ACCEPT_LANGUAGE: "en-au,en-gb;q=0.8,en;q=0.5,ja;q=0.3"  
  wl = {}  
  accept_langs.each {|accept_lang|  
      if (accept_lang + ';q=1') =~ /^(.+?);q=([^;]+).*/  
          wl[($2.to_f rescue -1.0)]= $1  
      end  
  }  
  return wl.sort{|a,b| b[0] <=> a[0] }.map{|a| a[1] }  
end  
  
# Returns a valid language that best suits the HTTP_ACCEPT_LANGUAGE request header.  
# If no valid language can be deduced, then nil is returned.  
def get_valid_lang_from_accept_header  
  # Get the sorted navigator languages and find the first one that matches our available languages  
  get_sorted_langs_from_accept_header.detect{|l| get_matching_ui_locale(l) }  
end  
  
# Returns the UI locale that best matches with the parameter  
# or nil if not found  
def get_matching_ui_locale(locale)  
  lang = locale[0,2].downcase  
  if locale[3,5]  
    country = locale[3,5].upcase  
    #debug_log "[globalite] trying to match locale: #{lang}-#{country}"  
    locale_code = "#{lang}-#{country}".to_sym  
  else  
    #debug_log "[globalite] trying to match #{lang}-*"  
    locale_code = "#{lang}-*".to_sym  
  end  
  
  #locale_code=format_locale_code(locale_code)  
    
  # Check with exact matching  
  if Globalite.ui_locales.values.include?(locale_code)  
    debug_log "[globalite] Globalite does include #{locale_code}"  
    locale_code  
  end  
  
  # Check on the language only  
  Globalite.ui_locales.values.each do |value|  
    value.to_s =~ /#{lang}-*/ ? value : nil  
  end  
end