mrbrdo / spree_mobility

BSD 3-Clause "New" or "Revised" License
2 stars 1 forks source link

Fallback on slug not working with API #12

Open dimidev opened 1 year ago

dimidev commented 1 year ago

i'm using spree 4.3.2 with this gems. Backend working fine with fallback, but on API it doesn't work

Gemfile

gem 'spree_i18n', '~> 5.0' # remove any existing spree_i18n in Gemfile
gem 'friendly_id-mobility', git: 'https://github.com/mrbrdo/friendly_id-mobility', branch: 'master'
gem 'mobility', git: 'https://github.com/mrbrdo/mobility', branch: 'mrbrdo'
gem 'spree_mobility'

mobility.rb

Mobility.configure do
    plugins do
        fallbacks({ en: [:el], el: [:en] })
    end
end

For example i have a product with both data on english and greek language. The product have only one slug with locale el (greek) en is empty, when i call http://localhost:4000/api/v2/storefront/products/product1?locale=el it returns the result. But when i use ?locale=en the result is "The resource you were looking for could not be found."

When i use cli i get proper results

I18n.locale = :el
Spree::Product.friendly.find "product1" # it will return product with greek data and works with greek slug

I18n.locale = :en
Spree::Product.friendly.find "product1" # it will return product with english data  and works with greek slug
mrbrdo commented 1 year ago

At the moment I am not using the API myself, I am using the legacy frontend. Due to time limitations I can't look into it myself, but if you manage to fix it I will be happy to merge it in. It seems like the way the API is finding the project bypasses the slug translations. Probably the cause is in the code of spree api and need to be fixed either by decorator or suggesting a PR to spree team in the api (they can be quite slow in my experience, but worth a try).

dimidev commented 1 year ago

I will compare it to spree globalize and friendly-id globalize if i can find any solution from there.