Middleman check if page exists

262 views Asked by At

I have a language menu such as this (it switches the current page to another language):

- path = current_page.path.split('/')[1..-1].join('/')
%ul{ role: 'langmenu'}
  -if I18n.locale != :en
    %li
      =link_to 'English', "/en/#{path}"
  -if I18n.locale != :sr
    %li
      =link_to 'Serbian', "/sr/#{path}"

I want to show the link to the locale only if the destination page actually exists since not all pages will have translation. I tried with sitemap.find_resource_by_path("/sr/#{path}") but it always returns false even when it exists. What am I missing ?

1

There are 1 answers

0
skoji On

The argument of sitemap.find_resource_by_path is a resource path, not destination url path. So you must do something like this :

resource_path = current_resource.path.split('/')[1..-1].join('/')
sr_resource_path = sitemap.find_resource_by_path "sr/#{resource_path}"
if sr_resource_path
  return sr_resource_path.destination_path
end