import gettext # Set up translations for English and Spanish translations = gettext.translation("myapp", "locales", languages=["en", "es"]) translations.install() # Use the _() function to mark strings for translation print(_("Hello, world!"))
from babel import gettext # Set up translations for English and Spanish translations = gettext.translation("myapp", "locales", languages=["en", "es"]) translations.install() # Use the _() function to mark strings for translation print(_("Hello, world!"))In this example code, the Babel library is used in a similar manner to the gettext library to translate the string "Hello, world!" into the appropriate language. The `gettext.translation()` function is used to load the appropriate translation files for the languages "en" and "es" from the directory "locales", and the `translations.install()` function is called to activate the translations. The `_()` function is used to mark the string literal for translation.