-
-
Notifications
You must be signed in to change notification settings - Fork 411
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
Problem changing locale with multiple threads #269
Comments
I'll try to verify your script and the issue tomorrow. Thanks for reporting. |
Thank you Carlos, I forgot to mention, this happens on master too and my ruby is:
|
As far as I can tell , |
On this same file, the comment on lines 5 and 6 says that :locale is the only config scoped to a specific thread, so I assumed that default_locale is a global, and all threads should respect it. If this is by design, we should think a way to change locale to every single thread. This is useful on a GUI software which has several threads running and a user changes the default locale. |
The issue is still unresolved. The source of the issue, as it seems from my testing, is that I18n stores the settings in the thread's variable using: As you can see in the source code, the settings are persistent. The default_locale isn't reviewed again after it had been reviewed for the first time. The solution would probably be to change the I18n::Config class's #locale method to keep accessing the @locale || default_locale An easy workaround is to manually reset the locale to the default locale each time. Here is the code I used for testing: require 'i18n'
I18n.available_locales = [:pt, :en]
I18n.enforce_available_locales = true
I18n.default_locale = :en
# t1 = Thread.new do
# 1.upto(4) do
# Thread.current[:i18n_config] = nil
# puts I18n.t(:foo)
# puts Thread.current.keys
# sleep 1
# end
# end
t1 = Thread.new do
1.upto(4) do
I18n.locale = I18n.default_locale
puts I18n.t(:foo)
sleep 1
end
end
sleep 0.1 #<<<THIS
I18n.default_locale = :pt
t1.join |
I've added the fix from #320 to master now, which should fix this issue. Thanks everyone! |
Hi,
I'm having a problem with I18n and multiple threads on a pure ruby app, which can be reproduced with this small script:
The problem is: If I run this, I got as output four
translation missing: en.foo
, but if I remove thesleep
marked on the code, I got fourtranslation missing: pt.foo
.I'm aware that the four messages for the pt make sense, since the locale change happens before the thread runs, but I think that on the first case it should print some messages on
en
and some onpt
(the amount of each would be not deterministic).This is a expected behavior?
The text was updated successfully, but these errors were encountered: