rails-breadcrumb和I18n本地化

时间:2014-01-10 12:06:35

标签: ruby-on-rails internationalization breadcrumbs

我在我的应用程序中使用BreadcrumbsOnRails。 应用程序是完全本地化的,但我有一个导航转换问题。 呈现菜单时,本地化名称始终为俄语(即使是I18n.locale ='en')

应用程序控制器中设置区域设置:

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :set_locale

  add_breadcrumb I18n.t('navigation.home'), :root_path
  ...
  def set_locale
    I18n.locale = params[:locale] || session[:lang]
    session[:lang] = locale unless session[:lang] == locale
    I18n.locale = locale
  end
  ... 
end

我的下一个控制器带有痕迹:

class ExamsController < ApplicationController
  ...
  add_breadcrumb I18n.t('navigation.exams'), :exams_path
  before_filter :exam_breadcrumb, :only => [:show, :edit]
  ...
end

#config/lib/breadcrumbs_builder

使用breadcrumbs

haml 查看

- breadcrumbs = render_breadcrumbs(:separator => ' / ', :tag => :li, :builder =>       BreadcrumbsBuilder)
 - unless breadcrumbs.blank?
   .subnav#main-subnav
     .container
       %ul.breadcrumbs
          = breadcrumbs

使用此硬编码,一切正常:

add_breadcrumb I18n::t('navigation.exams', locale: 'en'), :exams_path

1 个答案:

答案 0 :(得分:1)

必须在单独的方法中添加面包屑。其他解决方案我还没找到......

class ExamsController < ApplicationController

   before_filter :exam_breadcrumb_translate
   before_filter :exam_breadcrumb, :only => [:show, :edit]
   ...

   private

   def exam_breadcrumb_translate
     add_breadcrumb I18n::t('navigation.exams'), :exams_path
   end

   def exam_breadcrumb
     @exam = Exam.find(params[:id])
     add_breadcrumb @exam.name, exam_questions_path(@exam)
   end