Skip to content

Commit

Permalink
Merge pull request #2559 from jhawthorn/locale_chooser
Browse files Browse the repository at this point in the history
Add locale chooser to admin
  • Loading branch information
jhawthorn authored Mar 29, 2018
2 parents 7cb9dea + 63721f9 commit a779d17
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 8 deletions.
1 change: 1 addition & 0 deletions backend/app/assets/javascripts/spree/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
//= require spree/backend/handlebars_extensions
//= require spree/backend/images/index
//= require spree/backend/images/upload
//= require spree/backend/locale_selection
//= require spree/backend/navigation
//= require spree/backend/option_type_autocomplete
//= require spree/backend/option_value_picker
Expand Down
18 changes: 18 additions & 0 deletions backend/app/assets/javascripts/spree/backend/locale_selection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Spree.ready(function() {
var localeSelect = document.querySelector('.js-locale-selection');
if (!localeSelect) return;

localeSelect.addEventListener('change', function() {
Spree.ajax({
type: "PUT",
dataType: "json",
url: Spree.pathFor("admin/locale/set"),
data: {
switch_to_locale: localeSelect.value
},
success: function(data) {
document.location.href = data.location;
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ nav.menu {
}
}

.admin-locale-selection {
margin: 1em;
margin-bottom: 0;
}

.admin-nav.fits .admin-nav-footer {
position: fixed;
bottom: 0;
Expand Down
24 changes: 24 additions & 0 deletions backend/app/controllers/spree/admin/locale_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Spree
module Admin
class LocaleController < Spree::Admin::BaseController
def set
locale = params[:switch_to_locale].to_s.presence

if locale && I18n.available_locales.include?(locale.to_sym)
I18n.locale = locale
session[:locale] = locale

respond_to do |format|
format.json { render json: { locale: locale, location: spree.admin_url } }
end
else
respond_to do |format|
format.json { render json: { locale: I18n.locale }, status: 404 }
end
end
end
end
end
end
15 changes: 15 additions & 0 deletions backend/app/views/spree/admin/shared/_locale_selection.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% available_locales = Spree.i18n_available_locales %>
<% if available_locales.size > 1 %>
<div class="admin-locale-selection">
<select class="js-locale-selection custom-select fullwidth">
<%=
options_for_select(
available_locales.map do |locale|
[I18n.t('spree.i18n.this_file_language', locale: locale, default: locale.to_s, fallback: false), locale]
end.sort,
selected: I18n.locale,
)
%>
</select>
</div>
<% end %>
1 change: 1 addition & 0 deletions backend/app/views/spree/admin/shared/_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<div class="admin-nav-sticky">
<%= render partial: 'spree/admin/shared/menu' %>
<div class="admin-nav-footer">
<%= render partial: 'spree/admin/shared/locale_selection' %>
<%= render partial: 'spree/admin/shared/navigation_footer' %>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions backend/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
get '/search/users', to: "search#users", as: :search_users
get '/search/products', to: "search#products", as: :search_products

put '/locale/set', to: 'locale#set', defaults: { format: :json }, as: :set_locale

resources :dashboards, only: [] do
collection do
get :home
Expand Down
1 change: 1 addition & 0 deletions backend/spec/features/admin/locale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
month_names: []
},
spree: {
i18n: { this_file_language: "Français" },
admin: {
tab: { orders: "Ordres" }
},
Expand Down
15 changes: 11 additions & 4 deletions core/lib/spree/core/controller_helpers/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ def accurate_title
private

def set_user_language
locale = session[:locale]
locale ||= config_locale if respond_to?(:config_locale, true)
locale ||= Rails.application.config.i18n.default_locale
locale ||= I18n.default_locale
available_locales = Spree.i18n_available_locales
locale = [
params[:locale],
session[:locale],
(config_locale if respond_to?(:config_locale, true)),
I18n.default_locale
].detect do |candidate|
candidate &&
available_locales.include?(candidate.to_sym)
end
session[:locale] = locale
I18n.locale = locale
Carmen.i18n_backend.locale = locale
end
Expand Down
6 changes: 6 additions & 0 deletions core/lib/spree/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
require 'action_view'

module Spree
def self.i18n_available_locales
I18n.available_locales.select do |locale|
I18n.t('spree.i18n.this_file_language', locale: locale, fallback: false, default: nil)
end
end

class TranslationHelperWrapper # :nodoc:
include ActionView::Helpers::TranslationHelper
end
Expand Down
45 changes: 45 additions & 0 deletions core/spec/lib/i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@

RSpec.describe "i18n" do
before do
# This reload avoids an issue with I18n.available_locales being cached
I18n.reload!

I18n.backend.store_translations(:en,
{
spree: {
i18n: {
this_file_language: "English"
},
foo: "bar",
bar: {
foo: "bar within bar scope",
Expand All @@ -19,6 +25,9 @@
}
})
end
after do
I18n.reload!
end

it "translates within the spree scope" do
expect(Spree.t(:foo)).to eql("bar")
Expand All @@ -44,4 +53,40 @@
it "should have a Spree::I18N_GENERIC_PLURAL constant" do
expect(Spree::I18N_GENERIC_PLURAL).to eq 2.1
end

describe "i18n_available_locales" do
it "should only return :en" do
expect(Spree.i18n_available_locales).to eq([:en])
end

context 'with unprefixed translations in another locale' do
before do
I18n.backend.store_translations(:fr, { cheese: "fromage" })
end

it "should only return :en" do
expect(Spree.i18n_available_locales).to eq([:en])
end
end

context 'with spree-prefixed translations in another locale' do
before do
I18n.backend.store_translations(:fr, spree: { cheese: "fromage" })
end

it "should return :en and :fr" do
expect(Spree.i18n_available_locales).to eq([:en])
end
end

context 'with specific desired key' do
before do
I18n.backend.store_translations(:fr, spree: { i18n: { this_file_language: "Français" } })
end

it "should return :en and :fr" do
expect(Spree.i18n_available_locales).to eq([:en, :fr])
end
end
end
end
4 changes: 4 additions & 0 deletions frontend/spec/controllers/controller_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
before do
I18n.enforce_available_locales = false
Spree::Frontend::Config[:locale] = :de
I18n.backend.store_translations(:de, spree: {
i18n: { this_file_language: "Deutsch (DE)" }
})
end

after do
I18n.reload!
Spree::Frontend::Config[:locale] = :en
I18n.locale = :en
I18n.enforce_available_locales = true
Expand Down
12 changes: 8 additions & 4 deletions frontend/spec/features/locale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ def with_locale(locale)

context 'shopping cart link and page' do
before do
I18n.backend.store_translations(:fr,
spree: {
cart: 'Panier',
shopping_cart: 'Panier'
I18n.backend.store_translations(:fr, spree: {
i18n: { this_file_language: "Français" },
cart: 'Panier',
shopping_cart: 'Panier'
})
end

after do
I18n.reload!
end

it 'should be in french' do
with_locale('fr') do
visit spree.root_path
Expand Down

0 comments on commit a779d17

Please sign in to comment.