Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback local for sort if translated attribute is nil #396

Closed
aaronromeo opened this issue Jun 15, 2020 · 4 comments
Closed

Fallback local for sort if translated attribute is nil #396

aaronromeo opened this issue Jun 15, 2020 · 4 comments

Comments

@aaronromeo
Copy link

If a translated attribute is nil in the current Mobility.locale, ideally the attribute value in fallback language would be used as a substitute in the order by clause.

Context

I am currently using Mobility with Postgres (with a Table backend).

    mobility (0.8.10)
      i18n (>= 0.6.10, < 2)
      request_store (~> 1.0)

Expected Behavior

This is an example of the expected behaviour

class Category < ApplicationRecord
  extend Mobility

  translates :name, type: :string, fallbacks_generator: lambda {
                                                          I18n.available_locales.each_with_object({}) do |locale, memo|
                                                            memo[locale] = :'en-US'
                                                          end
                                                        }, locale_accessors: true

end

The data is seeded without a value in Spanish for category1

category1 = Category.new
category2 = Category.new

Mobility.with_locale('es-MX') do
  category2.name = 'Puerco'
  category2.save
end

Mobility.with_locale('en-US') do
  category1.name = 'Mouse'
  category1.save

  category2.name = 'Pig'
  category2.save
end

The expected output uses the English fallback name for Mouse as the Spanish name value is missing and English has been defined as the fallback locale.

Mobility.with_locale('es-MX') { Category.i18n.order(:name).map(&:name) }
["Mouse", "Puerco"]

Actual Behavior

The order attribute appears to resolve the name to nil when sorting. This moves category1 to the bottom of the array.

(byebug) Mobility.with_locale('es-MX') { Category.i18n.order(:name).map(&:name) }
["Puerco", "Mouse"]

Possible Fix

It might be possible to fallback the locale using COALESCE.

current_locale_table = "category_translations_#{Mobility.normalized_locale}"
Category.
  joins("LEFT OUTER JOIN \"category_translations\" \"category_translations_en_us\" ON \"category_translations_en_us\".\"category_id\" = \"categories\".\"id\" AND \"category_translations_en_us\".\"locale\" = 'en-US'").
  joins("LEFT OUTER JOIN \"category_translations\" \"#{current_locale_table}\" ON \"#{current_locale_table}\".\"category_id\" = \"categories\".\"id\" AND \"#{current_locale_table}\".\"locale\" = ").
  order(Arel.sql("COALESCE(#{current_locale_table}.name, category_translations_en_us.name)"))
@shioyama
Copy link
Owner

shioyama commented Jun 15, 2020

translates :name, type: :string, fallbacks_generator: lambda {
                                                        I18n.available_locales.each_with_object({}) do |locale, memo|
                                                          memo[locale] = :'en-US'
                                                        end
                                                      }, locale_accessors: true

fallbacks_generator: is not an option for the fallbacks plugin, so that option value is being ignored.

@shioyama
Copy link
Owner

Mental note: v1.0 should warn or raise on options that are not consumed by any plugin or backend.

@shioyama
Copy link
Owner

If a translated attribute is nil in the current Mobility.locale, ideally the attribute value in fallback language would be used as a substitute in the order by clause.

This is similar to #51, but it's tricky and not really a priority right now, sorry. I'm going to close it because realistically it's not going to happen (anytime soon, anyway).

@aaronromeo
Copy link
Author

@shioyama thanks for the clarification on fallbacks_generator. I misunderstood the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants