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">
<div class="locale"> <form class="locale-form">
<form class="locale-form"> <label class="inline-block" for="locale-switcher">
<label class="inline-block" for="locale-switcher"> <%= t("layouts.header.locale") %>
<%= t("layouts.header.locale") %> </label>
</label> <select class="js-location-changer locale-switcher inline-block" name="locale-switcher" id="locale-switcher">
<select class="js-location-changer locale-switcher inline-block" name="locale-switcher" id="locale-switcher"> <% locales.map do |loc| %>
<% I18n.available_locales.map do |loc| %> <option <%= "selected" if loc == I18n.locale %>
<option <%= "selected" if loc == I18n.locale %> value="<%= current_path_with_query_params(locale: loc) %>"><%= name_for_locale(loc) %></option>
value="<%= current_path_with_query_params(locale: loc) %>"><%= name_for_locale(loc) %></option> <% end %>
<% end %> </select>
</select> </form>
</form> </div>
</div>
<% end %>

View File

@@ -1,3 +1,13 @@
class Layout::LocaleSwitcherComponent < ApplicationComponent class Layout::LocaleSwitcherComponent < ApplicationComponent
delegate :name_for_locale, :current_path_with_query_params, to: :helpers 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 end