From dcf762726c9f4c32d8cf93c7dbe8552871be522a Mon Sep 17 00:00:00 2001 From: songtianlun Date: Tue, 8 Apr 2025 17:04:32 +0800 Subject: [PATCH] feat: add meta tags for SEO optimization - Implement meta tags in ArtsController, CitiesController, HomeController, and WeatherArtsController for better SEO. - Use translation helpers for titles, descriptions, and keywords to improve internationalization support. - Update default meta tags in ApplicationHelper for consistent site-wide SEO. These changes enhance the search engine visibility of the application by providing relevant meta information across various pages. Utilizing translation for these fields promotes better support for multiple languages, aligning with localization efforts. --- app/controllers/arts_controller.rb | 5 ++++ app/controllers/cities_controller.rb | 12 ++++++-- app/controllers/home_controller.rb | 6 ++-- app/controllers/weather_arts_controller.rb | 16 ++++++++-- app/helpers/application_helper.rb | 25 ++++++++-------- config/locales/en.yml | 34 ++++++++++++++++++++++ 6 files changed, 77 insertions(+), 21 deletions(-) diff --git a/app/controllers/arts_controller.rb b/app/controllers/arts_controller.rb index 6fa9182..6ba90a0 100644 --- a/app/controllers/arts_controller.rb +++ b/app/controllers/arts_controller.rb @@ -17,5 +17,10 @@ class ArtsController < ApplicationController end @weather_arts = @weather_arts.page(params[:page]).per(12) + + set_meta_tags( + title: t("meta.arts.index.title"), + description: t("meta.arts.index.description") + ) end end diff --git a/app/controllers/cities_controller.rb b/app/controllers/cities_controller.rb index b49372c..3c88b24 100644 --- a/app/controllers/cities_controller.rb +++ b/app/controllers/cities_controller.rb @@ -46,6 +46,12 @@ class CitiesController < ApplicationController ) } end + + set_meta_tags( + title: t("meta.cities.index.title"), + description: t("meta.cities.index.description"), + keywords: t("meta.cities.index.keywords") + ) end def show @@ -58,9 +64,9 @@ class CitiesController < ApplicationController } set_meta_tags( - title: "#{@city.name}, #{@city&.country&.name}", - description: "Experience #{@city.name}'s weather through AI-generated art. Daily updates of weather conditions visualized through artificial intelligence.", - keywords: "#{@city.name}, #{@city.country.name}, ai, ai web, ai art, ai weather, weather art, AI visualization", + title: t("meta.cities.show.title", city_name: @city.name, country_name: @city&.country&.name.to_s), + description: t("meta.cities.show.description", city_name: @city.name), + keywords: t("meta.cities.show.keywords", city_name: @city.name, country_name: @city.country&.name.to_s), og: { image: @city.latest_weather_art&.image&.attached? ? url_for(@city.latest_weather_art.image) : nil } diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 89203e0..8951701 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -5,9 +5,9 @@ class HomeController < ApplicationController # @random_arts = WeatherArt.includes(:city, :image_attachment).random(3) # @featured_arts = WeatherArt.includes(:city, :image_attachment).order(created_at: :desc).limit(5) set_meta_tags( - title: "AI-Generated Weather Art", - description: "Experience weather through artistic AI visualization. Daily updated weather art for cities worldwide.", - keywords: "AI weather art, weather visualization, city weather, artificial intelligence" + title: t("meta.home.index.title"), + description: t("meta.home.index.description"), + keywords: t("meta.home.index.keywords") ) end end diff --git a/app/controllers/weather_arts_controller.rb b/app/controllers/weather_arts_controller.rb index ed8ab63..3a2df34 100644 --- a/app/controllers/weather_arts_controller.rb +++ b/app/controllers/weather_arts_controller.rb @@ -15,10 +15,20 @@ class WeatherArtsController < ApplicationController event_type: "city_view" } + formatted_date = @weather_art.weather_date.strftime(t("date.formats.default")) + set_meta_tags( - title: "#{@city.name} Weather Art - #{@weather_art.weather_date.strftime('%B %d, %Y')}", - description: "#{@city.name}'s weather visualized through AI art. #{@weather_art.description} at #{@weather_art.temperature}°C.", - keywords: "#{@city.name}, #{@city.country.name}, ai, ai web, ai art, ai weather, weather art, AI visualization, #{@weather_art.description}", + title: t("meta.weather_arts.show.title", + city_name: @city.name, + date: formatted_date), + description: t("meta.weather_arts.show.description", + city_name: @city.name, + description: @weather_art.description, + temperature: @weather_art.temperature), + keywords: t("meta.weather_arts.show.keywords", + city_name: @city.name, + country_name: @city.country.name, + description: @weather_art.description), og: { image: @weather_art.image.attached? ? url_for(@weather_art.image) : nil } diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4884968..0c82573 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -28,24 +28,25 @@ module ApplicationHelper def default_meta_tags { - site: "TodayAIWeather", - title: "TodayAIWeather", - description: "Experience weather through artistic AI visualization. Daily updated weather art for cities worldwide.", - keywords: "AI weather art, weather visualization, city weather, artificial intelligence", + site: t("site_name", default: "TodayAIWeather"), + title: t("meta.default.title", default: "TodayAIWeather"), + description: t("meta.default.description", default: "Experience weather through artistic AI visualization. Daily updated weather art for cities worldwide."), + keywords: t("meta.default.keywords", default: "AI weather art, weather visualization, city weather, artificial intelligence"), separator: "—".html_safe, reverse: true, og: { - site_name: "TodayAIWeather", + site_name: t("site_name", default: "TodayAIWeather"), type: "website", - keywords: "ai, ai web, ai art, ai weather, weather art, AI visualization, today ai weather", + keywords: t("meta.default.og.keywords", default: "ai, ai web, ai art, ai weather, weather art, AI visualization, today ai weather"), url: request.original_url }, - alternate: { - "en" => url_for(locale: "en"), - "zh-CN" => url_for(locale: "zh-CN"), - "ja" => url_for(locale: "ja"), - "ko" => url_for(locale: "ko") - } + alternate: available_locales_with_urls } end + + def available_locales_with_urls + I18n.available_locales.each_with_object({}) do |locale, hash| + hash[locale.to_s] = url_for(locale: locale) + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 816304a..a18e201 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -117,3 +117,37 @@ en: default: "%Y-%m-%d" short: "%b %d" long: "%B %d, %Y" + + site_name: "TodayAIWeather" + meta: + default: + title: "TodayAIWeather" + description: "Experience weather through artistic AI visualization. Daily updated weather art for cities worldwide." + keywords: "AI weather art, weather visualization, city weather, artificial intelligence" + og: + keywords: "ai, ai web, ai art, ai weather, weather art, AI visualization, today ai weather" + + home: + index: + title: "AI-Generated Weather Art" + description: "Experience weather through artistic AI visualization. Daily updated weather art for cities worldwide." + keywords: "AI weather art, weather visualization, city weather, artificial intelligence" + arts: + index: + title: "Arts Directory" + description: "Explore our AI-generated weather art" + cities: + index: + title: "Cities Directory - Weather Art" + description: "Explore our collection of cities with AI-generated weather art. Filter by region, country or search for specific locations." + keywords: "city weather, AI visualization, weather directory, global cities" + show: + title: "%{city_name}, %{country_name}" + description: "Experience %{city_name}'s weather through AI-generated art. Daily updates of weather conditions visualized through artificial intelligence." + keywords: "%{city_name}, %{country_name}, ai, ai web, ai art, ai weather, weather art, AI visualization" + + weather_arts: + show: + title: "%{city_name} Weather Art - %{date}" + description: "%{city_name}'s weather visualized through AI art. %{description} at %{temperature}°C." + keywords: "%{city_name}, %{country_name}, ai, ai web, ai art, ai weather, weather art, AI visualization, %{description}"