Skip to content

Conversation

@KrishnaShuk
Copy link
Contributor

Closes #10810

Feature: Add language-aware filtering to the random book feature (/random endpoint)

When users select a non-English language for the Open Library interface, the random book feature now prioritizes showing books with editions available in their selected language. This applies only to "populated languages" (those with >15k borrowable ebooks), with automatic fallback to all languages for unpopulated languages.

Technical

Implementation follows the existing QueryCarousel.html pattern:

  • Modified get_random_borrowable_ebook_keys() to accept optional language parameter
  • Added per-language caching with dynamic keys: home.random_book.{lang} (e.g., home.random_book.fre, home.random_book.eng, home.random_book.all)
  • Updated random_book.GET() to:
    • Extract user language from web.ctx.lang
    • Convert ISO 639-1 to MARC21 format using convert_iso_to_marc()
    • Validate language is in get_populated_languages() set
    • Pass language to Solr query only for populated languages
  • Solr query becomes: type:edition AND ebook_access:[borrowable TO *] AND language:{lang} when language is populated

Populated languages (10 total): eng, fre, ger, spa, chi, ita, lat, dut, rus, jpn

Fallback behavior: For unpopulated languages (e.g., Hindi), the feature falls back to showing books from all languages, ensuring users always get results.

(cci:7://file:///home/krishna/Contribution/openlibrary/openlibrary/plugins/openlibrary/home.py:0:0-0:0)

Stakeholders

@cdrini

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Language-Aware "Random Book" Feature

2 participants