Ruby on Rails切换区域设置

时间:2014-05-08 06:04:20

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.2

我的应用程序中的语言环境切换有一个非常奇怪的问题。我编写了简单的区域设置切换器,用于在会话中存储区域设置。它看起来像这样:

class LocalesController < ApplicationController 
  skip_authorization_check

  def switch
    session[:locale] = params[:locale]               
    redirect_to_back_or_default
  end

  private
  def redirect_to_back_or_default(default = partners_root_path)
    if request.env["HTTP_REFERER"].present? and request.env["HTTP_REFERER"] != request.env["REQUEST_URI"]
      redirect_to :back
    else
      redirect_to default
    end
  end
end

在application_controller.rb中:

  before_filter :set_locale 
  def set_locale
    I18n.locale =  session[:locale] || I18n.default_locale
  end

在我的路线的顶部;

get 'locales/:locale', to: "locales#switch", as: :locales

在我的application.rb中我有:

#config.i18n.default_locale = 'pl' I must comment this line to switcher works..
I18n.locale = 'pl'

以及我对语言的更改链接:

 %a.lang-pl{href: locales_path('pl')}
 %a.lang-en{href: locales_path('en')}

此切换台可以正常工作但不是百分之百。当我切换语言时,我的大部分面包屑都无法正常工作。我将语言改为波兰语,我的应用程序正在寻找英文文件中的面包屑翻译......这很奇怪,我无法解决这个问题。例如,ReservationsController中的面包屑看起来像这样:

add_breadcrumb ->(controller){controller.current_partner.rental_company.name}, :partners_root_path
add_breadcrumb I18n.t('reservations.reservations'), :planned_partners_reservations_path
add_breadcrumb t('reservations.reservation.list_planned'

为了创建面包屑,我使用了breadcrumbs_on_rails gem。当我切换到波兰语langugage时,我附上截图是错误的(第一个和最后一个面包屑是好的,但第二个不是)。请帮忙。

enter image description here

编辑: 我发现了问题:

I18n.t('reservations.reservations')

这个I18n.t存在问题。当我将其更改为: t('reservations.reservations')并将其替换为索引操作,就像魅力一样。 现在的问题是如何将I18n更改为其他内容?

编辑: 我的控制器看起来像这样:

 class EmployeesController < ApplicationController
   add_breadcrumb ->(controller){controller.current_partner.rental_company.name}, :partners_root_path
    add_breadcrumb I18n.t('partners.employees.home'), :partners_employees_path

    def index
      add_breadcrumb t('partners.employees.list')
      @employees = @employees.order("partners.last_name ASC")
    end
end

我的面包屑结构。这个I18n.t breadcrubms引起了这个问题。当我将I18n.t更改为:t并将其置于索引操作中时,一切都很好。现在的问题是如何使用这个I18n作品来处理面包屑......

1 个答案:

答案 0 :(得分:0)

t帮助器只是I18n.translate方法的别名,由Rails&#39;提供。控制器和视图中的ActionPack。在模型中,您仍然需要使用完整的东西:I18n.t

除此之外,使用哪种变体无关紧要。如果它有所不同,则意味着某些东西覆盖了t帮助器。

translation_missing:en.reservations.reservations表示一切都按预期工作,但您没有为此区域设置(en)中的特定密钥提供实际翻译。为此,您需要转到config/locales/en.yml并将其编辑为:

en:
  reservations:
    reservations: Your translation here..

我建议您再次浏览Rails I18n Guides

相关问题