mobility icon indicating copy to clipboard operation
mobility copied to clipboard

Dynamic fallbacks per instance

Open RobPando opened this issue 5 years ago • 6 comments

Context

I am in need of a way to dynamically set the fallbacks based on each instance/record. I currently have a default fallback establish for I18n which is fine, however I need some records to have another locale as default leaving the I18n empty or nil, it works fine, for example say my default fallback locale is en and I try to do something like translates :title, fallbacks: { en: :nl } this works as expected, I can leave the en title empty if I have nl. However, it won't always be nl this can be other languages and here is where my problem is because each record has a field called default_locale which I need want to pull from in order to declare the fallback for en I tried looking but cannot find a way to do this.

Expected Behavior

Ideally the instance object and attributes would be available to me or have ways to dynamically set fallbacks for the attributes. like:

translate :title, fallbacks: -> { en: my_dynamic_attribute_from_current_model }

Actual Behavior

There is no scope to the current instance that would allow me to declare a fallback based on the models attribute. I also have not found any other method that allows this.

Possible Fix

Having a way to scope and access the attributes of an instance inside fallbacks or a way to declare dynamic fallbacks on locales for each instance.

RobPando avatar Jun 09 '20 20:06 RobPando

Overlap with #328 ?

shioyama avatar Jun 11 '20 04:06 shioyama

@shioyama that PR seems to have gone stale. Is there anything I can do to help move the process forward and get some sort of solution integrated into the library? What else is needed? I will be glad to help.

RobPando avatar Jun 12 '20 15:06 RobPando

@RobPando I'm working on a v1.0, and improving fallbacks fits in there. But I can't promise when that will be released, although I am putting time into it as you can see from the commit log.

I'd love to ask for help, but the problem is that the interface for the fallbacks plugin right now is ugly and I'd like to take this opportunity to improve it with 1.0. I think supporting procs makes a lot of sense and I intend to add it.

What would be maybe more useful than implementation right now is agreeing on what the API would look like.

You've suggested:

translate :title, fallbacks: -> { en: my_dynamic_attribute_from_current_model }

The earlier PR (and what I would imagine) would be something where you define a proc which takes the locale, and then does something to get the fallback, like this:

translate :title, fallbacks: ->(locale) { ... }

Does that work for you?

shioyama avatar Jun 14 '20 02:06 shioyama

@shioyama Thanks, that should definitely work as well. Looking forward to it.

RobPando avatar Jun 15 '20 12:06 RobPando

Also it would be nice to have global dynamic fallbacks, just like Mobility.with_locale, but Mobility.with_fallbacks &block

molfar avatar Jul 04 '20 09:07 molfar

Just want to give a heads up I could easily implement this as my own plugin with Mobility 1.0.0, see https://github.com/shioyama/mobility/pull/328#issuecomment-745270816

doits avatar Dec 15 '20 13:12 doits