Skip to content

Instantly share code, notes, and snippets.

@xeraa
Created August 4, 2015 23:48
Show Gist options
  • Save xeraa/c30161bac25011a03058 to your computer and use it in GitHub Desktop.
Save xeraa/c30161bac25011a03058 to your computer and use it in GitHub Desktop.
Middleman: Fully automated alternate language links
# Returns a hash of localized paths for a given page
def localized_paths_for(page)
localized_paths = {}
(langs).each do |locale|
# Loop over all pages to find the ones using the same templates (proxied_to) for each language
sitemap.resources.select do |resource|
if resource.proxied_to == page.proxied_to && resource.metadata[:options][:lang] == locale
localized_paths[locale] = resource.url
break
end
end
end
localized_paths
end
<% localized_paths_for(current_page).each do |text, path| %>
<link rel="alternate" hreflang="<%= text %>" href="https://example.com<%= path %>"/>
<% end %>
@xeraa
Copy link
Author

xeraa commented Aug 4, 2015

This assumes you are sharing templates across languages and supports localized paths.

Copy link

ghost commented Dec 20, 2017

@xeraa when I try to run my Middleman localhost site with it I am getting:

NoMethodError at /
undefined method `proxied_to' for #<Middleman::Sitemap::Resource path=tos.html>

Do I need to specify something in frontmatter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment