From 1caf0dc2f0caa553901f297480f27f9113e6e0e8 Mon Sep 17 00:00:00 2001
From: neuroalien <105230050+neuroalien@users.noreply.github.com>
Date: Fri, 21 Jul 2023 12:57:52 +0100
Subject: [PATCH] AO3-6553 Bonus i18n for media on homepage

This is not strictly related to the a11y issue, but we're trying to add
i18n if we can when we touch a file.

The presence of `ts` in the original shows that we always intended to
translate media names. We don't usually translate tags, but the media
tags are a special case, because they're heavily used in site navigation

The locale preference is not transmitted at the moment, so we don't risk
caching the navigation in a non-English language for the entire site.

When we know what the parameters or settings look like, we should be
able to do something like add the locale key to the cache key, to have
one cache per locale.
---
 app/models/media.rb              |  4 ++++
 app/views/home/_fandoms.html.erb |  2 +-
 config/locales/views/en.yml      | 10 ++++++++++
 spec/models/media_spec.rb        | 10 ++++++++++
 4 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 spec/models/media_spec.rb

diff --git a/app/models/media.rb b/app/models/media.rb
index 5aa0081185..851fcb1bdf 100644
--- a/app/models/media.rb
+++ b/app/models/media.rb
@@ -8,6 +8,10 @@ def child_types
     ['Fandom']
   end
 
+  def translation_key
+    name.parameterize(separator: "_")
+  end
+
   # The media tag for unwrangled fandoms
   def self.uncategorized
     tag = self.find_or_create_by_name(ArchiveConfig.MEDIA_UNCATEGORIZED_NAME)
diff --git a/app/views/home/_fandoms.html.erb b/app/views/home/_fandoms.html.erb
index f5cd74ef2e..8a92a8a8a2 100644
--- a/app/views/home/_fandoms.html.erb
+++ b/app/views/home/_fandoms.html.erb
@@ -3,7 +3,7 @@
   <% cache "homepage-fandoms-version1", skip_digest: true do %>
     <% Media.for_menu.each do |medium| %>
       <% unless medium.id.nil? %>
-        <li><%= link_to ts("#{medium.name}"), medium_fandoms_path(medium) %></li>
+        <li><%= link_to t(".#{medium.translation_key}"), medium_fandoms_path(medium) %></li>
       <% end %>
     <% end %>
   <% end %>
diff --git a/config/locales/views/en.yml b/config/locales/views/en.yml
index 69b2975528..6efd7e5d33 100644
--- a/config/locales/views/en.yml
+++ b/config/locales/views/en.yml
@@ -431,6 +431,16 @@ en:
       page_title: Donate or Volunteer
     fandoms:
       all_fandoms: All Fandoms
+      anime_manga: Anime & Manga
+      books_literature: Books & Literature
+      cartoons_comics: Cartoons & Comics
+      movies: Movies
+      music_bands: Music & Bands
+      other_media: Other Media
+      real_people_celebrities: Real People & Celebrities
+      tv_shows: TV Shows
+      uncategorized_fandoms: Uncategorized Fandoms
+      video_games: Video Games
     index:
       browse_or_favorite:
         one: Browse fandoms by media or favorite up to %{count} tag to have it listed here!
diff --git a/spec/models/media_spec.rb b/spec/models/media_spec.rb
new file mode 100644
index 0000000000..f36a178b06
--- /dev/null
+++ b/spec/models/media_spec.rb
@@ -0,0 +1,10 @@
+require "spec_helper"
+
+describe Media do
+  describe "#translation_key" do
+    it "returns a parameterized version of the media name using underscore as a separator" do
+      media = create(:media, name: "Widgets; Tools & Doodads")
+      expect(media.translation_key).to eql("widgets_tools_doodads")
+    end
+  end
+end