Skip to content

Instantly share code, notes, and snippets.

@j15e
Created September 12, 2012 15:15
Show Gist options
  • Save j15e/3707291 to your computer and use it in GitHub Desktop.
Save j15e/3707291 to your computer and use it in GitHub Desktop.
Spree language switch
<%
# app/views/spree/shared/_lang_choice.html.erb
# Deface + I18n.with_locale do not play well together, implementing it manually
saved_locale = I18n.locale
I18n.locale = I18n.locale == :fr ? :en : :fr
%>
<li class="lang-swich"><%= lang_switch_url %></li>
<% I18n.locale = saved_locale %>
# app/helpers/spree/base_help_decorator.rb
Spree::BaseHelper.module_eval do
def lang_switch_url
url = if params[:controller] == 'spree/products' && params[:action] == 'show'
{ :id => @product.permalink }
elsif params[:controller] == 'spree/taxons' && params[:action] == 'show'
{ :id => @taxon.permalink }
else
{ }
end
link_to I18n.t("layout.#{I18n.locale}"), url
end
end
# app/overrides/lang_menu.rb
Deface::Override.new(:virtual_path => "spree/layouts/spree_application",
:insert_bottom => "#nav-bar",
:partial => "spree/shared/lang_choice",
:name => "lang_menu")
@mhulet
Copy link

mhulet commented Sep 13, 2012

Thanks for sharing! Successfully integrated on a Spree 1.2.0 project.

Used the Deface DSL in place of Deface::Override:

# overrides/spree/shared/_nav_bar/lang_menu.html.erb.deface
<!-- insert_bottom "#nav-bar" -->
<%= render "spree/shared/lang_choice" %>

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