Simplify setting the options in language selector

This commit is contained in:
Javi Martín
2021-06-30 18:08:37 +02:00
parent 9ccfabcbda
commit 477c67efd8
2 changed files with 7 additions and 4 deletions

View File

@@ -4,10 +4,7 @@
<%= 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| %> <%= options_for_select(language_options, current_path_with_query_params(locale: I18n.locale)) %>
<option lang="<%= loc %>" <%= "selected" if loc == I18n.locale %>
value="<%= current_path_with_query_params(locale: loc) %>"><%= name_for_locale(loc) %></option>
<% end %>
</select> </select>
</form> </form>
</div> </div>

View File

@@ -10,4 +10,10 @@ class Layout::LocaleSwitcherComponent < ApplicationComponent
def locales def locales
I18n.available_locales I18n.available_locales
end end
def language_options
locales.map do |locale|
[name_for_locale(locale), current_path_with_query_params(locale: locale), lang: locale]
end
end
end end