Skip to content

Commit

Permalink
I18n: changing to the same page on click - fixes #27
Browse files Browse the repository at this point in the history
  • Loading branch information
andreslucena committed Oct 2, 2017
1 parent b8f2888 commit b97fe6a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 63 deletions.
40 changes: 3 additions & 37 deletions config.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'lib/i18n_helpers'
helpers I18nHelpers

# Activate multi-language
activate :i18n, :mount_at_root => :en

# Activate and configure extensions
# https://middlemanapp.com/advanced/configuration/#configuring-extensions

activate :directory_indexes

activate :autoprefixer do |prefix|
Expand All @@ -15,42 +15,8 @@
activate :livereload
end

# Layouts
# https://middlemanapp.com/basics/layouts/

# Per-page layout changes
page '/*.xml', layout: false
page '/*.json', layout: false
page '/*.txt', layout: false

# With alternative layout
# page '/path/to/file.html', layout: 'other_layout'

# Proxy pages
# https://middlemanapp.com/advanced/dynamic-pages/

# proxy(
# '/this-page-has-no-template.html',
# '/template-file.html',
# locals: {
# which_fake_page: 'Rendering a fake page with a local variable'
# },
# )

# Helpers
# Methods defined in the helpers block are available in templates
# https://middlemanapp.com/basics/helper-methods/

# helpers do
# def some_helper
# 'Helping'
# end
# end

# Build-specific configuration
# https://middlemanapp.com/advanced/configuration/#environment-specific-settings

# configure :build do
# activate :minify_css
# activate :minify_javascript
# end
25 changes: 25 additions & 0 deletions lib/i18n_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module I18nHelpers
# https://gist.github.com/johnnyshields/98a695df51b1e99f3593579d3c9a3fd1

def current_path_for_locale(loc = I18n.locale, is_link = true)
return 'javascript: void(0);' if is_link && I18n.locale == loc

url_regex = /\A\/(?:(#{I18n.available_locales.join('|')})\/)?/
current_page.url.gsub(url_regex, '').blank? ?
home_for_locale(loc) :
current_page.url.gsub(url_regex, root_for_locale(loc))
end

def path_for_locale(path, loc = I18n.locale)
root_for_locale(loc) + path
end

def home_for_locale(loc = I18n.locale)
root_for_locale(loc)
end

def root_for_locale(loc = I18n.locale)
loc == "en" ? '/' : "/#{loc}/"
end

end
52 changes: 26 additions & 26 deletions source/partials/navigation.html.erb
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<nav class="main-nav">
<div class="lang-wrapper">
<a href="#lang" class="lang-trigger"><%= t(:activeLang) %></a>
<ul id="lang" class="lang" role="menu">
<li role="menuitem">
<a href="/">English</a>
</li>
<li role="menuitem">
<a href="/es">Castellano</a>
</li>
<li role="menuitem">
<a href="/ca">Català</a>
</li>
</ul>
</div>
<div class="main-nav__links" id="nav-links">
<a href="<%= t(:path) %>/demo" class="main-nav__item"><%= t(:navDemo) %></a>
<a href="<%= t(:path) %>/community" class="main-nav__item"><%= t(:navCommunity) %></a>
<a href="<%= t(:path) %>/contract"class="main-nav__item"><%= t(:navContract) %></a>
<a href="https://github.com/decidim/decidim" target="_blank" class="main-nav__item">
<%= t(:navCode) %>
</a>
</div>
<a class="main-nav__trigger" href="#nav-links">
<span class="nav-icon"></span>
<span class="visually-hidden"><%= t(:navText) %></span>
</a>
<div class="lang-wrapper">
<a href="#lang" class="lang-trigger"><%= t(:activeLang) %></a>
<ul id="lang" class="lang" role="menu">
<li role="menuitem">
<%= link_to "English", current_path_for_locale("en") %>
</li>
<li role="menuitem">
<%= link_to "Castellano", current_path_for_locale("es") %>
</li>
<li role="menuitem">
<%= link_to "Català", current_path_for_locale("ca") %>
</li>
</ul>
</div>
<div class="main-nav__links" id="nav-links">
<a href="<%= t(:path) %>/demo" class="main-nav__item"><%= t(:navDemo) %></a>
<a href="<%= t(:path) %>/community" class="main-nav__item"><%= t(:navCommunity) %></a>
<a href="<%= t(:path) %>/contract"class="main-nav__item"><%= t(:navContract) %></a>
<a href="https://github.com/decidim/decidim" target="_blank" class="main-nav__item">
<%= t(:navCode) %>
</a>
</div>
<a class="main-nav__trigger" href="#nav-links">
<span class="nav-icon"></span>
<span class="visually-hidden"><%= t(:navText) %></span>
</a>
</nav>

0 comments on commit b97fe6a

Please sign in to comment.