Multi languages application

Hello, I was wondering if there was a way to make a multilanguage application with panel

We can change the theme light/dark of an application with the switch button, but what would be the best way to translate labels using a selector widget en/fr/de…

No easy way AFAIK. I imagine you would have a dict, and have each widget’s label/value bound to that.

{"en": {"widget_1": "Hello"}, "fr": {"widget_1": "..."}, ...}

Hi @xavArtley

Glad to hear from you :+1:

The Parameters of widgets and other Panel components can take references like bound functions, dependent functions/ methods and reactive expressions. You might use that to assign dynamic values.

Lets use reactive expressions (.rx) here:

import panel as pn
import param

TRANSLATIONS = {
    "en": {
        "greeting": "Hello",
        "farewell": "Goodbye",
        "thank_you": "Thank you",
    },
    "fr": {
        "greeting": "Bonjour",
        "farewell": "Au revoir",
        "thank_you": "Merci",
    },
    "de": {
        "greeting": "Hallo",
        "farewell": "Auf Wiedersehen",
        "thank_you": "Danke",
    }
}

def _get_translation(language, key):
    return TRANSLATIONS.get(language).get(key)

class State(param.Parameterized):
    language = param.Selector(objects=list(TRANSLATIONS))

    def translation(self, key):
        return self.param.language.rx.pipe(_get_translation, key)
        
state=State()

language = pn.widgets.RadioButtonGroup.from_param(state.param.language, button_type="primary", button_style="outline")

greeting = pn.widgets.Button(name=state.translation("greeting"))
thank_you = pn.widgets.Button(name=state.translation("thank_you"))

pn.Column(
    language, greeting, thank_you
).servable()

If you prefer bound functions you can replace with

def translation(self, key):
        return pn.bind(_get_translation, self.param.language, key)
1 Like