Add page_lang parameter to st.set_page_config#16024
Add page_lang parameter to st.set_page_config#16024atharv-sys32 wants to merge 3 commits intostreamlit:developstreamlit/streamlit:developfrom atharv-sys32:feat/page-lang-configatharv-sys32/streamlit:feat/page-lang-configCopy head branch name to clipboard
Conversation
Add a page_lang parameter to st.set_page_config that sets the lang attribute on the root <html> element via PageConfig proto. Previously the value was hardcoded to "en", which caused unwanted browser translation prompts for non-English apps. Changes: - PageConfig.proto: add string lang = 7 field - page_config.py: add page_lang parameter, wire to protobuf - App.tsx: set document.documentElement.lang when config arrives Fixes streamlit#15726
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
|
Thanks for contributing to Streamlit! 🎈 Please make sure you have read our Contributing Guide. You can find additional information about Streamlit development in the wiki. The review process:
We're receiving many contributions and have limited review bandwidth — please expect some delay. We appreciate your patience! 🙏 |
|
| Filename | Overview |
|---|---|
| lib/streamlit/commands/page_config.py | Adds the public page_lang parameter and includes non-empty values in page configuration messages. |
| frontend/app/src/App.tsx | Applies the configured language to the root HTML element while preserving the existing value when omitted. |
| proto/streamlit/proto/PageConfig.proto | Adds the backward-compatible lang string field to PageConfig. |
Reviews (3): Last reviewed commit: "Fix: use falsy check for lang proto3 def..." | Re-trigger Greptile
| if page_lang is not None: | ||
| msg.page_config_changed.lang = page_lang |
There was a problem hiding this comment.
Invalid Language Tags Pass Through
Any non-None string, including "" or "not_a_language", is forwarded despite the documented BCP 47 requirement. The frontend then either ignores the empty value and leaves a stale language in place or assigns an invalid <html lang> value, so browsers and screen readers can continue using the wrong language.
| if (lang) { | ||
| document.documentElement.lang = lang | ||
| } |
There was a problem hiding this comment.
The condition if (lang) will fail for empty strings since empty strings are falsy in JavaScript. However, the Python backend at page_config.py:309-310 checks if page_lang is not None, which means empty strings will be sent to the frontend but not applied. This creates an inconsistency.
Fix by checking explicitly for null/undefined:
if (lang !== undefined && lang !== null) {
document.documentElement.lang = lang
}Or if empty strings should be rejected, add validation in the Python backend:
if page_lang is not None and page_lang != "":
msg.page_config_changed.lang = page_lang| if (lang) { | |
| document.documentElement.lang = lang | |
| } | |
| if (lang !== undefined && lang !== null) { | |
| document.documentElement.lang = lang | |
| } | |
Spotted by Graphite
Is this helpful? React 👍 or 👎 to let us know.
…ull check in frontend
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit f7e90f7. Configure here.
protobuf3 defaults unset string fields to "", which is falsy in JS. Using if (lang) correctly skips the empty proto default so the original index.html lang="en" (or a previously set value) is not overwritten. Python backend already rejects empty strings.
Describe your changes
Adds a
page_langparameter tost.set_page_config()that sets thelangattribute on the root<html>element. Previously this was hardcoded to"en", causing unwanted browser translation prompts for non-English apps and incorrect screen reader pronunciation.GitHub Issue Link (if applicable)
Fixes #15726
Testing Plan
Changes follow the exact same pattern as the existing
title/faviconhandling. Proto field added, Python backend wires it, frontend sets it.Note
Low Risk
Small, additive API and DOM attribute change with no auth or data-path impact; behavior matches existing page config patterns.
Overview
Adds
page_langtost.set_page_config()so apps can set the root<html>langattribute instead of relying on the staticenin the shell HTML.The value flows through a new
PageConfig.langproto field; Python forwards non-empty tags onpage_config_changed, and the frontend updatesdocument.documentElement.langwhenpageConfigChangedincludeslang. Docs describe BCP 47 tags and the same additive/inherit behavior as other page config options (defaultenwhen never set).Reviewed by Cursor Bugbot for commit 0e6b338. Bugbot is set up for automated code reviews on this repo. Configure here.