i18n pages should have SEO links to other language versions
i18n_pages like https://openlibrary.org/subjects should include HTML headers for different languages. Eg.
<link rel="alternate" hreflang="en" href="https://openlibrary.org/subjects?lang=en" />
<link rel="alternate" hreflang="es" href="https://openlibrary.org/subjects?lang=es" />
And so on.
Describe the problem that you'd like solved
Improve SEO by following Google's best practices
Additional context
- Google's SEO docs: https://developers.google.com/search/docs/advanced/crawling/localized-versions
Stakeholders
@jimchamp I'd like to take this one. It's five years old, so I checked it's still real before asking — it is: grep -rn hreflang across the repo returns nothing, and the ?lang= URLs in the original example still work.
How it resolves today. get_i18n_page (vendor/infogami/infogami/core/db.py:44) looks up <key>.<lang>, falling back to .en, where web.ctx.lang comes from ?lang=, then the HTTP_LANG cookie, then Accept-Language (vendor/infogami/infogami/utils/i18n.py:150-160). So /subjects and /subjects?lang=es are two URLs serving different content off the same path, which is exactly the case hreflang exists for.
Approach. Emit the alternates into HeadContext.links (openlibrary/core/shell/head.py), rendered by templates/shell/head.html.jinja, so it stays on the Open Library side with no infogami change. For an i18n_page, the alternates are the <key>.<lang> docs that actually exist, plus x-default pointing at the untagged URL.
The one thing I want your read on. Listing which translations exist means a site query per render of an i18n_page. The cheap version is to check only the 27 locales in openlibrary/i18n/ and cache the result per key; the honest version queries the datastore. Happy either way, but I'd rather not guess which you'd prefer.
Files: openlibrary/core/shell/head.py, openlibrary/templates/shell/head.html.jinja, openlibrary/templates/type/i18n_page/view.html, and a test next to the existing head tests.
One note for @RayBB, since this sits next to the canonical work in #14198: lang must stay out of queries_to_exclude. Stripping it would point every language version at the same canonical and cancel the hreflang set out. It isn't on the list and I'm not proposing to add it — just flagging it so nobody adds it later as an obvious-looking cleanup.
@MohitSAGAR11 feel free to explore what it would take but I'm not sure how difficult it could be.
@RayBB — explored it, and it's small. Built it so the estimate isn't a guess.
Size of the problem. Production has 35 /type/i18n_page docs and 74 translation docs between them (/query.json?type=/type/i18n_page, then key~=<key>.* for each). The biggest is /subjects with 7 languages; most have 2. The alternate set is never more than a handful of links.
The cheap option I offered is the wrong one. I'd suggested skipping the lookup and assuming the 27 locales in openlibrary/i18n/. Translations actually exist in only 8 languages site-wide, and per page it's usually en + es — assuming 27 would emit ~25 hreflangs per page pointing at URLs that don't exist, which Search Console reports as errors. So the lookup has to be real: one site.things({"key~": "/subjects.*"}), memoized in memcache for an hour, since translations are added by hand a few times a year.
Alternates point at ?lang=, not /subjects.es. Both serve Spanish, but an hreflang set is only honoured when every URL in it returns the same set. ?lang=es renders through the i18n page, so it does; /subjects.es is an ordinary page with no head links of its own. ?lang= is also already self-canonical today. x-default goes to the bare URL, which negotiates on Accept-Language.
Running locally
/subjects and its seven translations copied from production into a dev instance, captured by swapping the template between master and the branch with gunicorn --reload in between — so both panes below are the same running server.

And the page the es link points at actually renders in Spanish:

Three things I found on the way
/tour.en.enand/about/catalog.en.enexist — malformed keys. The code skips anything that isn't language-shaped rather than emittinghreflang="en.en".- Five
/books/OL…Mdocs are typed/type/i18n_pagewith no translations at all, so they render "No translations found." /subjects.esis separately indexed and self-canonical, so Spanish has two competing URLs. Worth a canonical pointing at/subjects?lang=es, but that's its own issue.
Where it stands
~70 lines in openlibrary/core/i18n_pages.py, three in templates/type/i18n_page/view.html, one import, and 10 tests. Full pre-commit green.
@jimchamp you're the lead here — happy to open the PR if this is the right shape, could you assign?