GPT-Brain/modules/language.py

30 lines
1010 B
Python
Raw Normal View History

import gettext
import streamlit as st
import modules.utilities as util
languages = util.read_json('.locals/languages.json', default_value={'SESSION_LANGUAGE': 'en_US'})
def select_language():
language_index = util.get_index(list(languages.values()), st.session_state['SESSION_LANGUAGE'])
# Add a language selector widget to the Streamlit app
language = st.sidebar.selectbox('Language', languages.keys(), language_index)
selected_lang = languages[language]
if st.session_state['SESSION_LANGUAGE'] != selected_lang:
st.session_state['SESSION_LANGUAGE'] = selected_lang
util.write_json({'SESSION_LANGUAGE': selected_lang}, '.user/language.json')
st.experimental_rerun()
def set_language():
# set current language
lang_translations = gettext.translation('base', localedir='.locals', languages=[st.session_state['SESSION_LANGUAGE']])
lang_translations.install()
# define _ shortcut for translations
_ = lang_translations.gettext
return _