Sub topic 6
Templates and localization
This chapter assumes you know how events reach a channel worker, as covered in the ingestion and queue chapters. It also assumes you can read short Python snippets. Here you will see how a worker turns an event into the text a user reads.
Why templates exist
Producers should send facts, not finished messages. An event such as order_shipped carries an order id and a carrier name. The notification system owns the wording, so copy changes do not need a deploy in every producing service.
A template is a message body with named placeholders. The worker fills the placeholders from the event payload. One notification type usually has one template per channel, because each channel has different limits.
| Channel | Typical template parts | Constraint |
|---|---|---|
| Push | title, body | Short, roughly 100 characters visible |
| SMS | body | 160 characters per segment in GSM-7, fewer with non-Latin characters |
| subject, HTML body, text body | Needs a plain-text fallback | |
| In-app | title, body, deep link | Rendered by the client |
Rendering safely
Rendering must fail loudly when a variable is missing. Sending "Your order {order_id} shipped" to a customer is worse than sending nothing. The example below uses Python's standard library and raises an error on missing keys.
from string import Template
def render(template_text, payload):
# substitute() raises KeyError if a placeholder has no value
return Template(template_text).substitute(payload)
text = render(
"Order $order_id has shipped with $carrier.",
{"order_id": "A-77", "carrier": "DHL"},
)
print(text)For HTML email, escape every payload value before inserting it. A payload field such as a user-supplied name can contain markup. Use a template engine with auto-escaping, such as Jinja2 with autoescape=True, rather than plain string substitution.
Localization
Localization means choosing the template in the user's language. Store a locale on the user profile, such as de-DE. Look up the template by type, channel and locale.
Always define a fallback chain. If de-AT is missing, try de, then the default locale en. Without a chain, one missing translation silently blocks a notification.
def pick_template(templates, type_, channel, locale, default="en"):
candidates = [locale, locale.split("-")[0], default]
for loc in candidates:
found = templates.get((type_, channel, loc))
if found:
return found
raise LookupError(f"no template for {type_}/{channel}")
templates = {
("order_shipped", "push", "en"): "Order $order_id has shipped.",
("order_shipped", "push", "de"): "Bestellung $order_id wurde versendet.",
}
print(pick_template(templates, "order_shipped", "push", "de-AT"))Common errors
- Hardcoding dates and numbers. Format them per locale, for example with the Babel library, since
1,234.50and1.234,50are both valid. - Building sentences by joining fragments. Word order differs between languages, so translate whole sentences with placeholders.
- Ignoring plurals. Some languages have more than two plural forms. Use a message format that supports plural rules, such as ICU MessageFormat.
- Forgetting time zones. Render times in the user's zone, not the server's.
- Editing live templates without versions. Store a version number and record it with each send so you can trace a bad message.
Exercise
A German user has locale de-CH, and only de and en templates exist for a push notification. Which template is used, and why should the lookup raise an error if even en is missing?
Show answer
The de template is used, because the fallback chain tries de-CH, then de, then the default en. If no template exists at all, raising an error makes the failure visible and lets the retry or dead-letter path handle it, instead of sending an empty or wrong message.