【问题标题】:Starting rails I18n and url helpers seem to confuse locale with id启动 rails I18n 和 url helpers 似乎将 locale 与 id 混淆了
【发布时间】:2014-06-08 04:37:34
【问题描述】:

我刚刚开始尝试更新我们的 rails 3.2 应用程序以实现国际化。我在路线中有一个可选范围,例如

范围“(:locale)”,语言环境:/en|es|zh-HK|de|fr/do

http://guides.rubyonrails.org/i18n.html 所述。

我的网址助手喜欢

watch_url(@watch)

在表单中获取错误

没有路线匹配 {:action=>"show", :controller=>"watches", :locale=>#"", "recipients"=>"", "attributes"=>""}, state: {}, hidden: false, comment: "">}

关于它为什么会发生以及如何解决它的任何想法?

谢谢!

编辑:

在我的应用程序控制器中,我有

  before_filter :set_locale

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

还有用于手表控制器的路由(这是一个已有 8 年历史的专有应用,我们有 100 多个控制器 :(,所以我不会包括所有路由)...

$ rake routes | grep watch
                              watches GET    (/:locale)/watches(.:format)                                                         watches#index  {:locale=>/en|es|zh-HK|de|fr/}
                                      POST   (/:locale)/watches(.:format)                                                         watches#create {:locale=>/en|es|zh-HK|de|fr/}
                            new_watch GET    (/:locale)/watches/new(.:format)                                                     watches#new {:locale=>/en|es|zh-HK|de|fr/}
                           edit_watch GET    (/:locale)/watches/:id/edit(.:format)                                                watches#edit {:locale=>/en|es|zh-HK|de|fr/}
                                watch GET    (/:locale)/watches/:id(.:format)                                                     watches#show {:locale=>/en|es|zh-HK|de|fr/}
                                      PUT    (/:locale)/watches/:id(.:format)                                                     watches#update {:locale=>/en|es|zh-HK|de|fr/}
                                      DELETE (/:locale)/watches/:id(.:format)                                                     watches#destroy {:locale=>/en|es|zh-HK|de|fr/}

我不认为 show 方法是相关的。错误发生在 url 帮助程序中,并且流程永远不会到达显示代码。

【问题讨论】:

  • 请发布您的rake routes 输出和controllershow 方法。
  • 你在 application.rb 中定义了你的 default_locale 吗?

标签: ruby-on-rails ruby-on-rails-3 internationalization rails-i18n


【解决方案1】:

You need to give the url helper a locale option 以便正确通过:

watch_url(@watch, locale: I18n.locale)

如果这有点烦人,您可以重写 Rails url_options 方法,始终为每个请求提供一个 :locale 选项,这样您就不必在任何视图中指定它(一些指南,包括 Rails 指南上面的链接,说要覆盖default_url_options 方法,但这对我不起作用...):

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  # Every helper method dependent on url_for (e.g. helpers for named
  # routes like root_path or root_url, resource routes like books_path
  # or books_url, etc.) will now automatically include the locale in
  # the query string,
  def url_options
    { locale: I18n.locale }.merge(super)
  end

  # ...
end

app/views/your_view

# locale not needed explicitly as it's set in the controller
<%= link_to watch_url(@watch) %>

【讨论】:

  • 是的,这确实解决了这个问题。但是,引起了许多其他人...从本质上讲,它使可选的本地规范成为强制性的,我认为这是解决问题的间接必要条件。我想我还有一些工作要做!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 2014-06-12
  • 2021-04-26
相关资源
最近更新 更多