-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set fallback locale for Mobility based on current store
- Loading branch information
1 parent
fefbbce
commit 8a25054
Showing
10 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
core/app/services/spree/locales/set_fallback_locale_for_store.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Spree | ||
module Locales | ||
class SetFallbackLocaleForStore | ||
def call(store:) | ||
store_default_locale = store.default_locale | ||
fallbacks = store.supported_locales_list.each_with_object({}) do |locale, object| | ||
object[locale] = [store_default_locale] | ||
end | ||
|
||
fallbacks_instance = I18n::Locale::Fallbacks.new(fallbacks) | ||
|
||
Mobility.store_based_fallbacks = fallbacks_instance | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'concurrent' | ||
require 'mobility' | ||
|
||
module Mobility | ||
@store_based_fallbacks = Concurrent::ThreadLocalVar.new(I18n::Locale::Fallbacks.new) | ||
|
||
class << self | ||
def store_based_fallbacks | ||
@store_based_fallbacks.value | ||
end | ||
|
||
def store_based_fallbacks=(value) | ||
@store_based_fallbacks.value = value | ||
end | ||
end | ||
|
||
module Plugins | ||
module StoreBasedFallbacks | ||
extend ::Mobility::Plugin | ||
|
||
default true | ||
requires :backend, include: :before | ||
|
||
# Applies fallbacks plugin to attributes. Completely disables fallbacks | ||
# on model if option is +false+. | ||
included_hook do |_, backend_class| | ||
unless options[:fallbacks] == false | ||
backend_class.include(BackendInstanceMethods) | ||
end | ||
end | ||
|
||
module BackendInstanceMethods | ||
def read(locale, fallback: true, **kwargs) | ||
return super(locale, **kwargs) if !fallback || kwargs[:locale] | ||
|
||
locales = Mobility.store_based_fallbacks[locale] | ||
locales.each do |fallback_locale| | ||
value = super(fallback_locale, **kwargs) | ||
return value if Util.present?(value) | ||
end | ||
|
||
super(locale, **kwargs) | ||
end | ||
end | ||
end | ||
|
||
register_plugin(:store_based_fallbacks, StoreBasedFallbacks) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require 'friendly_id/paranoia' | ||
require 'mobility/plugins/store_based_fallbacks' | ||
|
||
require 'spree/core' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
core/spec/services/spree/locales/set_fallback_locale_for_store_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'spec_helper' | ||
|
||
module Spree | ||
describe Locales::SetFallbackLocaleForStore do | ||
let(:command) { described_class.new } | ||
|
||
describe '#call' do | ||
let(:store) { create(:store, default_locale: 'pl', supported_locales: 'en,de,pl') } | ||
let(:product) { create(:product, name: 'test') } | ||
|
||
let(:name_en) { 'name en' } | ||
let(:name_pl) { 'name pl' } | ||
|
||
let!(:translation_en) { product.translations.find_or_create_by(locale: 'en') { |e| e.name = name_en } } | ||
let!(:translation_pl) { product.translations.find_or_create_by(locale: 'pl') { |e| e.name = name_pl } } | ||
|
||
before { command.call(store: store) } | ||
|
||
context 'when translatable object does not have a translation in the requested locale' do | ||
it 'sets mobility to retrieve value in the fallback locale' do | ||
name = I18n.with_locale(:de) { product.name } | ||
expect(name).to eq(name_pl) | ||
end | ||
end | ||
|
||
context 'when translatable object has a translation in the requested locale' do | ||
let(:name_de) { 'name de' } | ||
let!(:translation_de) { product.translations.find_or_create_by(locale: 'de') { |e| e.name = name_de } } | ||
|
||
it 'sets mobility to retrieve value directly' do | ||
name = I18n.with_locale(:de) { product.name } | ||
expect(name).to eq(name_de) | ||
end | ||
end | ||
end | ||
end | ||
end |