i18n路由到挂载引擎

我有一个应用程序(my_test_app),它支持构建的i18n工作支持。 目前,有两种语言文件可用,即FR&EN,如果我在它们之间来回切换,一切都会正常工作,因为我期望看到它用于非引擎功能,例如用户索引/显示/编辑/删除(ISED)选项。

在my_test_app中,我有一个装有控制器和模型集(engine_job)的Rails引擎(my_engine)。 所以,一个可行的URL应该是

http://0.0.0.0:3000/fr/my_engine/engine_job

不管我选择哪种语言,它总是出现在EN中。 检查参数显示:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
locale: fr
action: index
controller: my_engine/engine_job

然而,所选择的翻译是EN。

my_test_app route.rb:

MyTestApp::Application.routes.draw do

  scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
    mount MyEngine::Engine, at: "/my_engine"
  end # scope locale

  match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" and !req.path == "/#{I18n.default_locale}/"}
  match '', to: redirect("/#{I18n.default_locale}/")
end

my_engine route.rb:

MyEngine::Engine.routes.draw do
  resources :my_jobs
end

耙路线:

my_engine        (/:locale)/my_engine           MyEngine::Engine {:locale=>/en|fr/}
                             /*path(.:format)                    :controller#:action
                             /                                   :controller#:action
                users GET    (/:locale)/users(.:format)          users#index {:locale=>/en|fr/}
                      POST   (/:locale)/users(.:format)          users#create {:locale=>/en|fr/}
             new_user GET    (/:locale)/users/new(.:format)      users#new {:locale=>/en|fr/}
            edit_user GET    (/:locale)/users/:id/edit(.:format) users#edit {:locale=>/en|fr/}
                 user GET    (/:locale)/users/:id(.:format)      users#show {:locale=>/en|fr/}
                      PUT    (/:locale)/users/:id(.:format)      users#update {:locale=>/en|fr/}
                      DELETE (/:locale)/users/:id(.:format)      users#destroy {:locale=>/en|fr/}
             sessions POST   (/:locale)/sessions(.:format)       sessions#create {:locale=>/en|fr/}
          new_session GET    (/:locale)/sessions/new(.:format)   sessions#new {:locale=>/en|fr/}
              session DELETE (/:locale)/sessions/:id(.:format)   sessions#destroy {:locale=>/en|fr/}
               signup        (/:locale)/signup(.:format)         users#new {:locale=>/en|fr/}
               signin        (/:locale)/signin(.:format)         sessions#new {:locale=>/en|fr/}
              signout DELETE (/:locale)/signout(.:format)        sessions#destroy {:locale=>/en|fr/}
                 help        (/:locale)/help(.:format)           static_pages#help {:locale=>/en|fr/}
                about        (/:locale)/about(.:format)          static_pages#about {:locale=>/en|fr/}
                 root        /(:locale)(.:format)                static_pages#home {:locale=>/en|fr/}

Routes for MyEngine::Engine {:locale=>/en|fr/}:
    engine_jobs GET    /engine_jobs(.:format)          my_engine/engine_jobs#index
                POST   /engine_jobs(.:format)          my_engine/engine_jobs#create
 new_engine_job GET    /engine_jobs/new(.:format)      my_engine/engine_jobs#new
edit_engine_job GET    /engine_jobs/:id/edit(.:format) my_engine/engine_jobs#edit
     engine_job GET    /engine_jobs/:id(.:format)      my_engine/engine_jobs#show
                PUT    /engine_jobs/:id(.:format)      my_engine/engine_jobs#update
                DELETE /engine_jobs/:id(.:format)      my_engine/engine_jobs#destroy

此外,引擎中单击的任何链接都将语言设置为EN。 一些网页搜索没有真正了解这里可能发生的情况,因为我能找到的所有i18n例子都不涉及安装的引擎。

编辑:显示代码来设置默认语言环境my_test_app / app / controllers / application_controller.rb:

class ApplicationController < ActionController::Base
  protect_from_forgery
  helper MyEngine::Engine.helpers

  ActiveRecord::Base.verify_active_connections!

  before_filter :set_locale

  private
    def set_locale
      I18n.locale = params[:locale] || I18n.default_locale
      Rails.application.routes.default_url_options[:locale]= I18n.locale
      logger.debug "My_Test_App:  default_url_options is passed options: #{Rails.application.routes.default_url_options.inspect}n"
      # current_user.locale
      # request.subdomain
      # request.env["HTTP_ACCEPT_LANGUAGE"]
      # request.remote_ip
    end # set_locale

end # class ApplicationController

/编辑

EDIT2(路线图在皮埃尔8月9日15:59建议更改后):

应用程序路径使用"locale"=>"en"命令http://0.0.0.0:3000/en/users 。 助手创建的引擎路由看起来像http://0.0.0.0:3000/my_engine?locale=en/engine_jobs和yeilds No route matches [GET] "/my_engine"

my_engine_plugin        /my_engine                     MyEnginePlugin::Engine
                users GET    (/:locale)/users(.:format)          users#index {:locale=>/en|fr/}
                      POST   (/:locale)/users(.:format)          users#create {:locale=>/en|fr/}
             new_user GET    (/:locale)/users/new(.:format)      users#new {:locale=>/en|fr/}
            edit_user GET    (/:locale)/users/:id/edit(.:format) users#edit {:locale=>/en|fr/}
                 user GET    (/:locale)/users/:id(.:format)      users#show {:locale=>/en|fr/}
                      PUT    (/:locale)/users/:id(.:format)      users#update {:locale=>/en|fr/}
                      DELETE (/:locale)/users/:id(.:format)      users#destroy {:locale=>/en|fr/}
             sessions POST   (/:locale)/sessions(.:format)       sessions#create {:locale=>/en|fr/}
          new_session GET    (/:locale)/sessions/new(.:format)   sessions#new {:locale=>/en|fr/}
              session DELETE (/:locale)/sessions/:id(.:format)   sessions#destroy {:locale=>/en|fr/}
               signup        (/:locale)/signup(.:format)         users#new {:locale=>/en|fr/}
               signin        (/:locale)/signin(.:format)         sessions#new {:locale=>/en|fr/}
              signout DELETE (/:locale)/signout(.:format)        sessions#destroy {:locale=>/en|fr/}
                 help        (/:locale)/help(.:format)           static_pages#help {:locale=>/en|fr/}
                about        (/:locale)/about(.:format)          static_pages#about {:locale=>/en|fr/}
                 root        /(:locale)(.:format)                static_pages#home {:locale=>/en|fr/}

Routes for MyEnginePlugin::Engine:
    engine_jobs GET    (/:locale)/engine_jobs(.:format)          my_engine_plugin/engine_jobs#index {:locale=>/en|fr/}
                     POST   (/:locale)/engine_jobs(.:format)          my_engine_plugin/engine_jobs#create {:locale=>/en|fr/}
 new_engine_job GET    (/:locale)/engine_jobs/new(.:format)      my_engine_plugin/engine_jobs#new {:locale=>/en|fr/}
edit_engine_job GET    (/:locale)/engine_jobs/:id/edit(.:format) my_engine_plugin/engine_jobs#edit {:locale=>/en|fr/}
     engine_job GET    (/:locale)/engine_jobs/:id(.:format)      my_engine_plugin/engine_jobs#show {:locale=>/en|fr/}
                     PUT    (/:locale)/engine_jobs/:id(.:format)      my_engine_plugin/engine_jobs#update {:locale=>/en|fr/}
                     DELETE (/:locale)/engine_jobs/:id(.:format)      my_engine_plugin/engine_jobs#destroy {:locale=>/en|fr/}

/ EDIT2(皮埃尔8月9日15:59建议更改之后的路线图)

所以,问题是我需要对我的路线或引擎进行哪些更改才能使其按预期工作?

预先感谢您的时间和建议!


我使用I18n引擎,它工作正常。 我创建了一个虚拟Rails应用程序来尝试你的场景。 据我所知,更改URL内的语言环境可以很好地处理Rails应用程序中定义的路由:

我的路线:

Bar::Application.routes.draw do
  root 'posts#index'

  scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
    resources :posts, only: :index
  end
end

我可以通过以下方式更改I18n语言环境:

http://localhost:3000/fr/posts
http://localhost:3000/en/posts

我认为你的问题是当你想要去任何引擎的路由,因为你没有设置I18n区域设置开关。 见下图:

engine_jobs GET    /engine_jobs(.:format)

然后,即使您在URL中指定了区域设置,当转到/engine_jobs时,它也会设置为( en )的I18n默认语言环境:

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

当使用你的引擎路由时, params[:locale]nil

将相同的逻辑添加到引擎的路线中:

config/routes.rb

MyTestApp::Application.routes.draw do

  mount MyEngine::Engine, at: "/my_engine"

  match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" and !req.path == "/#{I18n.default_locale}/"}
  match '', to: redirect("/#{I18n.default_locale}/")
end

your_engine/config/routes.rb

MyEngine::Engine.routes.draw do
  scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
    resources :my_jobs
  end
end

mount MyEngine::Engine, at: "/my_engine"仅告诉导轨“加载”所有引擎路径。 如果你需要添加约束,范围,命名空间或其他任何东西,你应该使用通常的rails方式来完成它,但是在你的引擎的路由文件中。

最后,您需要使用以下命令更新两个application_controller.rb (主应用程序+引擎):

class ApplicationController < ActionController::Base
  def url_options
    { locale: I18n.locale }
  end

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
    Rails.application.routes.default_url_options[:locale]= I18n.locale
  end
end
链接地址: http://www.djcxy.com/p/73265.html

上一篇: i18n Routing To Mounted Engine

下一篇: SERVER['REMOTE