avoid continuous changing i18n locale

This commit is contained in:
邱博亞 2021-11-09 18:34:07 +08:00
parent b5fbe80a7c
commit 815ee7628c
2 changed files with 120 additions and 99 deletions

View File

@ -20,16 +20,28 @@ class SiteFeedAnnc
self[:all_contents_for_feed] = self.cache_annc self[:all_contents_for_feed] = self.cache_annc
end end
end end
def cache_annc(force_refresh=false) def cache_annc(force_refresh=false,locales=nil,trans=nil)
feed = SiteFeed.find(self.feed_id) feed = SiteFeed.find(self.feed_id)
anns = feed.get_annc(force_refresh) anns = feed.get_annc(force_refresh)
cat = self.category_title cat = self.category_title
if locales.nil?
locales = Site.first.in_use_locales rescue I18n.available_locales locales = Site.first.in_use_locales rescue I18n.available_locales
end
if trans.nil?
trans = {}
locales.each do |locale|
trans[locale] = {}
I18n.with_locale(locale) do
trans[locale]['top'] = I18n.t(:top)
trans[locale]['hot'] = I18n.t(:hot)
trans[locale]['more_plus'] = I18n.t(:more_plus)
end
end
end
cache_data = {} cache_data = {}
locales.each do |locale| locales.each do |locale|
locale = locale.to_s locale = locale.to_s
data_for_locale = I18n.with_locale(locale) do data_for_locale = anns.collect do |a|
anns.collect do |a|
tmp = a.deep_dup tmp = a.deep_dup
tmp[:is_hidden] = self.hidden_annc.include?(tmp['id']) tmp[:is_hidden] = self.hidden_annc.include?(tmp['id'])
if self.channel_key == "announcement" if self.channel_key == "announcement"
@ -40,7 +52,7 @@ class SiteFeedAnnc
else else
tmp[:is_top] = true tmp[:is_top] = true
tmp['statuses'] << { tmp['statuses'] << {
"status" => I18n.t(:top), "status" => trans[locale]['top'],
"status-class" => "status-top" "status-class" => "status-top"
} }
end end
@ -49,7 +61,7 @@ class SiteFeedAnnc
else else
tmp[:is_hot] = true tmp[:is_hot] = true
tmp['statuses'] << { tmp['statuses'] << {
"status" => I18n.t(:hot), "status" => trans[locale]['hot'],
"status-class" => "status-hot" "status-class" => "status-hot"
} }
end end
@ -76,7 +88,7 @@ class SiteFeedAnnc
tmp["target"] = "_self" tmp["target"] = "_self"
tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg" tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg"
tmp["img_description"] = tmp["image_description_translations"][locale] tmp["img_description"] = tmp["image_description_translations"][locale]
tmp["more"] = I18n.t(:more_plus) tmp["more"] = trans[locale]['more_plus']
tmp["view_count"] = "" tmp["view_count"] = ""
else else
tmp['statuses'] = [] tmp['statuses'] = []
@ -85,7 +97,7 @@ class SiteFeedAnnc
else else
tmp[:is_top] = true tmp[:is_top] = true
tmp['statuses'] << { tmp['statuses'] << {
"status" => I18n.t(:top), "status" => trans[locale]['top'],
"status-class" => "status-top" "status-class" => "status-top"
} }
end end
@ -94,7 +106,7 @@ class SiteFeedAnnc
else else
tmp[:is_hot] = true tmp[:is_hot] = true
tmp['statuses'] << { tmp['statuses'] << {
"status" => I18n.t(:hot), "status" => trans[locale]['hot'],
"status-class" => "status-hot" "status-class" => "status-hot"
} }
end end
@ -109,7 +121,7 @@ class SiteFeedAnnc
tmp["source-site-link"] = tmp["source-site"] tmp["source-site-link"] = tmp["source-site"]
tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>" tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>"
tmp["target"] = "_self" tmp["target"] = "_self"
tmp["more"] = I18n.t(:more_plus) tmp["more"] = trans[locale]['more_plus']
tmp["view_count"] = "" tmp["view_count"] = ""
new_tmp = {} new_tmp = {}
tmp.each do |key,value| tmp.each do |key,value|
@ -135,7 +147,6 @@ class SiteFeedAnnc
end end
tmp tmp
end end
end
cache_data[locale.to_s] = data_for_locale cache_data[locale.to_s] = data_for_locale
end end
cache_data cache_data

View File

@ -6,6 +6,16 @@ module Feeds
require File.expand_path('../../../app/models/site_feed', __FILE__) require File.expand_path('../../../app/models/site_feed', __FILE__)
require File.expand_path('../../../app/models/site_feed_annc', __FILE__) require File.expand_path('../../../app/models/site_feed_annc', __FILE__)
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console') && defined?(SiteFeed) && defined?(SiteFeedAnnc) if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console') && defined?(SiteFeed) && defined?(SiteFeedAnnc)
locales = Site.first.in_use_locales rescue I18n.available_locales
trans = {}
locales.each do |locale|
trans[locale] = {}
I18n.with_locale(locale) do
trans[locale]['top'] = I18n.t(:top)
trans[locale]['hot'] = I18n.t(:hot)
trans[locale]['more_plus'] = I18n.t(:more_plus)
end
end
SiteFeed.each do |site_feed| SiteFeed.each do |site_feed|
tmp = SiteFeedAnnc.where(feed_id: site_feed.id).first tmp = SiteFeedAnnc.where(feed_id: site_feed.id).first
if site_feed.disabled != true if site_feed.disabled != true
@ -18,7 +28,7 @@ module Feeds
tmp.category_title = site_feed.category[:title] rescue {} tmp.category_title = site_feed.category[:title] rescue {}
tmp.remote_site_url = site_feed.remote_site_url tmp.remote_site_url = site_feed.remote_site_url
tmp.channel_title = site_feed.channel_title_for_cache tmp.channel_title = site_feed.channel_title_for_cache
tmp.all_contents_for_feed = tmp.cache_annc tmp.all_contents_for_feed = tmp.cache_annc(false,locales,trans)
tmp.save tmp.save
elsif !tmp.nil? elsif !tmp.nil?
tmp.destroy tmp.destroy