Skip to content

Commit 81e288e

Browse files
authored
Merge pull request #3397 from AlchemyCMS/locale-select
Add Alchemy::Admin::LocaleSelect component
2 parents 051134a + 58c1496 commit 81e288e

File tree

5 files changed

+75
-18
lines changed

5 files changed

+75
-18
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module Alchemy
2+
module Admin
3+
# Renders a locale select tag for switching the backend locale.
4+
class LocaleSelect < ViewComponent::Base
5+
attr_reader :name
6+
7+
def initialize(name = :change_locale)
8+
@name = name
9+
end
10+
11+
def call
12+
select_tag(
13+
name,
14+
options_for_select(
15+
translations_for_select,
16+
::I18n.locale
17+
)
18+
)
19+
end
20+
21+
def render?
22+
available_locales.many?
23+
end
24+
25+
private
26+
27+
def available_locales
28+
@_available_locales ||= Alchemy::I18n.available_locales.sort!
29+
end
30+
31+
def translations_for_select
32+
available_locales.map do |locale|
33+
[Alchemy.t(locale, scope: :translations), locale]
34+
end
35+
end
36+
end
37+
end
38+
end

app/helpers/alchemy/admin/base_helper.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ def link_to_dialog(content, url, options = {}, html_options = {})
6666
end
6767
end
6868

69-
# Used for translations selector in Alchemy cockpit user settings.
70-
def translations_for_select
71-
Alchemy::I18n.available_locales.sort.map do |locale|
72-
[Alchemy.t(locale, scope: :translations), locale]
73-
end
74-
end
75-
7669
def alchemy_admin_js_translations(locale = ::I18n.locale)
7770
render partial: "alchemy/admin/translations/#{locale}", formats: [:js]
7871
rescue ActionView::MissingTemplate

app/views/layouts/alchemy/admin.html.erb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@
8383
<% end %>
8484
<div id="user_info">
8585
<%= current_alchemy_user_name %>
86-
<% if Alchemy::I18n.available_locales.length > 1 %>
87-
<%= select_tag 'change_locale',
88-
options_for_select(translations_for_select, ::I18n.locale) %>
89-
<% end %>
86+
<%= render Alchemy::Admin::LocaleSelect.new %>
9087
</div>
9188
</div>
9289
<div id="toolbar">
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
RSpec.describe Alchemy::Admin::LocaleSelect, type: :component do
6+
subject! do
7+
expect(Alchemy::I18n).to receive(:available_locales) { locales }
8+
render
9+
end
10+
11+
let(:render) { render_inline described_class.new }
12+
13+
context "with one available locale" do
14+
let(:locales) { [:de] }
15+
16+
it "should not render anything" do
17+
expect(rendered_content).to be_blank
18+
end
19+
end
20+
21+
context "with many available locales" do
22+
let(:locales) { %i[de en] }
23+
24+
it "should return a select with available locales with current locale selected" do
25+
expect(page).to have_selector("select[name='change_locale'] option[value='de'] + option[value='en'][selected]", text: "EN")
26+
end
27+
28+
context "with name given" do
29+
let(:render) { render_inline described_class.new(:language) }
30+
31+
it "should return a select with available locales with current locale selected" do
32+
expect(page).to have_selector("select[name='language']")
33+
end
34+
end
35+
end
36+
end

spec/helpers/alchemy/admin/base_helper_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ module Alchemy
4747
end
4848
end
4949

50-
describe "#translations_for_select" do
51-
it "should return an Array of Arrays with available locales" do
52-
allow(Alchemy::I18n).to receive(:available_locales).and_return(%i[de en cz it])
53-
expect(helper.translations_for_select.size).to eq(4)
54-
end
55-
end
56-
5750
describe "#clipboard_select_tag_options" do
5851
let(:page) { build_stubbed(:alchemy_page) }
5952

0 commit comments

Comments
 (0)