avoid continuous changing i18n locale
This commit is contained in:
parent
b5fbe80a7c
commit
815ee7628c
|
@ -20,121 +20,132 @@ 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
|
||||||
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 = {}
|
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"
|
tmp["postdate"] = tmp["postdate"].blank? ? nil : tmp["postdate"].to_time
|
||||||
tmp["postdate"] = tmp["postdate"].blank? ? nil : tmp["postdate"].to_time
|
tmp['statuses'] = []
|
||||||
tmp['statuses'] = []
|
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
|
||||||
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
|
tmp[:is_top] = false
|
||||||
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"] = ""
|
|
||||||
else
|
else
|
||||||
tmp['statuses'] = []
|
tmp[:is_top] = true
|
||||||
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
|
tmp['statuses'] << {
|
||||||
tmp[:is_top] = false
|
"status" => trans[locale]['top'],
|
||||||
else
|
"status-class" => "status-top"
|
||||||
tmp[:is_top] = true
|
}
|
||||||
tmp['statuses'] << {
|
end
|
||||||
"status" => I18n.t(:top),
|
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
|
||||||
"status-class" => "status-top"
|
tmp[:is_hot] = false
|
||||||
}
|
else
|
||||||
end
|
tmp[:is_hot] = true
|
||||||
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
|
tmp['statuses'] << {
|
||||||
tmp[:is_hot] = false
|
"status" => trans[locale]['hot'],
|
||||||
else
|
"status-class" => "status-hot"
|
||||||
tmp[:is_hot] = true
|
}
|
||||||
tmp['statuses'] << {
|
end
|
||||||
"status" => I18n.t(:hot),
|
tmp["category"] = cat
|
||||||
"status-class" => "status-hot"
|
tmp["source-site"] = self.remote_site_url
|
||||||
}
|
tmp["source-site-title"] = (self[:channel_title][locale] rescue "")
|
||||||
end
|
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h"
|
||||||
tmp["category"] = cat
|
if !tmp["source-site-title"].blank?
|
||||||
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'] << {
|
tmp['statuses'] << {
|
||||||
"status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
|
"status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
|
||||||
"status-class" => "status-source"
|
"status-class" => "status-source"
|
||||||
}
|
}
|
||||||
tmp["source-site-link"] = tmp["source-site"]
|
end
|
||||||
tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>"
|
|
||||||
tmp["target"] = "_self"
|
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 []
|
||||||
tmp["more"] = I18n.t(:more_plus)
|
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["view_count"] = ""
|
tmp["bulletin_links"] = links
|
||||||
new_tmp = {}
|
tmp["bulletin_files"] = files
|
||||||
tmp.each do |key,value|
|
tmp["title"] = tmp["title_translations"][locale]
|
||||||
if key.include? "_translations"
|
tmp["subtitle"] = tmp["subtitle_translations"][locale]
|
||||||
new_tmp[key.sub("_translations","")] = value[locale].to_s rescue ""
|
tmp["source-site-link"] = tmp["source-site"]
|
||||||
elsif key.include?("date") || key.include?("Date")
|
tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>"
|
||||||
new_tmp[key] = DateTime.parse(value) rescue nil
|
tmp["link_to_show"] = !tmp["external_link"].blank? ? tmp["external_link"] : nil
|
||||||
else
|
tmp["target"] = "_self"
|
||||||
if value.class == Hash
|
tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg"
|
||||||
value.each do |sub_k,sub_v|
|
tmp["img_description"] = tmp["image_description_translations"][locale]
|
||||||
if sub_k.include? "_translations"
|
tmp["more"] = trans[locale]['more_plus']
|
||||||
new_tmp[key][sub_k.sub("_translations","")] = sub_v[locale].to_s rescue ""
|
tmp["view_count"] = ""
|
||||||
else
|
else
|
||||||
new_tmp[key][sub_k] = sub_v
|
tmp['statuses'] = []
|
||||||
end
|
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
|
end
|
||||||
else
|
|
||||||
new_tmp[key] = value
|
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
new_tmp[key] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
tmp = BSON::Document.new(new_tmp)
|
|
||||||
end
|
end
|
||||||
tmp
|
tmp = BSON::Document.new(new_tmp)
|
||||||
end
|
end
|
||||||
|
tmp
|
||||||
end
|
end
|
||||||
cache_data[locale.to_s] = data_for_locale
|
cache_data[locale.to_s] = data_for_locale
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue