Rails 4 / i18n /无法将语言环境传递到另一页

时间:2018-09-16 08:12:40

标签: ruby-on-rails-4 internationalization locale rails-i18n

我正在制作一个带有主页和联系表的简单网站。现在,我尝试使用rails-i18n gem将其转换为3种语言,并且在主页上成功实现了这一点,但是现在,我对如何将语言环境从主页传递到联系页面感到困惑。代码如下,如果您有任何线索,请告诉我。

routes.rb(在范围内删除2个资源没有区别)

Rails.application.routes.draw do

  get 'contact' => 'inquiry#contact'
  post 'contact/confirm' => 'inquiry#confirm'
  post 'contact/thanks' => 'inquiry#thanks'

  root 'static_pages#home'
  match '/home', to: 'static_pages#home', via: 'get'

  scope '(:locale)', locale: /#{I18n.available_locales.map(&:to_s).join('|')}/ do
    resources :static_pages
    resources :inquiry
  end

end

application_controller.rb(删除default_url_options没有区别)

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :set_locale

  def set_locale
    I18n.locale = locale
  end

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

  def default_url_options(options={})
    options.merge(locale: locale)
  end
end

inquiry_controller.rb(调用联系人时,语言环境变为:en,这是默认设置)

class InquiryController < ApplicationController
  def contact
    @inquiry = Inquiry.new
    render :action => 'contact'
  end

  def confirm
    @inquiry = Inquiry.new(params[:inquiry])
    if @inquiry.valid?
      render :action => 'confirm'
    else
      render :action => 'contact'
    end
  end

  def thanks
    @inquiry = Inquiry.new(params[:inquiry])
    InquiryMailer.received_email(@inquiry).deliver

    render :action => 'thanks'
  end
end

_dropdown.html.erb(用于更改我在主页中添加的语言的下拉菜单。未在联系页面中添加)

<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Languages <span class="caret"></span></a>
  <ul class="dropdown-menu">
    <li><%= link_to '英語/English', url_for(controller: controller.controller_name, action: controller.action_name, locale: 'en') %></li>
    <li><%= link_to '中文/Mandarin', url_for(controller: controller.controller_name, action: controller.action_name, locale: 'ma') %></li>
    <li><%= link_to '日本語/Japanese', url_for(controller: controller.controller_name, action: controller.action_name, locale: 'ja') %></li>
  </ul>
</li>

控制台(我觉得有问题...)

$ rake routes
          Prefix Verb   URI Pattern                                Controller#Action
         contact GET    /contact(.:format)                         inquiry#contact
 contact_confirm POST   /contact/confirm(.:format)                 inquiry#confirm
  contact_thanks POST   /contact/thanks(.:format)                  inquiry#thanks
            root GET    /                                          static_pages#home
            home GET    /home(.:format)                            static_pages#home
    static_pages GET    (/:locale)/static_pages(.:format)          static_pages#index {:locale=>/en|ma|ja/}
                 POST   (/:locale)/static_pages(.:format)          static_pages#create {:locale=>/en|ma|ja/}
 new_static_page GET    (/:locale)/static_pages/new(.:format)      static_pages#new {:locale=>/en|ma|ja/}
edit_static_page GET    (/:locale)/static_pages/:id/edit(.:format) static_pages#edit {:locale=>/en|ma|ja/}
     static_page GET    (/:locale)/static_pages/:id(.:format)      static_pages#show {:locale=>/en|ma|ja/}
                 PATCH  (/:locale)/static_pages/:id(.:format)      static_pages#update {:locale=>/en|ma|ja/}
                 PUT    (/:locale)/static_pages/:id(.:format)      static_pages#update {:locale=>/en|ma|ja/}
                 DELETE (/:locale)/static_pages/:id(.:format)      static_pages#destroy {:locale=>/en|ma|ja/}
   inquiry_index GET    (/:locale)/inquiry(.:format)               inquiry#index {:locale=>/en|ma|ja/}
                 POST   (/:locale)/inquiry(.:format)               inquiry#create {:locale=>/en|ma|ja/}
     new_inquiry GET    (/:locale)/inquiry/new(.:format)           inquiry#new {:locale=>/en|ma|ja/}
    edit_inquiry GET    (/:locale)/inquiry/:id/edit(.:format)      inquiry#edit {:locale=>/en|ma|ja/}
         inquiry GET    (/:locale)/inquiry/:id(.:format)           inquiry#show {:locale=>/en|ma|ja/}
                 PATCH  (/:locale)/inquiry/:id(.:format)           inquiry#update {:locale=>/en|ma|ja/}
                 PUT    (/:locale)/inquiry/:id(.:format)           inquiry#update {:locale=>/en|ma|ja/}
                 DELETE (/:locale)/inquiry/:id(.:format)           inquiry#destroy {:locale=>/en|ma|ja/}

根据我提到的用于设置所有这些内容的博客页面,routes.rb中的作用域对象应该启用/ en或/ ja等的访问,它会给出路由错误。我觉得我没有把事情做好……任何小建议都将不胜感激!

1 个答案:

答案 0 :(得分:0)

通过将/ contact的链接从href =“ / contact”更改为<%= link_to t('view.contact'),url_for(controller::inquiry,action::contact)%>

来解决