-
Notifications
You must be signed in to change notification settings - Fork 83
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
Fall through to I18n.fallbacks when defined #180
Conversation
53807ed
to
a4d0ee9
Compare
a4d0ee9
to
68c3dfd
Compare
This is necessary to avoid leakage when the default locale is changed (even temporarily) in specs.
And here's what I get with this PR: irb(main):002:0> I18n.fallbacks[:sv]
=> [:sv]
irb(main):003:0> I18n.fallbacks[:en]
=> [:en, :sv]
irb(main):004:0> I18n.fallbacks[:de]
=> [:de, :en, :sv]
irb(main):005:0> Mobility.new_fallbacks[:sv]
=> [:sv]
irb(main):006:0> Mobility.new_fallbacks[:en]
=> [:en, :sv]
irb(main):007:0> Mobility.new_fallbacks[:de]
=> [:de, :sv, :en] Important thing here is that you can also add model-specific fallbacks, and they will be "mixed in" with the default fallbacks: irb(main):008:0> Mobility.new_fallbacks(de: [:fr])[:de]
=> [:de, :fr, :sv, :en] This means that you have |
@shioyama I tested this and it works as expected and now Mobility respect the For "dynamic" cases what I'm finally doing is to have a before_action and override the static fallbacks in this way: |
Great to hear that it works. About dynamic cases, I can add support for passing a proc to class Post < ApplicationRecord
extend Mobility
translates :title, fallbacks: -> { current_user.language.shortname }
end or something like that. The point about thread safety is basically that if you change global state from one thread, there is the possibility that another thread will change the same data before you actually use your value. Basically mutable globals are bad for thread safety, and in this case But if you don't have a lot of requests, you may not notice this problem. |
@shioyama that solution looks great for dynamic cases, just one question |
Yes, you're right that won't work since Actually, in this case the safer way to do it would be to pass in the user's fallbacks to the getter method, like this (assuming a model post.title(fallback: current_user.language.shortname) this will try the current locale, and if it is |
@shioyama thanks, yes I think that it will work although I believe that could be interesting to have an option to set it globally instead to have to pass it each time we call a translated attribute (not sure if set it globally and be thread safe is possible). I believe that if we have to pass the dynamic value in each getter method for all the translated attributes in the application code can be difficult to maintain. |
In this case, I'd say it's better to explicitly pass in the fallbacks through the getter. Using globals for convenience is not something I would recommend, thread safety is one issue but not the only one. Having an option to use a |
Fixes #161.