diff --git a/config.rb b/config.rb index ee527f34..a9ca0fe3 100644 --- a/config.rb +++ b/config.rb @@ -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| @@ -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 diff --git a/lib/i18n_helpers.rb b/lib/i18n_helpers.rb new file mode 100644 index 00000000..b8662fa3 --- /dev/null +++ b/lib/i18n_helpers.rb @@ -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 diff --git a/source/partials/navigation.html.erb b/source/partials/navigation.html.erb index 14e4b9ea..1c2e413b 100644 --- a/source/partials/navigation.html.erb +++ b/source/partials/navigation.html.erb @@ -1,28 +1,28 @@