Skip to content

feat(campaigns): localized strings as campaign messages - #354

Open
jeroenrinzema wants to merge 2 commits into
mainfrom
feat/campaign-messages
Open

jeroenrinzema wants to merge 2 commits into
mainfrom
feat/campaign-messages

Conversation

@jeroenrinzema

@jeroenrinzema jeroenrinzema commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A campaign keeps one template and its messages: the strings by key, then by locale. Translating stops meaning a copy of the whole template per locale.

{
  "subject": { "en": "Tomorrow: DevPulse {campaign.title}", "nl": "Morgen: DevPulse {campaign.title}" },
  "manage":  { "en": "Your sign-ups, to change or cancel: {url}", "nl": "Je aanmeldingen, om te wijzigen of af te melden: {url}" }
}
function Email({ user, campaign, t }) {
  return <Text>{t("manage", { url: <Link href={campaign.account_url}>devpulse.nl/account</Link> })}</Text>
}

Subject: {{ messages.subject }}.

How it's stored and defined

  • Where: campaigns.messages JSONB NOT NULL DEFAULT '{}', on the campaign rather than the template, so every template of the campaign (and a per-locale override) shares one set. Exposed as Campaign.messages / UpdateCampaign.messages (CampaignMessages); an update replaces the map, leaving it out keeps it.
  • Shape: key first ({key: {locale: string}}). A key with its languages side by side is what you edit and review; a missing translation is a missing entry right there.
  • Keys: ^[a-z][a-z0-9_]*(\.[a-z0-9_]+)*$ (dots group, footer.line), validated on update (400). Locales are the project's locale keys.
  • Placeholders: {path}. Filled from the vars passed to t, else the render data ({campaign.title}, {user.data.name}); missing → "". Whole sentences with placeholders, never glued fragments.

Flow at send time

flowchart LR
  U["user.locale nl-BE"] --> C["chain: nl-BE, nl, project default, its base"]
  C --> T["template: first in chain, else first"]
  C --> M["messages: per key, first locale in chain"]
  M --> P["React props: locale, messages, t built by the renderer"]
  M --> L["Liquid data: messages with placeholders filled"]
Loading
  • internal/render/messages.go: LocaleChain, ResolveLocale, ResolveMessages, FillPlaceholders, Localize (returns the React props and the Liquid data).
  • campaigns_render.go: the one campaign render path (journeys, broadcasts, inbox, API sends) uses the chain for the template and the messages.
  • renderer/compiler.ts: createTranslator builds t from props.messages; an element value (a <Link/>) comes back as parts JSX renders in place.
  • SendTest: resolves messages like a real send (locale from props.locale, props.user.locale, else the template's) unless the props carry them.
  • Docs: campaigns/localization.mdx; templates/create pages point to it.

Templates without messages render as before; template choice gains only the base-language step.

Not in this PR (proposed next)

  • Project-wide messages (footer line, button labels) merged under each campaign's, and shared layouts/components: the bigger duplication in practice is the frame copied into every campaign.
  • Plurals (ICU {count, plural, …}); v1 is plain placeholders.
  • A Liquid subject preview in the console.

Console: #355 (data panel tab). luno: cloudproud/lunogram.studio#4 (messages.yaml).

Evidence

  • go test ./internal/render/ (chain, resolution, placeholders, Liquid map), go test ./internal/store/management -run Campaign, go test ./internal/http/controllers/v1/management -run 'Campaign|SendTest', go test ./internal/pubsub/consumer -run 'Campaign|SelectTemplate|BuildRenderData': pass (Postgres via testcontainers).
  • deno test --no-check --allow-env in renderer/: 5 pass (--no-check: compiler.ts:89 pixelBasedPreset has a type error on main already).
  • End to end on a local stack (this branch's server and renderer, the console branch, logger provider), a test send for a user with locale nl and campaign messages logged:
    subject: Morgen: DevPulse 
    html: … Tot morgen. De deuren gaan om  open. …
    
    (empty values: the preview props had no campaign variables).

Merge Danger

Door: two-way (an additive column; dropping it loses only messages written since)

Blast Radius: campaign-rendering

Every send now lists the project's locale keys once (one small query). Two templates with the same locale: the first now wins (before: the last).

A campaign carries its strings by key and locale; templates read them for the recipient's locale.
A campaign keeps one template and its strings per key and locale. Sends resolve them along the locale chain (locale, base language, project default): React Email templates get t(key, vars), locale and messages; Liquid fields get {{ messages.<key> }}. Templates are chosen along the same chain.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant