From 09fa1ceea91373896120a797324f86d4970a6077 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Sat, 22 Feb 2025 12:08:59 +0800 Subject: [PATCH] feat: update sitemap generation for multiple locales - Refactor generate_sitemap method to support locale - Add setup_sitemap_config method for configuration - Implement sitemap generation for default and localized paths This update enhances the sitemap generation process by supporting multiple languages. Users can now access a sitemap with language prefixes, improving SEO and usability for different locales. Additionally, error handling has been improved to log specific errors related to locale generation. --- app/workers/refresh_sitemap_worker.rb | 85 +++++++++++++++++++++------ config/locales/ja.yml | 6 +- config/locales/ko.yml | 6 +- 3 files changed, 77 insertions(+), 20 deletions(-) diff --git a/app/workers/refresh_sitemap_worker.rb b/app/workers/refresh_sitemap_worker.rb index 04eb004..6ff1834 100644 --- a/app/workers/refresh_sitemap_worker.rb +++ b/app/workers/refresh_sitemap_worker.rb @@ -10,7 +10,14 @@ class RefreshSitemapWorker if redis.set(lock_key, Time.current.to_s, nx: true, ex: lock_ttl) begin - generate_sitemap + setup_sitemap_config + # 生成默认的不带语言前缀的 sitemap + generate_sitemap(nil) + + # 为每个可用语言生成带前缀的 sitemap + I18n.available_locales.each do |locale| + generate_sitemap(locale) + end ensure redis.del(lock_key) end @@ -21,10 +28,11 @@ class RefreshSitemapWorker private - def generate_sitemap - host = Rails.env.production? ? "https://todayaiweather.com" : "http://127.0.0.1:3000" - Rails.application.routes.default_url_options[:host] = host - SitemapGenerator::Sitemap.default_host = ENV.fetch("RAILS_SITEMAP_DEFAULT_HOST", host) + def setup_sitemap_config + @host = Rails.env.production? ? "https://todayaiweather.com" : "http://127.0.0.1:3000" + Rails.application.routes.default_url_options[:host] = @host + SitemapGenerator::Sitemap.default_host = ENV.fetch("RAILS_SITEMAP_DEFAULT_HOST", @host) + if Rails.env.production? SitemapGenerator::Sitemap.adapter = SitemapGenerator::AwsSdkAdapter.new( ENV.fetch("AWS_BUCKET", Rails.application.credentials.dig(:minio, :bucket)), @@ -45,36 +53,77 @@ class RefreshSitemapWorker ) end SitemapGenerator::Sitemap.sitemaps_path = "sitemaps/" + end - SitemapGenerator::Sitemap.create do - add root_path, changefreq: "daily", priority: 1.0 - add cities_path, changefreq: "daily", priority: 0.9 - add arts_path, changefreq: "daily", priority: 0.9 + def generate_sitemap(locale = nil) + # 设置当前语言环境 + I18n.locale = locale || I18n.default_locale + Rails.application.routes.default_url_options[:locale] = locale + # 设置 sitemap 路径 + # path_prefix = locale ? "sitemaps/#{locale}/" : "sitemaps/" + # SitemapGenerator::Sitemap.sitemaps_path = path_prefix + + filename = locale==nil ? "sitemap" : "sitemap_#{locale}" + SitemapGenerator::Sitemap.create(filename: filename) do + available_locales = I18n.available_locales + + # 首页 + add root_path(locale: locale), + changefreq: "daily", + priority: 1.0, + alternate: available_locales.map { |al| + { lang: al, href: root_url(locale: al) } + } + + # 城市列表页 + add cities_path(locale: locale), + changefreq: "daily", + priority: 0.9, + alternate: available_locales.map { |al| + { lang: al, href: cities_url(locale: al) } + } + + # 艺术作品列表页 + add arts_path(locale: locale), + changefreq: "daily", + priority: 0.9, + alternate: available_locales.map { |al| + { lang: al, href: arts_url(locale: al) } + } + + # 城市详情页 City.find_each do |city| - add city_path(city), + add city_path(city, locale: locale), changefreq: "daily", priority: 0.8, - lastmod: city.updated_at + lastmod: city.updated_at, + alternate: available_locales.map { |al| + { lang: al, href: city_url(city, locale: al) } + } end + # 天气艺术作品页 WeatherArt.includes(:city).find_each do |art| if art.image.attached? - add city_weather_art_path(art.city, art), + add city_weather_art_path(art.city, art, locale: locale), changefreq: "daily", priority: 0.7, lastmod: art.updated_at, images: [ { - loc: url_for(art.image), - title: "#{art.city.name} Weather Art - #{art.weather_date.strftime('%B %d, %Y')}" - } ] + loc: url_for(art.image), + title: "#{art.city.name} Weather Art - #{art.weather_date.strftime('%B %d, %Y')}" + } ], + alternate: available_locales.map { |al| + { lang: al, href: city_weather_art_url(art.city, art, locale: al) } + } end end end - # SitemapGenerator::Sitemap.ping_search_engines if Rails.env.production? - Rails.logger.info "Sitemap has been generated and uploaded to S3 successfully" + Rails.logger.info "Generated sitemap for #{locale || 'default'} version" rescue => e - Rails.logger.error "Error refreshing sitemap: #{e.message}" + Rails.logger.error "Error generating sitemap for #{locale || 'default'}: #{e.message}" + raise e end end diff --git a/config/locales/ja.yml b/config/locales/ja.yml index 6677335..26bf946 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -59,4 +59,8 @@ ja: formats: time_only: "%H:%M" with_zone: "%{time} %{zone}" - date_and_time: "%{date} %{time}" \ No newline at end of file + date_and_time: "%{date} %{time}" + date: + formats: + short: "%Y-%m-%d" + long: "%Y 年 %m 月 %d 日" \ No newline at end of file diff --git a/config/locales/ko.yml b/config/locales/ko.yml index 7f323c3..468ba39 100644 --- a/config/locales/ko.yml +++ b/config/locales/ko.yml @@ -59,4 +59,8 @@ ko: formats: time_only: "%H:%M" with_zone: "%{time} %{zone}" - date_and_time: "%{date} %{time}" \ No newline at end of file + date_and_time: "%{date} %{time}" + date: + formats: + short: "%Y-%m-%d" + long: "%Y 년 %m 월 %d 일" \ No newline at end of file