Skip to content

Commit c56d5ff

Browse files
authored
Merge pull request solidusio#95 from DanielePalombo/nebulab/localize-every-store
Localize each store
2 parents 14039be + 3803c36 commit c56d5ff

19 files changed

Lines changed: 252 additions & 50 deletions

File tree

.hound.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Style/Documentation:
88
Enabled: false
99

1010
# Neatly aligned code is too swell.
11-
Style/SingleSpaceBeforeFirstArg:
11+
Layout/SpaceBeforeFirstArg:
1212
Enabled: false
1313

1414
# Don't mess with RSpec DSL.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Spree.EditInlineLocales = Backbone.View.extend(
2+
initialize: ->
3+
@editing = false
4+
@render()
5+
6+
events:
7+
'click [data-action=edit]': 'onEdit'
8+
'click [data-action=save]': 'onSave'
9+
'click [data-action=cancel]': 'onCancel'
10+
11+
onEdit: (e) ->
12+
return if @editing
13+
@$el.addClass 'editing'
14+
@editing = true
15+
@render()
16+
17+
onCancel: (e) ->
18+
e.preventDefault()
19+
@$el.removeClass("editing")
20+
@editing = false
21+
@render()
22+
23+
onSave: (e) ->
24+
e.preventDefault()
25+
preferred_available_locales = $('#available-locales-store-' + @model.id)
26+
Spree.ajax
27+
type: 'PUT'
28+
url: Spree.routes.available_locales_api + '/' + @model.id + '.json'
29+
data:
30+
store:
31+
preferred_available_locales: preferred_available_locales.val()
32+
success: (response) =>
33+
@model = response.store
34+
@editing = false
35+
@$el.removeClass("editing")
36+
@render()
37+
error: (response) =>
38+
show_flash 'error', response.responseJSON.error
39+
40+
render: ->
41+
renderAttr =
42+
availableLocales: <%= SolidusI18n::Locale.all.push(:en).to_json %>
43+
availableLocalesPresentation: <%= Hash[(SolidusI18n::Locale.all + [:en]).map do |locale|
44+
[locale, Spree.t(:'i18n.this_file_language', locale: locale)]
45+
end].to_json %>
46+
store: @model
47+
editing: @editing
48+
_.extend(renderAttr, @model.attributes)
49+
50+
@$el.html(HandlebarsTemplates['available_locales'](renderAttr))
51+
$('.available-locales').select2({placeholder: Spree.translations['please_choose_language'], width: 'element'})
52+
53+
return @
54+
)
55+
56+
Spree.routes.available_locales_api = Spree.pathFor('api/config/available_locales');
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
window.Handlebars.registerHelper 'locale-presentation', (locale, localePresentationHashmap, options) ->
2+
localePresentationHashmap[locale]
3+
4+
window.Handlebars.registerHelper 'selected', (locale, store, options) ->
5+
for k of store.preferences.available_locales
6+
value = store.preferences.available_locales[k]
7+
return 'selected="selected"' if value == locale
8+
9+
window.Handlebars.registerHelper 'store-available-locales', (store, options) ->
10+
locales = for k of store.preferences.available_locales
11+
locale = store.preferences.available_locales[k]
12+
@availableLocalesPresentation[locale]
13+
14+
locales.join(',')
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Spree.InlineTableLocales = Backbone.View.extend(
2+
initialize: ->
3+
if @$el.length
4+
Spree.ajax({
5+
type: 'GET'
6+
url: "/api/config/available_locales"
7+
success: (collection) =>
8+
for store in collection
9+
row = $("<tr id='store-id-#{store.id}'/>")
10+
@$el.append(row)
11+
new Spree.EditInlineLocales({
12+
el: row
13+
model: store
14+
});
15+
})
16+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<td>
2+
{{store.name}}
3+
{{#if store.default}}
4+
<span class="label label-default">default</span>
5+
{{/if}}
6+
</td>
7+
<td>{{store.url}}</td>
8+
<td>
9+
{{#if editing}}
10+
<select id='available-locales-store-{{store.id}}' class='available-locales select2 fullwidth' name='store[preferred_available_locales][]' multiple='true'>
11+
{{#each availableLocales}}
12+
<option value="{{ this }}" {{#selected this ../store}}{{/selected}}>
13+
{{#locale-presentation this ../availableLocalesPresentation}}{{/locale-presentation}}
14+
</option>
15+
{{/each}}
16+
</select>
17+
{{else}}
18+
{{store-available-locales store availableLocales}}
19+
{{#each store.preferences.available_locales}}
20+
{{locale-presentation this}}
21+
{{/each}}
22+
{{/if}}
23+
</td>
24+
<td class="actions">
25+
{{#if editing}}
26+
<a class="fa fa-check icon_link with-tip no-text" data-action="save" data-id="{{id}}" href="#"></a>
27+
<a class="fa fa-cancel icon_link with-tip no-text" data-action="cancel" data-id="{{id}}" href="#"></a>
28+
{{else}}
29+
<a class="fa fa-edit icon_link with-tip no-text" data-action="edit" data-id="{{id}}" href="#"></a>
30+
{{/if}}
31+
</td>
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
$ ->
22
_.extend (Spree.translations),
33
please_choose_language: "<%= Spree.t(:'i18n.choose_language') %>"
4-
5-
$('#available_locales_').select2({placeholder: Spree.translations['please_choose_language']})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Spree
2+
module Api
3+
class AvailableLocalesController < Spree::Api::BaseController
4+
def index
5+
respond_with Store.all
6+
end
7+
8+
def update
9+
authorize! :update, store
10+
if store.update_attributes(store_params)
11+
respond_with(store, status: 200, default_template: :show)
12+
else
13+
invalid_resource!(store)
14+
end
15+
end
16+
17+
private
18+
19+
def store_params
20+
params.require(:store).permit(preferred_available_locales: [])
21+
end
22+
23+
def store
24+
@store ||= Store.find(params[:id])
25+
end
26+
end
27+
end
28+
end
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
Spree::LocaleController.class_eval do
2-
def set
3-
redirect_to root_path(locale: params[:switch_to_locale])
4-
end
2+
def set
3+
redirect_to root_path(locale: params[:switch_to_locale])
4+
end
55
end
6-

app/helpers/solidus_i18n/locale_helper.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module SolidusI18n
22
module LocaleHelper
3-
4-
def select_available_locales
5-
select_tag('available_locales[]', options_for_select(
6-
all_locales_options,
7-
Config.available_locales
8-
), common_options)
3+
def select_available_locales(store = nil)
4+
select_tag('store[preferred_available_locales][]',
5+
options_for_select(
6+
all_locales_options,
7+
store.preferred_available_locales
8+
), common_options)
99
end
1010

1111
def available_locales_options
12-
Config.available_locales.map { |locale| locale_presentation(locale) }
12+
current_store.preferred_available_locales.map { |locale| locale_presentation(locale) }
1313
end
1414

1515
def all_locales_options
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module SolidusI18n
2+
module AddAvailableLanguagesPreferenceToStore
3+
def self.prepended(base)
4+
base.preference :available_locales, :array, default: [:en]
5+
end
6+
7+
def preferred_available_locales
8+
super.map(&:to_sym)
9+
end
10+
end
11+
12+
Spree::Store.prepend AddAvailableLanguagesPreferenceToStore
13+
end

0 commit comments

Comments
 (0)