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,121 +20,132 @@ class SiteFeedAnnc
self[:all_contents_for_feed] = self.cache_annc
end
end
def cache_annc(force_refresh=false)
def cache_annc(force_refresh=false,locales=nil,trans=nil)
feed = SiteFeed.find(self.feed_id)
anns = feed.get_annc(force_refresh)
cat = self.category_title
locales = Site.first.in_use_locales rescue I18n.available_locales
if locales.nil?
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 = {}
locales.each do |locale|
locale = locale.to_s
data_for_locale = I18n.with_locale(locale) do
anns.collect do |a|
tmp = a.deep_dup
tmp[:is_hidden] = self.hidden_annc.include?(tmp['id'])
if self.channel_key == "announcement"
tmp["postdate"] = tmp["postdate"].blank? ? nil : tmp["postdate"].to_time
tmp['statuses'] = []
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_top] = false
else
tmp[:is_top] = true
tmp['statuses'] << {
"status" => I18n.t(:top),
"status-class" => "status-top"
}
end
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_hot] = false
else
tmp[:is_hot] = true
tmp['statuses'] << {
"status" => I18n.t(:hot),
"status-class" => "status-hot"
}
end
tmp["category"] = cat
tmp["source-site"] = self.remote_site_url
tmp["source-site-title"] = (self[:channel_title][locale] rescue "")
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h"
if !tmp["source-site-title"].blank?
tmp['statuses'] << {
"status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
"status-class" => "status-source"
}
end
files = tmp["bulletin_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (bf["title_translations"][locale].blank? ? File.basename(bf["url"]) : bf["title_translations"][locale] rescue '') }} rescue []
links = tmp["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue []
tmp["bulletin_links"] = links
tmp["bulletin_files"] = files
tmp["title"] = tmp["title_translations"][locale]
tmp["subtitle"] = tmp["subtitle_translations"][locale]
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["link_to_show"] = !tmp["external_link"].blank? ? tmp["external_link"] : nil
tmp["target"] = "_self"
tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg"
tmp["img_description"] = tmp["image_description_translations"][locale]
tmp["more"] = I18n.t(:more_plus)
tmp["view_count"] = ""
data_for_locale = anns.collect do |a|
tmp = a.deep_dup
tmp[:is_hidden] = self.hidden_annc.include?(tmp['id'])
if self.channel_key == "announcement"
tmp["postdate"] = tmp["postdate"].blank? ? nil : tmp["postdate"].to_time
tmp['statuses'] = []
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_top] = false
else
tmp['statuses'] = []
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_top] = false
else
tmp[:is_top] = true
tmp['statuses'] << {
"status" => I18n.t(:top),
"status-class" => "status-top"
}
end
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_hot] = false
else
tmp[:is_hot] = true
tmp['statuses'] << {
"status" => I18n.t(:hot),
"status-class" => "status-hot"
}
end
tmp["category"] = cat
tmp["source-site"] = self.remote_site_url
tmp["source-site-title"] = (self[:channel_title][locale] rescue "")
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h"
tmp[:is_top] = true
tmp['statuses'] << {
"status" => trans[locale]['top'],
"status-class" => "status-top"
}
end
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_hot] = false
else
tmp[:is_hot] = true
tmp['statuses'] << {
"status" => trans[locale]['hot'],
"status-class" => "status-hot"
}
end
tmp["category"] = cat
tmp["source-site"] = self.remote_site_url
tmp["source-site-title"] = (self[:channel_title][locale] rescue "")
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h"
if !tmp["source-site-title"].blank?
tmp['statuses'] << {
"status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
"status-class" => "status-source"
}
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["target"] = "_self"
tmp["more"] = I18n.t(:more_plus)
tmp["view_count"] = ""
new_tmp = {}
tmp.each do |key,value|
if key.include? "_translations"
new_tmp[key.sub("_translations","")] = value[locale].to_s rescue ""
elsif key.include?("date") || key.include?("Date")
new_tmp[key] = DateTime.parse(value) rescue nil
else
if value.class == Hash
value.each do |sub_k,sub_v|
if sub_k.include? "_translations"
new_tmp[key][sub_k.sub("_translations","")] = sub_v[locale].to_s rescue ""
else
new_tmp[key][sub_k] = sub_v
end
end
files = tmp["bulletin_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (bf["title_translations"][locale].blank? ? File.basename(bf["url"]) : bf["title_translations"][locale] rescue '') }} rescue []
links = tmp["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue []
tmp["bulletin_links"] = links
tmp["bulletin_files"] = files
tmp["title"] = tmp["title_translations"][locale]
tmp["subtitle"] = tmp["subtitle_translations"][locale]
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["link_to_show"] = !tmp["external_link"].blank? ? tmp["external_link"] : nil
tmp["target"] = "_self"
tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg"
tmp["img_description"] = tmp["image_description_translations"][locale]
tmp["more"] = trans[locale]['more_plus']
tmp["view_count"] = ""
else
tmp['statuses'] = []
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_top] = false
else
tmp[:is_top] = true
tmp['statuses'] << {
"status" => trans[locale]['top'],
"status-class" => "status-top"
}
end
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_hot] = false
else
tmp[:is_hot] = true
tmp['statuses'] << {
"status" => trans[locale]['hot'],
"status-class" => "status-hot"
}
end
tmp["category"] = cat
tmp["source-site"] = self.remote_site_url
tmp["source-site-title"] = (self[:channel_title][locale] rescue "")
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h"
tmp['statuses'] << {
"status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
"status-class" => "status-source"
}
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["target"] = "_self"
tmp["more"] = trans[locale]['more_plus']
tmp["view_count"] = ""
new_tmp = {}
tmp.each do |key,value|
if key.include? "_translations"
new_tmp[key.sub("_translations","")] = value[locale].to_s rescue ""
elsif key.include?("date") || key.include?("Date")
new_tmp[key] = DateTime.parse(value) rescue nil
else
if value.class == Hash
value.each do |sub_k,sub_v|
if sub_k.include? "_translations"
new_tmp[key][sub_k.sub("_translations","")] = sub_v[locale].to_s rescue ""
else
new_tmp[key][sub_k] = sub_v
end
else
new_tmp[key] = value
end
else
new_tmp[key] = value
end
end
tmp = BSON::Document.new(new_tmp)
end
tmp
tmp = BSON::Document.new(new_tmp)
end
tmp
end
cache_data[locale.to_s] = data_for_locale
end

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_annc', __FILE__)
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|
tmp = SiteFeedAnnc.where(feed_id: site_feed.id).first
if site_feed.disabled != true
@ -18,7 +28,7 @@ module Feeds
tmp.category_title = site_feed.category[:title] rescue {}
tmp.remote_site_url = site_feed.remote_site_url
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
elsif !tmp.nil?
tmp.destroy