made the locale switcher use less js, more ruby & html

This commit is contained in:
kikito
2015-08-28 17:02:06 +02:00
parent 57917f850c
commit 934512ad7a
6 changed files with 17 additions and 32 deletions

View File

@@ -31,7 +31,7 @@ var initialize_modules = function() {
App.Votes.initialize(); App.Votes.initialize();
App.Tags.initialize(); App.Tags.initialize();
App.Stats.initialize(); App.Stats.initialize();
App.LocaleSwitcher.initialize(); App.LocationChanger.initialize();
App.DebatesOrderSelector.initialize(); App.DebatesOrderSelector.initialize();
}; };

View File

@@ -1,19 +0,0 @@
App.LocaleSwitcher =
href_with_params: (query_params) ->
loc = window.location
loc.protocol + "//" + loc.hostname +
(if loc.port then ':' + loc.port else '') +
loc.pathname +
loc.hash +
'?' + $.param(query_params)
initialize: ->
$('.js-locale-switcher').on 'change', ->
query_params = window.getQueryParameters()
query_params['locale'] = $(this).val()
window.location.assign(App.LocaleSwitcher.href_with_params(query_params))

View File

@@ -0,0 +1,8 @@
App.LocationChanger =
initialize: ->
$('.js-location-changer').on 'change', ->
window.location.assign($(this).val())

View File

@@ -14,13 +14,4 @@ module ApplicationHelper
home_page? ? '' : 'results' home_page? ? '' : 'results'
end end
def available_locale_options_for_select
options_for_select(available_locales_array, I18n.locale)
end
private
def available_locales_array
I18n.available_locales.map { |loc| [I18n.t('locale', locale: loc), loc] }
end
end end

View File

@@ -1,8 +1,13 @@
<div class="locale"> <div class="locale">
<span class="inline-block"><%= t("layouts.header.locale") %></span> <span class="inline-block"><%= t("layouts.header.locale") %></span>
<form class="locale-form"> <form class="locale-form">
<select class="js-locale-switcher locale-switcher" name="locale-switcher"> <select class="js-location-changer locale-switcher" name="locale-switcher">
<%= available_locale_options_for_select %> <% I18n.available_locales.map do |loc| %>
<option <%= 'selected' if loc == I18n.locale %>
value='<%= url_for(request.query_parameters.merge(locale: loc))%>'>
<%= I18n.t('locale', locale: loc) %>
</option>
<% end %>
</select> </select>
</form> </form>
</div> </div>

View File

@@ -12,7 +12,7 @@ feature 'Localization' do
scenario 'Available locales appear in the locale switcher' do scenario 'Available locales appear in the locale switcher' do
visit '/' visit '/'
within('.js-locale-switcher') do within('.locale-form .js-location-changer') do
expect(page).to have_content 'Español' expect(page).to have_content 'Español'
expect(page).to have_content 'English' expect(page).to have_content 'English'
end end