2012-06-02 106 views
0

由於我本地化了我的應用程序,所以遇到rails-breadcrumb問題。rails-breadcrumb和I18n

在我的控制,我有這樣的:

class FooController < PrivateController 
    add_breadcrumb I18n.t('breadcrumbs.foo.index'), :foo_url 
end 

當顯示我的麪包屑,本地化的字符串總是從en.yml拍攝,無論我在I18n.locale

設置的語言了之後看看代碼,它發生add_breadcrumb作爲before_filter,並經過一些測試後,我得出結論,即使add_breadcrumb的內容具有正確的語言環境,似乎傳遞的值不是。

如果我試試這個:

add_breadcrumb I18n.t('breadcrumbs.foo.index', :locale => "fr"), :foo_url 

一切順利。

如何強制我的字符串正確本地化?

謝謝您的預付款

回答

1

我終於明白了這一點。在我確定我的問題是因爲I18n對我的語言環境不瞭解,因爲我要求它翻譯某些內容的事實後,我爲了管理本地化本身而使用了rails- breadcrumb。

知道我傳遞一個符號作爲第一參數,以及i調用I18n.translate()在導軌-麪包屑

add_breadcrumb (:'breadcrumbs.foo.index'), :foo_url 

d

# config/initializers/rails-breadcrumb-fix.rb 
module Rails 
    module Breadcrumbs 

    class ActionController::Base 

     protected 

     def add_breadcrumb(name, url = '') 
     @breadcrumbs ||= [] 
     # if given `name` is a Symbol, we localize it 
     if name.is_a?(Symbol) 
      name = I18n.t(name) 
     end 
     url = send(url) if url.is_a?(Symbol) 
     @breadcrumbs << [name, url] 
     end 

     def self.add_breadcrumb(name, url, options = {}) 
     before_filter options do |controller| 
      controller.send(:add_breadcrumb, name, url) 
     end 
     end 

    end 

    module Helper 

     def breadcrumbs(separator = "&rsaquo;") 
     @breadcrumbs.map do |txt, path| 
      link_to_unless (path.blank? || current_page?(path)), h(txt), path 
     end.join(" #{separator} ").html_safe 
     end 

    end 

    end 
end 

ActionController::Base.send(:include, Rails::Breadcrumbs) 
ActionView::Base.send(:include, Rails::Breadcrumbs::Helper)