Streamlit component that allows you to get browser language.
Project description
Streamlit Browser Language Detector
A Streamlit component that detects the user's browser language and allows you to customize your app's content based on the detected language.
Installation
You can install the component via pip:
pip install streamlit-browser-language
Usage
Basic Usage
To detect the user's browser language and display content accordingly:
import streamlit as st
from language_detection import detect_browser_language
# Detect the browser language
browser_language = detect_browser_language()
# Display content based on the detected language
if browser_language.startswith('es'):
st.write("¡Hola! Bienvenido a nuestra aplicación.")
elif browser_language.startswith('fr'):
st.write("Bonjour! Bienvenue dans notre application.")
else:
st.write("Hello! Welcome to our application.")
# Display the detected language for debugging
st.write(f"Detected browser language: {browser_language}")
Function Parameters
The detect_browser_language function has the following parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
timeout |
Union[int, float] |
5 |
Maximum time (in seconds) to wait for the language detection. |
interval |
Union[int, float] |
0.1 |
Interval (in seconds) to check for the language detection result. |
key |
str |
"browser_language" |
A unique key for the component to avoid conflicts. |
How It Works
- The component uses the
navigator.languageproperty in the browser to detect the user's preferred language. - The detected language is sent back to the Streamlit app, where you can use it to customize the content.
- The component is invisible and does not affect the layout of your app.
Example Output
If the user's browser language is set to es-ES (Spanish), the app might display:
¡Hola! Bienvenido a nuestra aplicación.
Detected browser language: es-ES
If the user's browser language is set to fr-FR (French), the app might display:
Bonjour! Bienvenue dans notre application.
Detected browser language: fr-FR
For all other languages, the app defaults to English:
Hello! Welcome to our application.
Detected browser language: en-US
Development
Setting Up the Development Environment
-
Clone the repository:
git clone https://github.com/your-repo/streamlit-browser-language-detector.git cd streamlit-browser-language-detector
-
Navigate to the
frontenddirectory and install dependencies:cd language_detection/frontend npm install
-
Start the development server:
npm start -
In a separate terminal, run the Streamlit app (set RELEASE to False for development in init.py):
streamlit run example.py
Building for Production
-
Build the React component:
cd language_detection/frontend npm run build
-
Package and distribute the component:
python setup.py sdist bdist_wheel twine upload dist/*
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or bug fixes.
Acknowledgments
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file streamlit_browser_language-0.0.4.tar.gz.
File metadata
- Download URL: streamlit_browser_language-0.0.4.tar.gz
- Upload date:
- Size: 494.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af709e074ddf014ab647dbf1c1b4bf3d267e8e07281d9d449c5159233a0e3436
|
|
| MD5 |
1c327a42a848686238941466360ee484
|
|
| BLAKE2b-256 |
601c570b160d066cd2c09e057eb48329d7a7a84939e92080a558b39afd26c2b2
|
File details
Details for the file streamlit_browser_language-0.0.4-py3-none-any.whl.
File metadata
- Download URL: streamlit_browser_language-0.0.4-py3-none-any.whl
- Upload date:
- Size: 498.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d3e1390bc01d2ee7ea642f40d6d5f249c72f60c0fedac8d0c24e65a18e06ed5
|
|
| MD5 |
4208e12b6ad9cded150ae0e67eec659c
|
|
| BLAKE2b-256 |
b9efe11e52fe87530439702c90d4cd1ef856358efc148f6e1a5100c43b1c7793
|