From 815ee7628c5856140a16bf6deadcfd1d5bbb7e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Tue, 9 Nov 2021 18:34:07 +0800 Subject: [PATCH] avoid continuous changing i18n locale --- app/models/site_feed_annc.rb | 207 ++++++++++++++++++----------------- lib/feeds/engine.rb | 12 +- 2 files changed, 120 insertions(+), 99 deletions(-) diff --git a/app/models/site_feed_annc.rb b/app/models/site_feed_annc.rb index b3e93c0..97ae61a 100644 --- a/app/models/site_feed_annc.rb +++ b/app/models/site_feed_annc.rb @@ -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" => "#{tmp["source-site-title"]}", - "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"] = "#{tmp["source-site-title"]}" - 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" => "#{tmp["source-site-title"]}", "status-class" => "status-source" } - tmp["source-site-link"] = tmp["source-site"] - tmp["source-site"] = "#{tmp["source-site-title"]}" - 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"] = "#{tmp["source-site-title"]}" + 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" => "#{tmp["source-site-title"]}", + "status-class" => "status-source" + } + tmp["source-site-link"] = tmp["source-site"] + tmp["source-site"] = "#{tmp["source-site-title"]}" + 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 diff --git a/lib/feeds/engine.rb b/lib/feeds/engine.rb index 87bc8c5..8a811e4 100644 --- a/lib/feeds/engine.rb +++ b/lib/feeds/engine.rb @@ -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