Konnichiwa, Streamlit

Nate Jensen
3 min readJun 19, 2022

--

Localizing your Python web app for global audiences

The Python-based web app framework Streamlit has been gaining considerable interest in the past couple of years. Being able to localize the app is important for a user base that spans more than one language locale. While Streamlit does not provide native localization capabilities, the elegance of the framework still makes internationalization (I18N) and localization (L10N) a breeze.

Source: Google Trends (https://trends.google.com/trends/explore?date=today%205-y&geo=US&q=streamlit)

Getting started

Let’s start by creating a Python file for our Streamlit app. Name the file app.py:

import streamlit as stdef main():
st.subheader("Hello, world.")
return
if __name__ == "__main__":
main()

and from the command line, start the app:

streamlit run app.py

You should see something like the following:

Localization bundle

Let’s say the above greeting might need to be displayed in another language. The easiest and perhaps most common approach for managing different text strings is to capture and maintain the text strings in a text bundle. Create a file named text_bundle.csv and add the following:

locale,key,value
en_US,greeting,"Hello, World"
ja_JP,greeting,こんにちは世界

(Note the string value containing a comma is escaped with double quotes.)

Bundle file loader

Now that we know what our text bundle looks like, we need to create a function to load in the file and filter the key values for the given language locale:

def load_bundle(locale):
# Load in the text bundle and filter by language locale.
df = pd.read_csv("text_bundle.csv")
df = df.query(f"locale == '{locale}'")
# Create and return a dictionary of key/values.
lang_dict = {df.key.to_list()[i]:df.value.to_list()[i] for i in range(len(df.key.to_list()))}
return lang_dict

Language select option

Let’s create a radio button to select the language locale. Right underneath the main() function create a dictionary called lang_options that holds the human-readable locale names and the ISO locale values.

def main():
lang_options = {
"English (US)":"en_US",
"日本語":"ja_JP"
}
locale = st.radio(label='Language', options=list(lang_options.keys()))st.subheader(lang_dict['greeting'])
return

Dynamically loading locale key/value pairs

The locale selected with the radio button can be used for loading a dictionary of label key/values. Directly underneath the st.radio() function call, add a new load to load in the dictionary:

# Note we use the selected human-readable locale to get the relevant 
# ISO locale code from the lang_options dictionary.
lang_dict = load_bundle(lang_options[locale])

When we reload the Streamlit app, we should now see a radio button that allows us to select between languages. The default choice in this example is English US because that is the first key/value in the lang_options dictionary.

Wrapping it up

This article shows how to implement basic label display of key/value pairs. Of course, localization is more involved than that considering how time zones, calendar display, currencies and lots more can differ across locales, but hopefully this simple tutorial will get you up and running with multi-language support for your Streamlit app.

import streamlit as st
import pandas as pd
def load_bundle(locale):
# Load in the text bundle and filter by language locale.
df = pd.read_csv("text_bundle.csv")
df = df.query(f"locale == '{locale}'")
# Create and return a dictionary of key/values.
lang_dict = {df.key.to_list()[i]:df.value.to_list()[i] for i in range(len(df.key.to_list()))}
return lang_dict
def main():
lang_options = {
"English (US)":"en_US",
"日本語":"ja_JP"
}
locale = st.radio(label='Language', options=list(lang_options.keys()))# Note we use the selected human-readable locale to get the relevant
# ISO locale code from the lang_options dictionary.
lang_dict = load_bundle(lang_options[locale])
st.subheader(lang_dict['greeting'])returnif __name__ == "__main__":
main()

--

--