Dynamic fallbacks per instance
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.
Overlap with #328 ?
@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 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 Thanks, that should definitely work as well. Looking forward to it.
Also it would be nice to have global dynamic fallbacks, just like Mobility.with_locale, but Mobility.with_fallbacks &block
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