openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Use Babel to select correct language from the user’s browser settings

Open tfmorris opened this issue 8 months ago • 9 comments

Feature Request

Problem / Opportunity

There are (at least) three different mappings which need to be made, independently, from the user's browser's language settings, taking into account their language preferences in priority order:

  1. Book text language - #12003 covers this.
  2. UI localization language for Babel - this should use Babel's negotiate_locale to find the best match
  3. Preferred Wikipedia link - I suspect Wikipedia has an algorithm / lookup to do this, but it would require investigation

As an example, consider zh-HK and zh-TW:

BCP 47 code: zh-HK Book language codes: cmn + chi (chi is a macro language code for Chinese, while cmn refers to Mandarin Chinese) Wikipedia code: zh (Chinese Wikipedia)

BCP 47 code: zh-TW Book language codes: yue + chi Wikipedia code: zh-yue? (Cantonese Wikipedia is much smaller than Chinese, so zh might be a better recommendation)

Unfortunately, the current implementation conflates these three mappings. It also only considers the first language in the Accept-Language header, when the user may have a localization that's supported and preferred to English as their second or third choice. eg. pt-BR, pt, es-419, en

Proposal

The full browser Accept-Language info should be retained for use in these mappings and for future uses like identifying locales which share a common language across multiple nations, such as Portugese Brazilian (pt-BR) or Latin American Spanish (es-419).

Babel's Locale.negotiate_locale should be used to find best matches so that more than the first locale/language is considered.

Breakdown

Implementation Details (for maintainers)

Related files

Refer to this map of common Endpoints: *

Requirements Checklist

Checklist of requirements that need to be satisfied in order for this issue to be closed:

  • [ ]

Stakeholders


Instructions for Contributors

tfmorris avatar Mar 15 '26 21:03 tfmorris

We're struggling a bit on the i18n front and would really benefit from a community lead to take on i18n PM'ing. As a first step, only capacity I have is to spin up an agent to classify all these and propose some sort of project management.

mekarpeles avatar Mar 16 '26 19:03 mekarpeles

Hey @mekarpeles! I'd love to help out here. I went through the issue and the core problem makes sense to me — the three mappings (book language, UI locale, Wikipedia) are currently tangled together and only the first Accept-Language value is being used. I took a quick look in the codebase and found the entry point is infogami/utils/i18n.py (the parse_lang_header function). I'm genuinely interested in taking this on. Would it be okay if I start by posting a clean technical breakdown of the three separate functions + the exact places to update? Happy to go at whatever pace works for the team!

bhardwajparth51 avatar Mar 16 '26 19:03 bhardwajparth51

@bhardwajparth51 this seems like a significant effort and I don't have the bandwidth to review the solution. I can try to contribute project management towards i18n, to clear up what is being asked holistically within our i18n landscape but I cannot in good faith recommend you take on this issue because I'm not sure yet how I'd support you.

mekarpeles avatar Mar 16 '26 22:03 mekarpeles

I am interested in this issue can i do it

eshanthh avatar Mar 17 '26 10:03 eshanthh

#12110 closes this

eshanthh avatar Mar 17 '26 10:03 eshanthh

@eshantharjun9-hub I recommend waiting until the requirements are clearly defined before attempting a solution (and waiting to be assigned)

@mekarpeles The first item has #12003 associated with it already and the third item can be deferred, so that just leaves the second. If it would help, I can isolate that into a ticket of its own.

Part of the reason I created the ticket now is that I see the same bugs being replicated in the greenfield API implementation, which doesn't seem to make sense to me. The code below won't give the user their best UI language if their first choice isn't available, but their second or third is. Babel's negotiate_locale takes care of that.

https://github.com/internetarchive/openlibrary/blob/e84f7e11767e3dfaa00d722a484ad0df96b49fa2/openlibrary/asgi_app.py#L85-L97

Looking back in time, it looks like this is basically cleaning up the implementation that I started to review in 2020.

tfmorris avatar Mar 17 '26 18:03 tfmorris

@tfmorris will work on this

eshanthh avatar Mar 19 '26 17:03 eshanthh

@eshantharjun9-hub there isn't anything to work on yet.

This issue is currently mixing multiple concerns, but it looks like we can make concrete progress by focusing on the UI localization (Babel) piece only.

Problem (Current Behavior)

Right now, parse_lang_header:

  • Only takes the first language
  • Drops region info (pt-BR → pt)
  • Ignores user preference order beyond the first entry
  • Doesn’t check against supported locales

Example:

Accept-Language: pt-BR,pt;q=0.9,es-419;q=0.8,en;q=0.7

Today → "pt" Expected → "pt_BR" (if supported), otherwise "pt" or next best match


Proposed Approach

Use Babel’s negotiate_locale to select the best match.

Step 1 — Parse Accept-Language

Extract full language tags (preserve region):

["pt-BR", "pt", "es-419", "en"]

(Simple parsing is fine; we can ignore q-values initially if needed.)


Step 2 — Normalize format

Babel expects underscores:

["pt_BR", "pt", "es_419", "en"]

Step 3 — Define supported locales

We need a list of locales OL actually supports, e.g.:

SUPPORTED_LOCALES = ["en", "es", "fr", "pt_BR", "zh_CN"]

(This likely already exists somewhere in our i18n config.)


Step 4 — Negotiate best match

from babel.core import negotiate_locale

best = negotiate_locale(preferred_languages, SUPPORTED_LOCALES)

Fallback to "en" if no match.


Step 5 — Integrate

Replace the current logic in:

openlibrary/openlibrary/asgi_app.py

Specifically, update parse_lang_header (or its usage) to return the negotiated locale instead of langs[0].


Notes / Gotchas

  • Browser sends pt-BR, Babel expects pt_BR → normalization required
  • Some parts of the codebase may assume 2-letter codes (en, fr)
  • We should confirm the source of truth for SUPPORTED_LOCALES

Suggested Scope for PR

✅ Implement:

  • Accept-Language parsing (basic)
  • Babel negotiate_locale integration
  • Fallback to "en"

🚫 Defer:

  • Book language mapping
  • Wikipedia language selection
  • Perfect q-value handling (can be iterative)

Expected Outcome

Users get the best available UI language, not just their first preference truncated to 2 letters.


If you’d like to take this on, a good first step would be:

  • Identify where SUPPORTED_LOCALES is defined
  • Prototype replacing parse_lang_header with negotiate_locale

Happy to help review a focused PR on just this slice.

@tfmorris, this is my attempt at throwing notes together, I haven't researched this deeply. How well does this map to what you are expecting?

mekarpeles avatar Apr 08 '26 06:04 mekarpeles

How well does this map to what you are expecting?

@mekarpeles That looks good to me. It would be useful to include a test specifically for the fallback case, so that a user with their Accept-Language set to nl;fr, ends up selecting French, not the English fallback (in addition to the locale fallback for pt_BR that you mentioned).

(This likely already exists somewhere in our i18n config.)

It looks like the list is hardcoded in this Python function, but I'm not sure if there's an easy way to access it from Javascript.

https://github.com/internetarchive/openlibrary/blob/4625b575a131bba7a3f655e65a2b02b1b11cd443/openlibrary/plugins/openlibrary/code.py#L1249-L1265

It's accessed from HTML via this template:

https://github.com/internetarchive/openlibrary/blob/4625b575a131bba7a3f655e65a2b02b1b11cd443/openlibrary/templates/languages/language_list.html#L4

tfmorris avatar Apr 08 '26 19:04 tfmorris