I use the link_to to generate language switcher links. The Problem is that link_to do not work with “/” as path, or not always.
My frontpage url looks like so:
german: http://localhost:4567/
english: http://localhost:4567/en
If i am on the german frontpage (http://localhost:4567) the urls work like expected. The german language switcher link is “/” and the englisch one is “/en/”.
If i am on the english frontpage (http://localhost:4567/en) the urls don’t work. The german language switcher link is “/en/” and the english is “/en/”.
So as you can see i can’t switch back to german because it also links to english. On all other subpages the language switcher works.
I use a little helper function to generate the localized paths.
def i18n_path(lang, path)
translated_path = t("paths.#{path}", locale: lang)
if lang.to_s == 'de'
prefix = ''
else
prefix = "/#{lang}"
end
"#{prefix}/#{translated_path}"
end
I use this function in a loop (all languages) in the layout.erb
<ul>
<% langs.each do |lang| %>
<li><%= link_to data.languages[lang], i18n_path(lang, current_page.data.path) %></li>
<% end %>
</ul>
So this basically works but as i told above, it produces the wrong path on the frontpage for the german language switcher link, if i am on the english frontpage. If i create the link manually everything works, so there must be something wrong with the link_to.
This works
<ul>
<% langs.each do |lang| %>
<li>
<a href="<%= i18n_path(lang, current_page.data.path) %>"><%= data.languages[lang] %></a>
</li>
<% end %>
</ul>
Anyone an idea whats wrong here? The i18n_path function returns a “/” as path, but after link_to it is /en/.