2
我在我的路由文件的代碼,所以我需要得到帕拉姆「:語言環境」的範圍我可以從範圍訪問一個參數嗎?
scope '/:locale', :locale => /it|en|es/ do
# How I can access value of :locale here ?
end
我在我的路由文件的代碼,所以我需要得到帕拉姆「:語言環境」的範圍我可以從範圍訪問一個參數嗎?
scope '/:locale', :locale => /it|en|es/ do
# How I can access value of :locale here ?
end
This博客解釋得很好,你正在努力實現的目標。
這個想法是,你可以使用I18n.available_locales
和I18n.default_locale
做你想要的一切。
# resources
scope "/:locale", locale: /#{I18n.available_locales.join("|")}/ do
resources :posts
end
# default url
root to: redirect("/#{I18n.default_locale}", status: 302), as: :redirected_root
# constraint to redirect when no valid locale is specified
get "/*path", to: redirect("/#{I18n.default_locale}/%{path}", status: 302),
constraints: {path: /(?!(#{I18n.available_locales.join("|")})\/).*/}, format: false
並回答你的問題,這裏是你如何使用當前的區域
scope "/:locale", locale: /#{I18n.available_locales.join("|")}/ do
root to: redirect("/%{locale}/posts", status: 302)
end