Redirect same page to different language with Globalize & Friendly_id

做~自己de王妃 提交于 2019-12-05 04:16:55

I believe you have url_for in your global layout, for different controllers. In that case, you have to set certain record in each controller (you can make helper or move everything into a router/middleware level). url_for(page) must be run in I18n.with_locale and might be require https://github.com/norman/friendly_id-globalize.

Some more info can be found here: https://github.com/norman/friendly_id-globalize/issues/7 and http://www.cantierecreativo.net/blog/2015/02/10/alternate-sitemap/ (in some language, but examples may help you to understand the whole concept).

TL;DR

# app/views/layouts/application.html.slim
ul.switch_locale
  - I18n.available_locales.each do |locale|
    li= I18n.with_locale(locale) do
      - url = yield(:current_page_url) || url_for(locale: locale)
      = link_to "Switch to #{locale}", url

# app/views/posts/show.html.slim
- content_for(:current_page_url) { post_url(@post) }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!