feat(mail): one admin record per mail template with a locale switcher

Gap G-35-1 (supersedes D-05), tracer slice:
- postcard: migration 202611130001_mail_template_base_code (base_code
  column and index); SyncIndex writes base_code; EnsureLocaleRows creates
  the <template>-<locale> rows of locales that ship no file; TemplateBase
  and Catalog.TemplateFor; send, preview and validation resolve a missing
  <template>-<locale> to the locale's customization, else the base's
  customization, else the base file. Names with their own file send as
  before.
- cabana: mail locales are the translator's plus the TranslationWriter's
  enabled locales; sync ensures the locale rows and re-syncs when the
  locale set changes; record lookups admit fallback-locale rows; the
  record title is the base code; RecordMeta.LocaleVariants
  (RecordLocaleVariant) on the show and update responses.
- admin: LocaleVariantSwitcher in the update form header, from
  meta.localeVariants, behind the unsaved-changes confirm.
- tests: TestMailOverrideSmoke/locale_fallback,
  TestMailScreensSmoke/locales, LocaleVariantSwitcher.test.ts.
This commit is contained in:
Jakub Zych
2026-11-13 20:29:42 +01:00
parent 9afe95ccef
commit 52c9c7eb18
22 changed files with 920 additions and 63 deletions

View File

@@ -240,14 +240,47 @@ func (c *Catalog) resolveLayoutLocked(t *parsedTemplate) error {
return nil
}
func (c *Catalog) lookupTemplate(name string) (*parsedTemplate, error) {
// localeTag matches the locale tag a fallback-locale name ends in: a
// two-letter language, optionally followed by a dash or an underscore and
// a 2-8 character alphanumeric subtag (de, pt-BR, zh_Hant).
var localeTag = regexp.MustCompile(`^[A-Za-z]{2}(?:[-_][A-Za-z0-9]{2,8})?$`)
// resolveTemplate is the lookup every send, preview and validation goes
// through. A name with its own file is that file. A name the catalog does
// not hold of the form <base>-<tag>, where <base> is a registered template
// and <tag> a locale tag, is the base's file: a locale that ships no file
// of its own sends the default-locale version. When several bases match,
// the longest one wins. Any other name is an unknown template.
func (c *Catalog) resolveTemplate(name string) (*parsedTemplate, error) {
c.mu.RLock()
defer c.mu.RUnlock()
t, ok := c.templates[name]
if !ok {
return nil, fmt.Errorf("postcard: unknown template %s", name)
if t, ok := c.templates[name]; ok {
return t, nil
}
return t, nil
if t := c.fallbackLocked(name); t != nil {
return t, nil
}
return nil, fmt.Errorf("postcard: unknown template %s", name)
}
// fallbackLocked returns the base template a fallback-locale name resolves
// to, or nil. The caller holds c.mu.
func (c *Catalog) fallbackLocked(name string) *parsedTemplate {
_, rest, ok := strings.Cut(name, "::")
if !ok {
return nil
}
// The tag holds no dot, so the dash is in the last dotted segment.
start := len(name) - len(rest) + strings.LastIndex(rest, ".") + 1
for i := len(name) - 1; i > start; i-- {
if name[i] != '-' || !localeTag.MatchString(name[i+1:]) {
continue
}
if t, ok := c.templates[name[:i]]; ok {
return t
}
}
return nil
}
// RegisterPartials loads declared partial names from fsys. Each name must
@@ -490,7 +523,7 @@ type renderSource struct {
// render renders msg.Template: the file version, or the file version with
// opts.overrides applied. Both go through renderFrom.
func (c *Catalog) render(msg Message, opts renderOptions) (RenderedMessage, error) {
t, err := c.lookupTemplate(msg.Template)
t, err := c.resolveTemplate(msg.Template)
if err != nil {
return RenderedMessage{}, err
}
@@ -869,6 +902,19 @@ func (c *Catalog) Template(name string) (TemplateFile, bool) {
return t.file(), true
}
// TemplateFor returns the file a template name renders from: the name's
// own file, or, for a <template>-<locale> name whose locale ships no file,
// the file of the base template (see the send-resolution rule in the
// package README). The returned Name is the base's in the second case,
// which is how a caller tells the two apart.
func (c *Catalog) TemplateFor(name string) (TemplateFile, bool) {
t, err := c.resolveTemplate(name)
if err != nil {
return TemplateFile{}, false
}
return t.file(), true
}
// Layout returns the registered layout of that full name.
func (c *Catalog) Layout(name string) (LayoutFile, bool) {
c.mu.RLock()
@@ -939,3 +985,15 @@ func TemplateLocale(name string, locales []string) string {
}
return best
}
// TemplateBase returns the name of the template a locale variant belongs
// to: name without the locale suffix TemplateLocale finds in it, or name
// itself when it has none. acme.blog::mail.welcome-en and
// acme.blog::mail.welcome share the base acme.blog::mail.welcome.
func TemplateBase(name string, locales []string) string {
locale := TemplateLocale(name, locales)
if locale == "" {
return name
}
return name[:len(name)-len(locale)-1]
}