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

Dynamic fallbacks per instance #391

Closed
RobPando opened this issue Jun 9, 2020 · 6 comments
Closed

Dynamic fallbacks per instance #391

RobPando opened this issue Jun 9, 2020 · 6 comments

Comments

@RobPando
Copy link

RobPando commented Jun 9, 2020

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.

@shioyama
Copy link
Owner

Overlap with #328 ?

@RobPando
Copy link
Author

RobPando commented Jun 12, 2020

@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.

@shioyama
Copy link
Owner

shioyama commented Jun 14, 2020

@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?

@RobPando
Copy link
Author

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

@molfar
Copy link

molfar commented Jul 4, 2020

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

@doits
Copy link
Contributor

doits commented Dec 15, 2020

Just want to give a heads up I could easily implement this as my own plugin with Mobility 1.0.0, see #328 (comment)

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

4 participants