【问题标题】:Ruby on Rails switching localeRuby on Rails 切换语言环境
【发布时间】:2014-05-08 06:04:20
【问题描述】:

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

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')}

此切换器可以工作,但不是 100%。当我切换语言时,我的大部分面包屑都不起作用。我将语言更改为波兰语,我的应用正在寻找英文文件中的面包屑翻译......这很奇怪,我无法解决这个问题。例如,我在 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。我附上屏幕截图当我切换到波兰语时出了什么问题(第一个和最后一个面包屑是好的,但第二个不是)。请帮忙。

编辑: 我发现问题原因:

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 面包屑会导致问题。当我将 I18n.t 更改为:t 并将其置于索引操作中时,一切都很好。现在的问题是如何用这个 I18n 作品来处理面包屑......

【问题讨论】:

  • 显示您的 pl.yml 语言环境文件。
  • 谢谢回复,我已经更新了帖子。问题在于控制器中的这个 I18n.t。也许你知道这个奇怪的东西的解决方案?
  • 很高兴你明白了,但现在你的意思是——“现在的问题是我怎样才能把这个 I18n 改成别的东西?” 1. 你说的其他是什么意思 2. 代码中有几个 i18n 你要改哪个,为什么?
  • 我已经更新了帖子。看看我的控制器是什么样子,也许你明白我的问题。

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


【解决方案1】:

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

除此之外,您使用哪种变体并不重要。如果它有所作为,则意味着某些东西正在覆盖 t 帮助器。

translation_missing:en.reservations.reservations 表示一切都按预期工作,但您尚未提供此语言环境中特定键的实际翻译 (en)。为此,您需要转到 config/locales/en.yml 并将其编辑为:

en:
  reservations:
    reservations: Your translation here..

我建议你再去一次Rails I18n Guides

【讨论】:

  • 在非英语上下文中没有正确的翻译,所以您指定的结构应该在 pl.yml 文件中,而不是 en.yml 我猜。
  • 但在屏幕截图中,OP 提供的内容清楚地以波兰语列出,Rails 抱怨en.reservations...。这意味着他使用的是英语语言环境,并且他缺少相应的波兰语翻译。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多