Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Multiple language menus.

Matthew Kennedy20880d51cd2d
diff --git a/backend/app/controllers/spree/admin/menus_controller.rb b/backend/app/controllers/spree/admin/menus_controller.rb
index 394e545199..9ef6c28545 100644
--- a/backend/app/controllers/spree/admin/menus_controller.rb
+++ b/backend/app/controllers/spree/admin/menus_controller.rb
@@ -1,6 +1,8 @@
module Spree
module Admin
class MenusController < ResourceController
+ before_action :load_data
+
def index; end
private
@@ -8,6 +10,10 @@ module Spree
def location_after_save
spree.edit_admin_menu_path(@menu)
end
+
+ def load_data
+ @menu_locations = Spree::Menu::MENU_LOCATIONS_FOR_SELECT
+ end
end
end
end
diff --git a/backend/app/views/spree/admin/menus/_form.html.erb b/backend/app/views/spree/admin/menus/_form.html.erb
index bc5fc07ed8..4f2c4d4dc3 100644
--- a/backend/app/views/spree/admin/menus/_form.html.erb
+++ b/backend/app/views/spree/admin/menus/_form.html.erb
@@ -6,11 +6,10 @@
<%= f.error_message_on :name %>
<% end %>
- <%= f.field_container :unique_code, class: ['form-group'] do %>
- <%= f.label :unique_code, raw(Spree.t('admin.navigation.unique_code') + required_span_tag) %>
- <%= f.text_field :unique_code, class: 'form-control', required: true %>
- <%= f.error_message_on :unique_code %>
- <small class="form-text text-muted"><%= Spree.t('admin.navigation.give_your_menu_a_unique_name') %></small>
+ <%= f.field_container :location, class: ['form-group'] do %>
+ <%= f.label :location, Spree.t('admin.navigation.location') %>
+ <%= f.select(:location, @menu_locations, {include_blank: false}, class: 'select2') %>
+ <%= f.error_message_on :location %>
<% end %>
</div>
<div class="col-12 col-md-6">
@@ -19,5 +18,10 @@
<%= collection_select(:menu, :store_id, @stores, :id, :unique_name, {}, { class: 'select2' }) %>
<%= f.error_message_on :menu_stores %>
</div>
+ <div class="form-group">
+ <%= f.label :locale %>
+ <%= f.select :locale, options_from_collection_for_select(all_locales_options, :last, :first, @menu.locale || I18n.locale), {}, { class: 'select2' } %>
+ <%= f.error_message_on :default_locale %>
+ </div>
</div>
</div>
diff --git a/backend/app/views/spree/admin/menus/index.html.erb b/backend/app/views/spree/admin/menus/index.html.erb
index 48984b6fd1..82a236523e 100644
--- a/backend/app/views/spree/admin/menus/index.html.erb
+++ b/backend/app/views/spree/admin/menus/index.html.erb
@@ -11,8 +11,9 @@
<table class="table" id="menusTable">
<thead>
<th scope="col"><%= Spree.t(:name) %></th>
- <th scope="col"><%= Spree.t('admin.navigation.unique_code') %></th>
+ <th scope="col"><%= Spree.t('admin.navigation.location') %></th>
<th scope="col"><%= Spree.t('store') %></th>
+ <th scope="col"><%= Spree.t('language') %></th>
<th scope="col"></th>
</thead>
<tbody>
@@ -22,10 +23,13 @@
<%= menu.name %>
</td>
<td>
- <%= menu.unique_code %>
+ <%= menu.location.titleize %>
</td>
<td>
- <span class="badge badge-secondary"><%= menu.store.unique_name %></span>
+ <%= menu.store.unique_name %>
+ </td>
+ <td>
+ <%= menu.locale %>
</td>
<td class="actions" data-hook="admin_menus_index_row_actions">
<span class="d-flex justify-content-end">
diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rb
index 0db28489c9..038af94a88 100644
--- a/core/app/models/spree/menu.rb
+++ b/core/app/models/spree/menu.rb
@@ -1,26 +1,40 @@
module Spree
class Menu < Spree::Base
+ MENU_LOCATIONS = ['Header', 'Footer']
+ MENU_LOCATIONS_PARAMETERIZED = []
+ MENU_LOCATIONS_FOR_SELECT = []
+
+ MENU_LOCATIONS.each do |location|
+ parameterize_location = location.parameterize(separator: '_')
+ MENU_LOCATIONS_PARAMETERIZED << parameterize_location
+ MENU_LOCATIONS_FOR_SELECT << [location, parameterize_location]
+ end
+
has_many :menu_items, dependent: :destroy
belongs_to :store
- before_validation :paremeterize_unique_code
+ before_validation :paremeterize_location
after_create :set_root
- validates :name, :store, presence: true
- validates :unique_code, uniqueness: { scope: :store }
-
- scope :by_store, ->(store) { where store: store }
+ validates :name, :store, :locale, presence: true
+ validates :location, uniqueness: { scope: [:store, :locale] }
+ validates :location, inclusion: { in: MENU_LOCATIONS_PARAMETERIZED }
has_one :root, -> { where(parent_id: nil) }, class_name: 'Spree::MenuItem', dependent: :destroy
- def self.by_unique_code(menu_code)
- find_by(unique_code: menu_code)
+ MENU_LOCATIONS_PARAMETERIZED.each do |name|
+ define_singleton_method("for_#{name}") do |locale|
+ menu = find_by(location: name, locale: locale.to_s) || find_by(location: name)
+ if menu.present?
+ menu.root
+ end
+ end
end
private
- def paremeterize_unique_code
- self.unique_code = unique_code.parameterize
+ def paremeterize_location
+ self.location = location.parameterize(separator: '_')
end
def set_root
diff --git a/core/db/migrate/20210407200948_create_spree_menus.rb b/core/db/migrate/20210407200948_create_spree_menus.rb
index 7ec057fda0..9d82fffa74 100644
--- a/core/db/migrate/20210407200948_create_spree_menus.rb
+++ b/core/db/migrate/20210407200948_create_spree_menus.rb
@@ -2,7 +2,8 @@ class CreateSpreeMenus < ActiveRecord::Migration[6.0]
def change
create_table :spree_menus do |t|
t.column :name, :string
- t.column :unique_code, :string, null: false
+ t.column :location, :string
+ t.column :locale, :string
t.belongs_to :store
@@ -10,6 +11,7 @@ class CreateSpreeMenus < ActiveRecord::Migration[6.0]
end
add_index :spree_menus, :name
- add_index :spree_menus, :unique_code
+ add_index :spree_menus, :location
+ add_index :spree_menus, :locale
end
end
diff --git a/frontend/app/helpers/spree/navigation_helper.rb b/frontend/app/helpers/spree/navigation_helper.rb
index 2a17dc8b86..8c13c3f1f5 100644
--- a/frontend/app/helpers/spree/navigation_helper.rb
+++ b/frontend/app/helpers/spree/navigation_helper.rb
@@ -42,20 +42,6 @@ module Spree
(defined?(should_render_currency_dropdown?) && should_render_currency_dropdown?)
end
- # Find a menu by its unique_code
- # this method will only return a menu if it is
- # available for use in the current store.
- def spree_menu(unique_code)
- available_menus.by_unique_code(unique_code)
- end
-
- # Returns only the top level items for the menu by unique_code
- def spree_top_level_items_for_menu(unique_code)
- return unless spree_menu(unique_code).present?
-
- spree_menu(unique_code).root.children
- end
-
def spree_localized_item_link(item)
return if item.destination.nil?
diff --git a/frontend/app/views/spree/shared/_footer.html.erb b/frontend/app/views/spree/shared/_footer.html.erb
index 081e63e7b4..516cee613b 100644
--- a/frontend/app/views/spree/shared/_footer.html.erb
+++ b/frontend/app/views/spree/shared/_footer.html.erb
@@ -49,8 +49,8 @@
</div>
<div class="row d-none d-xl-flex border-top py-5 text-uppercase footer-spree-menu-links">
- <% if spree_top_level_items_for_menu('spree-all-footer').present? %>
- <% spree_top_level_items_for_menu('spree-all-footer').each do |parent| %>
+ <% if available_menus.for_footer(I18n.locale).present? %>
+ <% available_menus.for_header(I18n.locale).children.each do |parent| %>
<div class="col-3">
<div class="footer-spree-label">
<%= link_to parent.name, spree_localized_item_link(parent) %>
diff --git a/frontend/app/views/spree/shared/_main_nav_bar.html.erb b/frontend/app/views/spree/shared/_main_nav_bar.html.erb
index 15e566d7f1..242a69c110 100644
--- a/frontend/app/views/spree/shared/_main_nav_bar.html.erb
+++ b/frontend/app/views/spree/shared/_main_nav_bar.html.erb
@@ -1,10 +1,10 @@
-<% if spree_top_level_items_for_menu('spree-all-main').present? %>
+<% if available_menus.for_header(I18n.locale).present? %>
<% category = 'category' %>
<% promo = 'promo' %>
<div class="h-100" role="navigation" aria-label="<%= Spree.t('nav_bar.desktop') %>">
<ul class="nav h-100 main-nav-bar">
- <% spree_top_level_items_for_menu('spree-all-main').each do |parent| %>
+ <% available_menus.for_header(I18n.locale).children.each do |parent| %>
<li class="p-0 nav-item main-nav-bar-category">
<% has_dropdown = parent.children.present? %>
<% dropdown_toggle_class = has_dropdown ? "dropdown-toggle" : "" %>
diff --git a/frontend/app/views/spree/shared/_mobile_navigation.html.erb b/frontend/app/views/spree/shared/_mobile_navigation.html.erb
index a76fe0823f..17715c8ef0 100644
--- a/frontend/app/views/spree/shared/_mobile_navigation.html.erb
+++ b/frontend/app/views/spree/shared/_mobile_navigation.html.erb
@@ -1,4 +1,4 @@
-<% if spree_top_level_items_for_menu('spree-all-main').present? %>
+<% if available_menus.for_header(I18n.locale).present? %>
<div class="position-fixed text-uppercase d-xl-none mobile-navigation" role="navigation" aria-label="<%= Spree.t('nav_bar.mobile') %>">
<div class="container d-flex align-items-center justify-content-between header-spree" data-hook>
<button class="m-0 p-0" id="mobile-navigation-back-button" aria-label="<%= Spree.t('nav_bar.go_to_previous_menu') %>">
@@ -18,7 +18,7 @@
</div>
<ul class="list-unstyled position-relative h-100 mobile-navigation-list">
- <% spree_top_level_items_for_menu('spree-all-main').each do |parent| %>
+ <% available_menus.for_header(I18n.locale).children.each do |parent| %>
<li class="d-flex justify-content-between align-items-center mobile-navigation-list-item">
<%= link_to parent.name, spree_localized_item_link(parent), class: 'w-75' %>
<% if parent.children.present? %>
diff --git a/sample/db/samples/menu_items.rb b/sample/db/samples/menu_items.rb
index 24711f0b93..6ba5147f34 100644
--- a/sample/db/samples/menu_items.rb
+++ b/sample/db/samples/menu_items.rb
@@ -8,9 +8,111 @@ MENUS.each do |menu|
##############
# Root Items #
##############
+ case menu.locale
+ when 'en'
+ root_name_a = 'Women'
+ root_name_b = 'Men'
+ root_name_c = 'Sportsware'
+
+ catagories = 'Categories'
+
+ skirts = 'Skirts'
+ dresses = 'Dresses'
+ shirts_and_blouses = 'Skirts and Blouses'
+ sweaters = 'Sweaters'
+ tops_and_t_shirts = 'Tops and T-Shirts'
+ jackets_and_coats = 'Jackets and Coats'
+ shirts = 'Shirts'
+ t_shirts = 'T-Shirts'
+ tops = 'Tops'
+ sweat_shirts = 'Sweatshirts'
+ pants = 'Pants'
+
+ promo_a_name = 'New Collection'
+ promo_a_subtitle = "Summer #{Date.today.year}"
+
+ promo_b_name = 'Special Offers'
+ promo_b_subtitle = 'Get up to 30% off'
+
+ when 'fr'
+ root_name_a = 'Femmes'
+ root_name_b = 'Hommes'
+ root_name_c = 'Vêtements de sport'
+
+ catagories = 'Catégories'
+
+ skirts = 'Jupes'
+ dresses = 'Robes'
+ shirts_and_blouses = 'Jupes et chemisiers'
+ sweaters = 'Chandails'
+ tops_and_t_shirts = 'Hauts et T-shirts'
+ jackets_and_coats = 'Vestes et manteaux'
+ shirts = 'Chemises'
+ t_shirts = 'T-Shirts'
+ tops = 'Hauts'
+ sweat_shirts = 'Pulls molletonnés'
+ pants = 'Pantalon'
+
+ promo_a_name = 'Nouvelle collection'
+ promo_a_subtitle = "Été #{Date.today.year}"
+
+ promo_b_name = 'Offres spéciales'
+ promo_b_subtitle = "Obtenez jusqu'à 30% de réduction"
+
+ when 'de'
+ root_name_a = 'Frauen'
+ root_name_b = 'Männer'
+ root_name_c = 'Sportartikel'
+
+ catagories = 'Kategorien'
+
+ skirts = 'die Röcke'
+ dresses = 'Kleider'
+ shirts_and_blouses = 'Röcke und Blusen'
+ sweaters = 'Pullovers'
+ tops_and_t_shirts = 'Tops und T-Shirts'
+ jackets_and_coats = 'Jacken und Mäntel'
+ shirts = 'Hemden'
+ t_shirts = 'T-Shirts'
+ tops = 'Tops'
+ sweat_shirts = 'Sweatshirts'
+ pants = 'Hose'
+
+ promo_a_name = 'Neue Kollektion'
+ promo_a_subtitle = "Sommer #{Date.today.year}"
+
+ promo_b_name = 'Sonderangebote'
+ promo_b_subtitle = 'Erhalten Sie bis zu 30% Rabatt'
+
+ when 'es'
+ root_name_a = 'Hombres'
+ root_name_b = 'Mujeres'
+ root_name_c = 'Deportes'
+
+ catagories = 'Categorías'
+
+ skirts = 'Faldas'
+ dresses = 'Vestidos'
+ shirts_and_blouses = 'Faldas y blusas'
+ sweaters = 'Suéteres'
+ tops_and_t_shirts = 'Tops y camisetas'
+ jackets_and_coats = 'Chaquetas y abrigos'
+ shirts = 'Camisas'
+ t_shirts = 'Camisetas'
+ tops = 'Tops'
+ sweat_shirts = 'Sudaderas'
+ pants = 'Pantalones'
+
+ promo_a_name = 'Nueva colección'
+ promo_a_subtitle = "Verano #{Date.today.year}"
+
+ promo_b_name = 'Ofertas especiales'
+ promo_b_subtitle = 'Obtenga hasta un 30% de descuento'
+ end
+
woman_taxon = Spree::Taxon.find_by!(permalink: 'women')
menu_root_women = Spree::MenuItem.where(
- name: 'Women',
+ name: root_name_a,
item_type: 'Link',
linked_resource_type: 'Spree::Taxon',
menu_id: menu,
@@ -21,7 +123,7 @@ MENUS.each do |menu|
men_taxon = Spree::Taxon.find_by!(permalink: 'men')
menu_root_men = Spree::MenuItem.where(
- name: 'Men',
+ name: root_name_b,
item_type: 'Link',
linked_resource_type: 'Spree::Taxon',
menu_id: menu,
@@ -32,7 +134,7 @@ MENUS.each do |menu|
sw_taxon = Spree::Taxon.find_by!(permalink: 'sportswear')
menu_root_sw = Spree::MenuItem.where(
- name: 'Sportsware',
+ name: root_name_c,
item_type: 'Link',
linked_resource_type: 'Spree::Taxon',
menu_id: menu,
@@ -45,7 +147,7 @@ MENUS.each do |menu|
# Categories #
##############
menu_cat_women = Spree::MenuItem.where(
- name: 'Categories',
+ name: catagories,
item_type: 'Container',
code: 'category',
menu_id: menu,
@@ -53,7 +155,7 @@ MENUS.each do |menu|
).first_or_create!
menu_cat_men = Spree::MenuItem.where(
- name: 'Categories',
+ name: catagories,
item_type: 'Container',
code: 'category',
menu_id: menu,
@@ -61,7 +163,7 @@ MENUS.each do |menu|
).first_or_create!
menu_cat_sw = Spree::MenuItem.where(
- name: 'Categories',
+ name: catagories,
item_type: 'Container',
code: 'category',
menu_id: menu,
@@ -71,7 +173,7 @@ MENUS.each do |menu|
##############
# Promotions #
##############
- if menu.unique_code == 'spree-all-main'
+ if menu.location == 'header'
menu_promo_women = Spree::MenuItem.where(
name: 'Promos',
item_type: 'Container',
@@ -110,8 +212,8 @@ MENUS.each do |menu|
promos.each do |promo|
summer_promo = Spree::MenuItem.where(
- name: 'New Collection',
- subtitle: "Summer #{Date.today.year}",
+ name: promo_a_name,
+ subtitle: promo_a_subtitle,
linked_resource_type: 'Spree::Taxon',
item_type: 'Promotion',
menu_id: menu,
@@ -121,8 +223,8 @@ MENUS.each do |menu|
summer_promo.save!
special_offer = Spree::MenuItem.where(
- name: 'Special Offers',
- subtitle: 'Get up to 30% OFF',
+ name: promo_b_name,
+ subtitle: promo_b_subtitle,
linked_resource_type: 'Spree::Taxon',
item_type: 'Promotion',
menu_id: menu,
@@ -137,7 +239,7 @@ MENUS.each do |menu|
#################################
women_skirts_t = Spree::Taxon.find_by!(permalink: 'women/skirts')
women_skirts = Spree::MenuItem.where(
- name: 'Skirts',
+ name: skirts,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -148,7 +250,7 @@ MENUS.each do |menu|
women_dresses_t = Spree::Taxon.find_by!(permalink: 'women/dresses')
women_dresses = Spree::MenuItem.where(
- name: 'Dresses',
+ name: dresses,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -159,7 +261,7 @@ MENUS.each do |menu|
women_s_b_t = Spree::Taxon.find_by!(permalink: 'women/shirts-and-blouses')
women_s_b = Spree::MenuItem.where(
- name: 'Shirts And Blouses',
+ name: shirts_and_blouses,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -170,7 +272,7 @@ MENUS.each do |menu|
women_sweaters_t = Spree::Taxon.find_by!(permalink: 'women/sweaters')
women_sweaters = Spree::MenuItem.where(
- name: 'Sweaters',
+ name: sweaters,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -181,7 +283,7 @@ MENUS.each do |menu|
women_tops_tees_t = Spree::Taxon.find_by!(permalink: 'women/tops-and-t-shirts')
women_tops_tees = Spree::MenuItem.where(
- name: 'Tops and T-Shirts',
+ name: tops_and_t_shirts,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -192,7 +294,7 @@ MENUS.each do |menu|
women_j_c_t = Spree::Taxon.find_by!(permalink: 'women/jackets-and-coats')
women_j_c = Spree::MenuItem.where(
- name: 'Jackets and Coats',
+ name: jackets_and_coats,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -206,7 +308,7 @@ MENUS.each do |menu|
###############################
men_shirts_t = Spree::Taxon.find_by!(permalink: 'men/shirts')
men_shirts = Spree::MenuItem.where(
- name: 'Shirts',
+ name: shirts,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -217,7 +319,7 @@ MENUS.each do |menu|
men_t_shirts_t = Spree::Taxon.find_by!(permalink: 'men/t-shirts')
men_t_shirts = Spree::MenuItem.where(
- name: 'T-Shirts',
+ name: t_shirts,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -228,7 +330,7 @@ MENUS.each do |menu|
men_sweaters_t = Spree::Taxon.find_by!(permalink: 'men/sweaters')
men_sweaters = Spree::MenuItem.where(
- name: 'Sweaters',
+ name: sweaters,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -239,7 +341,7 @@ MENUS.each do |menu|
men_j_c_t = Spree::Taxon.find_by!(permalink: 'men/jackets-and-coats')
men_j_c = Spree::MenuItem.where(
- name: 'Jackets and Coats',
+ name: jackets_and_coats,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -253,7 +355,7 @@ MENUS.each do |menu|
######################################
sw_tops_t = Spree::Taxon.find_by!(permalink: 'sportswear/tops')
sw_tops = Spree::MenuItem.where(
- name: 'Tops',
+ name: tops,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -264,7 +366,7 @@ MENUS.each do |menu|
sw_sweatshirts_t = Spree::Taxon.find_by!(permalink: 'sportswear/sweatshirts')
sw_sweatshirts = Spree::MenuItem.where(
- name: 'Sweatshirts',
+ name: sweat_shirts,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
@@ -275,7 +377,7 @@ MENUS.each do |menu|
sw_pants_t = Spree::Taxon.find_by!(permalink: 'sportswear/pants')
sw_pants = Spree::MenuItem.where(
- name: 'Pants',
+ name: pants,
linked_resource_type: 'Spree::Taxon',
item_type: 'Link',
menu_id: menu,
diff --git a/sample/db/samples/menus.rb b/sample/db/samples/menus.rb
index 24b90072aa..b1346a7b07 100644
--- a/sample/db/samples/menus.rb
+++ b/sample/db/samples/menus.rb
@@ -1,13 +1,17 @@
-Spree::Store.ids.each do |store|
- Spree::Menu.where(
- name: 'Main Menu',
- unique_code: 'spree-all-main',
- store_id: store
- ).first_or_create!
+Spree::Store.all.each do |store|
+ store.supported_locales_list.each do |locale|
+ Spree::Menu.where(
+ name: 'Main Menu',
+ location: 'header',
+ store_id: store,
+ locale: locale
+ ).first_or_create!
- Spree::Menu.where(
- name: 'Footer Menu',
- unique_code: 'spree-all-footer',
- store_id: store
- ).first_or_create!
+ Spree::Menu.where(
+ name: 'Footer Menu',
+ location: 'footer',
+ store_id: store,
+ locale: locale
+ ).first_or_create!
+ end
end