I have created a feature that publish a news with the language of the page's creator.
Here is the code that create the news :
def add_news
locale = creator.language.blank? ? I18n.locale : creator.language
I18n.with_locale(locale) do
title = I18n.t('news.subject')
end
create_news({title: title})
end
It works good, the news is created with the good language. But sometimes, a wrong language is used. I have read the sourcecode of i18n (https://github.com/svenfuchs/i18n/blob/master/lib/i18n.rb), and for me the with_local function is not threadsafe. I was very surprised beacause I have read no post on that problem.
So, waht do you think about that ? Threadsafe or not ? Do you know a other solution if so ?
Thanks and br,
Eric
Looks like it is from the Ruby on Rails guides as it is using Thread.current.
Also ran a small (conclusive) experiment:
n = I18n.available_locales.length
10.times do |i|
loc = I18n.available_locales[i % n]
Thread.new do
I18n.with_locale(loc) do
puts "#{loc} #{I18n.t 'one.of.your.keys'}"
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With