[英]rails-breadcrumb and I18n localization
I'm using BreadcrumbsOnRails in my application. 我在我的应用程序中使用BreadcrumbsOnRails 。 Application is fully localized, but I have one problem with navigation translating. 应用程序已完全本地化,但是导航翻译存在一个问题。 When menu is rendered, the localized name is always on russian (an even I18n.locale='en') 呈现菜单时,本地化名称始终为俄语(甚至是I18n.locale ='en')
set up locale in application controller : 在应用程序控制器中设置语言环境:
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
I have next controller with breadcrumb: 我有面包屑的下一个控制器 :
class ExamsController < ApplicationController
...
add_breadcrumb I18n.t('navigation.exams'), :exams_path
before_filter :exam_breadcrumb, :only => [:show, :edit]
...
end
#config/lib/breadcrumbs_builder #配置/ lib目录/ breadcrumbs_builder
haml view with breadcrumbs: 带有面包屑的haml 视图 :
- breadcrumbs = render_breadcrumbs(:separator => ' / ', :tag => :li, :builder => BreadcrumbsBuilder)
- unless breadcrumbs.blank?
.subnav#main-subnav
.container
%ul.breadcrumbs
= breadcrumbs
With this hardcode everything is OK: 有了这个硬代码,一切都OK:
add_breadcrumb I18n::t('navigation.exams', locale: 'en'), :exams_path
Had to add breadcrumbs in separate method. 必须在单独的方法中添加面包屑。 Other solution I have not found... 我还没有找到其他解决方案...
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.