Extract methods in locale switcher

This commit is contained in:
Javi Martín
2021-06-28 14:41:33 +02:00
parent ff0f2215ea
commit 0750166d77
2 changed files with 23 additions and 15 deletions

View File

@@ -1,15 +1,13 @@
<% if I18n.available_locales.size > 1 %>
<div class="locale">
<form class="locale-form">
<label class="inline-block" for="locale-switcher">
<%= t("layouts.header.locale") %>
</label>
<select class="js-location-changer locale-switcher inline-block" name="locale-switcher" id="locale-switcher">
<% I18n.available_locales.map do |loc| %>
<option <%= "selected" if loc == I18n.locale %>
value="<%= current_path_with_query_params(locale: loc) %>"><%= name_for_locale(loc) %></option>
<% end %>
</select>
</form>
</div>
<% end %>
<div class="locale">
<form class="locale-form">
<label class="inline-block" for="locale-switcher">
<%= t("layouts.header.locale") %>
</label>
<select class="js-location-changer locale-switcher inline-block" name="locale-switcher" id="locale-switcher">
<% locales.map do |loc| %>
<option <%= "selected" if loc == I18n.locale %>
value="<%= current_path_with_query_params(locale: loc) %>"><%= name_for_locale(loc) %></option>
<% end %>
</select>
</form>
</div>

View File

@@ -1,3 +1,13 @@
class Layout::LocaleSwitcherComponent < ApplicationComponent
delegate :name_for_locale, :current_path_with_query_params, to: :helpers
def render?
locales.size > 1
end
private
def locales
I18n.available_locales
end
end