chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

Allow to change the language in the backend

Open lucasiscovici opened this issue 1 year ago • 6 comments

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] Browser language dependant

Describe the solution you'd like Allow to change the language in the backend (config)

Describe alternatives you've considered dirty hack

original_config_load_translation = cl.config.load_translation
cl.config.load_translation = lambda _: original_config_load_translation("fr-FR")

lucasiscovici avatar Apr 03 '24 15:04 lucasiscovici

+1 to the feature request. Would be great to dynamically detect or set the language of the current user_session.

@lucasiscovici I tried to incorporate your code (thanks for posting your workaround!) However, I think this might override the language for all users at once, server-side - if I understood correctly...

I tried to fetch the cl.user_session variables to detect the session or read a user language preference, but the function seems to miss some session context. Even trying to fetch it does not work :(

import chainlit as cl
from chainlit.context import context_var, ChainlitContext, ChainlitContextException

original_config_load_translation = cl.config.load_translation
# Stores the original load_translation function in original_config_load_translation.

def listening_config_load_translation(*args, **kwargs):
    loaded_language = args[0]
    
    # Ensure the context is set
    try:
        context = context_var.get()
    except LookupError:
        print("Context not found. Cannot load user session.")
        return original_config_load_translation(*args, **kwargs)
    
    user = cl.user_session.get("user")
    cl.user_session.set("language", loaded_language)
    print(f"The language {loaded_language} was loaded for user {user}.")
    return original_config_load_translation(*args, **kwargs)

# WARNING: THIS SETS THE LANGUAGE FOR ALL CURRENT USERS!
def force_set_language(language_code="AUTO"):
    if language_code == "AUTO":
        cl.config.load_translation = listening_config_load_translation
    else:
        cl.config.load_translation = lambda _: listening_config_load_translation(language_code)

force_set_language("AUTO")

It just keeps printing "Context not found. Cannot load user session." even if the session should long be initialized (e.g. starting a new chat) Maybe you have any idea how to build on this hacky workaround to finally detect/set user languages

jannikh avatar Jun 17 '24 18:06 jannikh

+1 on localizing the user session, I have a multilingual group of end-users. Chainlit UI translation files are the only thing I cannot hot reload for the user. With pybabel.gettext I can dynamically reload all my python source strings.

GillesJ avatar Sep 03 '24 07:09 GillesJ

Is there any update on this? Any way to implement multi-lingual chatbot?

Rajat-TVSM avatar Jan 29 '25 17:01 Rajat-TVSM

+1 request

vund-clevai avatar May 12 '25 07:05 vund-clevai

+1

For my use-case (transcribing audio inputs), I don't need to change the language from the backend but at least need to be able to detect it. I see https://github.com/Chainlit/chainlit/issues/1141 mentions cl.user_session.get("languages"), but that returns None for me?

athewsey avatar Jul 25 '25 09:07 athewsey

+1 need to access user UI language from inside python code

kairntech avatar Sep 16 '25 08:09 kairntech