Translations
Write your app in one language and ship it in several. Mark text with _() (or ngettext() for counts), in Python and in markup alike; translators fill in standard gettext .po files.
from pyweb import App, _, ngettext
app = App(title="Shop", locales=["en", "de", "fr"])
@app.page("/")
def Home():
count = 3
def add():
count += 1
<h1>{_("Welcome")}</h1>
<p>{ngettext("{n} item in your cart", "{n} items in your cart", count)}</p>
<button onclick={add}>{_("Add one")}</button>What this compiles to
// Home.js
import { gettext as $_, ngettext as $ngettext } from "./i18n.js";
function Home($s) {
const count = $signal("count" in $s ? $s["count"] : 3, "count");
function add() {
count($py.add(count(), 1));
}
return [
$h("h1", null, () => [$t($py.text($_("Welcome")))]),
$h("p", null, () => [$dyn(() => $ngettext("{n} item in your cart", "{n} items in your cart", count()))]),
$h("button", {"onclick": add}, () => [$t($py.text($_("Add one")))])
];
}
$mount("Home", Home);page Home route=/
browser count reactive state: assigned in add(); literal initial value
browser add event handler (compiled to JavaScript)<h1></h1>
<p></p>
<button></button>ngettext picks the right form for the number in each language (Russian has three, Arabic six) and puts the number where {n} is.
#The catalogs
pyweb i18n extract # every _() and ngettext() string -> locales/messages.pot
pyweb i18n init de # locales/de/messages.po, ready to translate
pyweb i18n extract # later: adds new strings to every language's .po
pyweb i18n check # what's translated, missing or unused (--strict fails CI on missing)A messages.po is plain text that any translator or translation service reads:
msgid "Welcome"
msgstr "Willkommen"
msgid "{n} item in your cart"
msgid_plural "{n} items in your cart"
msgstr[0] "{n} Artikel im Warenkorb"
msgstr[1] "{n} Artikel im Warenkorb"A string without a translation shows as written. Entries marked #, fuzzy are ignored until a translator confirms them. Changes to a .po file are picked up on the next request.
#Which language
For each request: the lang cookie when it names one of App(locales=[...]), else the best match in the browser's Accept-Language, else default_locale (the first of locales). Let people choose:
from pyweb import server
from pyweb.i18n import set_locale
@server
def choose_language(lang: str) -> bool:
set_locale(lang) # this request, and a cookie for the next ones
return Truepyweb.i18n.get_locale() says which one is in effect. The page's <html lang> follows it, and dir="rtl" is set for Arabic, Hebrew, Persian and Urdu.
#In the browser
Markup and handlers that run in the browser translate with the same catalog: each page is sent the translations of the strings its browser code uses (and nothing else), and plurals use the browser's own rules for the language. Pass literal strings to _() and ngettext() so pyweb i18n extract and the compiler can find them; for a value inside, use {name} and .replace("{name}", value).
#Numbers, money and dates
from pyweb.i18n import currency, date, number
number(1234567.5, 2) # "1,234,567.50" in English, "1.234.567,50" in German
currency(9.99, "EUR") # "€9.99", "9,99 €"
date(order.created_at) # "Oct 28, 2026", "28.10.2026"They format on the server for the current language (common languages are built in); compute the text in the page body or a server function and show it in markup.