mkhorasani / Streamlit-Authenticator

A secure authentication module to validate user credentials in a Streamlit application.
Apache License 2.0
1.38k stars 229 forks source link

Add a way how to localize dialogs #66

Closed Ota-Sandr-MamaAI closed 5 months ago

Ota-Sandr-MamaAI commented 1 year ago

The users of my streamlit page protected by streamlit authenticator cannot speak English. So, I want to localize the login form. However, I did not find a way how to do it easily. When I looked into issue history, I found #1 and it looks like it was solved already. However, this does not work for me.

My current workaround looks like this:

import streamlit as st
import streamlit_authenticator as stauth

def _translate_elements(elements: list[tuple[str, str]]):
    css = ""
    for element in elements:
        css += element[0] + " { font-size: 0; }\n"
        css += element[0] + ":after { font-size: 1rem; content: '" + element[1] + "'; }\n"
    st.markdown("<style>\n" + css + "</style>", unsafe_allow_html=True)

authenticator = stauth.Authenticate(
    {"usernames": {}},
    "sessionid",
    "123456789",
    1
)

authenticator._check_cookie() # to ensure that st.session_state['authentication_status'] has proper value

# translate elements only when the login dialog is going to be shown
if st.session_state['authentication_status'] is not True:
    elements_to_translate = [
        ("div > div:nth-child(2) > div.stTextInput > label > div > p", "Localized username label"),
        ("div > div:nth-child(3) > div.stTextInput > label > div > p", "Localized password label"),
        ("div > div:nth-child(4) > div > div.stButton > button > div > p", "Localized login button")
    ]
    _translate_elements(elements_to_translate)

name, authentication_status, username = authenticator.login('Localized login form header', 'main')

This solution is not very safe because it heavily depends on the way how the streamlit generates HTML elements during page creation. So, it would be nice if streamlit authenticator has some way how to specify custom labels.

vidz1979 commented 1 year ago

Hey @Ota-Sandr-MamaAI, I've opened a P.R. for this case: https://github.com/mkhorasani/Streamlit-Authenticator/pull/68

Take a look at my repo if you want it quickly.

Ota-Sandr-MamaAI commented 1 year ago

Thanks @vidz1979 , this looks great.

mkhorasani commented 5 months ago

Hi @Ota-Sandr-MamaAI, this feature has been implemented in release v0.3.1. You can now customize the text of the fields/headers/buttons of all the widgets by passing your own labels to the fields parameter of each widget.