Skip to main content

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

  1. The component uses the navigator.language property in the browser to detect the user's preferred language.
  2. The detected language is sent back to the Streamlit app, where you can use it to customize the content.
  3. 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

  1. Clone the repository:

    git clone https://github.com/your-repo/streamlit-browser-language-detector.git
    cd streamlit-browser-language-detector
    
  2. Navigate to the frontend directory and install dependencies:

    cd language_detection/frontend
    npm install
    
  3. Start the development server:

    npm start
    
  4. In a separate terminal, run the Streamlit app (set RELEASE to False for development in init.py):

    streamlit run example.py
    

Building for Production

  1. Build the React component:

    cd language_detection/frontend
    npm run build
    
  2. 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

  • Built with Streamlit and React.
  • Inspired by the need for multilingual support in Streamlit apps.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_browser_language-0.0.4.tar.gz (494.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_browser_language-0.0.4-py3-none-any.whl (498.5 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_browser_language-0.0.4.tar.gz.

File metadata

File hashes

Hashes for streamlit_browser_language-0.0.4.tar.gz
Algorithm Hash digest
SHA256 af709e074ddf014ab647dbf1c1b4bf3d267e8e07281d9d449c5159233a0e3436
MD5 1c327a42a848686238941466360ee484
BLAKE2b-256 601c570b160d066cd2c09e057eb48329d7a7a84939e92080a558b39afd26c2b2

See more details on using hashes here.

File details

Details for the file streamlit_browser_language-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_browser_language-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4d3e1390bc01d2ee7ea642f40d6d5f249c72f60c0fedac8d0c24e65a18e06ed5
MD5 4208e12b6ad9cded150ae0e67eec659c
BLAKE2b-256 b9efe11e52fe87530439702c90d4cd1ef856358efc148f6e1a5100c43b1c7793

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page