Jakub Zych 7e64224eaf feat: plugin.yaml manifest for the SummerCMS marketplace
- plugin.yaml generated by summer plugin:manifest (name override: Translate)
- TestPluginManifest guards it with kiosk.CheckT
- require SummerCMS v0.6.0, the framework minimum in plugin.yaml
2026-10-29 19:26:41 +01:00

golem15.translate

The shared model-translation plugin for SummerCMS applications: request locale resolution, enabled Locale rows, and the tables other plugins use to store translated attributes.

import _ "git.golem15.com/golem15/sm-translate-plugin"

Module git.golem15.com/golem15/sm-translate-plugin, package translate, plugin ID golem15.translate.

Overview

The plugin is a compiled SummerCMS plugin. Its init registers it with the framework, so an application loads it by listing it in summer.yaml; summer build then emits the blank import above into the application's plugins.gen.go.

It owns golem15_translate_locales, golem15_translate_attributes, golem15_translate_indexes and an empty golem15_translate_messages table. Boot publishes a surf.LocaleResolver so the host router can resolve a validated locale onto the request context. Other plugins store translated model fields through classes.Translatable, classes.Translated, classes.SetTranslated, classes.WithLocale and classes.WhereIndex. It is a core plugin shared by several applications, so its table names, migration IDs, plugin ID and config keys are a contract: change them only in a backwards-compatible way.

Features

  • Locale rows. The Locale model maps golem15_translate_locales with no timestamps. Fillable fields are code, name and is_enabled. is_default and sort_order are not fillable.
  • Locales admin. Permissioned YAML CRUD at controller golem15.translate.locales, gated by golem15.translate.manage_locales only. The list sorts by sort_order ascending. A crafted body cannot persist is_default or sort_order; the permissioned makeDefault record action atomically leaves exactly one enabled default. Messages catalogue admin and drag-reorder are out of scope. Locales opens from the Settings page (category Translate) through a settings entry that links to the controller, not from the main navigation, as in the PHP plugin.
  • Phrasebooks. Embedded lang/en/lang.yaml and lang/pl/lang.yaml hold plugin and Locales UI strings. They are independent of golem15_translate_attributes.
  • Schema. Five gormigrate steps create the four tables (with the PHP pin's columns and indexes) and seed enabled en (default, sort_order 1) and enabled pl (Polski, sort_order 2). Messages is created empty; there is no Messages admin in this plugin yet. AutoMigrate is not used.
  • Request locale. classes.Translator implements surf.LocaleResolver. Front-end resolution is URL prefix, then a valid principal preferred_locale, then the remembered golem15.translate.locale cookie, then Accept-Language only when browser detection is enabled and the locale_manually_set flag cookie is absent, then the enabled default. Every candidate is checked against enabled Locale rows. Translator.Locale reads towel.Locale from context; there is no process-wide current locale.
  • API locale. Translator.ResolveAPI uses principal preferred_locale, then Accept-Language, then default, and does not persist cookies.
  • URL prefix. A first path segment that is an enabled locale is stripped before routing and sets the manual-selection flag cookie. The flag value is 1; it is never a locale code.
  • Locale cookies. The remembered-locale and flag cookies are set with Path=/, HttpOnly and SameSite=Lax. They carry Secure when the request arrived over HTTPS: a TLS connection, or X-Forwarded-Proto: https from an immediate peer listed in the framework's http.trusted_proxies. The header is ignored from any other client, and plain-HTTP requests get no Secure, so local development over http:// keeps working.
  • Model translations. A host model implements Translatable, TranslatableIndexes and MorphName. Default-locale values stay on the host columns. Each non-default locale is one JSON object row in golem15_translate_attributes. Indexed fields are mirrored to golem15_translate_indexes. MorphName must be an import-stable string (the PHP class name on a ported model); it is never reflect.Type.String().

Usage

Mount the plugin in an application as a git submodule at plugins/golem15/translate:

git submodule add git@git.golem15.com:golem15/sm-translate-plugin.git plugins/golem15/translate

Add it to the application's go.work:

use (
	.
	./plugins/golem15/translate
)

Require it in the application's go.mod with a local replace:

require git.golem15.com/golem15/sm-translate-plugin v0.0.0

replace git.golem15.com/golem15/sm-translate-plugin => ./plugins/golem15/translate

List it in summer.yaml and run summer build:

module: git.golem15.com/golem15/acme
binary: acme
plugins:
  - id: golem15.translate
    module: git.golem15.com/golem15/sm-translate-plugin

The plugin's own go.mod replaces the framework with ../summercms.go for a sibling checkout next to summercms.go. A host application that mounts the plugin at plugins/golem15/translate should replace the framework from the host go.work.

Requires() is empty: user-preference lookup is optional and uses bouncer.Principal when a guard has already run.

A blog post model declares translatable fields and an indexed slug:

func (Post) Translatable() []string        { return []string{"title", "slug", "content"} }
func (Post) TranslatableIndexes() []string { return []string{"slug"} }
func (Post) MorphName() string             { return `Acme\Blog\Models\Post` }

Default-locale writes go to the host column. Other locales go through SetTranslated. Translated falls back to the host column when a non-default value is missing. WithLocale validates an explicit locale onto a GORM handle without changing global state; WhereIndex looks up an indexed field in that locale and falls back to the host column when no index row exists.

_ = classes.SetTranslated(ctx, db, post, "title", "pl", "Witaj")
_ = classes.SetTranslated(ctx, db, post, "slug", "pl", "witaj")
title, _ := classes.Translated(ctx, db, post, "title", "pl")
q := classes.WhereIndex(classes.WithLocale(ctx, db.Model(post), "pl"), post, "slug", "witaj")

API reference

Identifier Description
translate.Plugin Compiled plugin; ID is golem15.translate.
translate.Plugin.LangFS Embedded English and Polish Locales admin phrasebooks.
translate.Plugin.Settings One settings entry, locales, linking to controller golem15.translate.locales and gated by golem15.translate.manage_locales. It uses the Controller field of pact.SettingsItem, so the plugin needs a framework release that has that field.
controllers.PermissionManageLocales golem15.translate.manage_locales; the only Locales admin permission.
models.Locale Locale row; TableName is golem15_translate_locales.
models.Locale.Fillable Allow-list: code, name, is_enabled.
models.Locale.Rules code and name are required.
models.All Models registered by this plugin.
models.Attribute Internal attribute JSON row; TableName is golem15_translate_attributes.
models.Index Internal indexed value row; TableName is golem15_translate_indexes.
updates.All gormigrate steps registered by this plugin.
classes.Translatable Host-model contract: Translatable, TranslatableIndexes, MorphName.
classes.Translated Reads a field in an explicit locale, falling back to the host column.
classes.TranslatedExact Reads a field without fallback so empty translations stay empty.
classes.SetTranslated Writes a field in an explicit locale; indexed fields upsert the index row.
classes.WithLocale Returns a GORM handle carrying a validated content locale on context.
classes.ContentLocale Reads the locale WithLocale stored on a GORM handle.
classes.WhereIndex Indexed equality lookup in the WithLocale locale, with host-column fallback.
classes.ErrUndeclaredField Field is not in Translatable().
classes.ErrDisabledLocale Locale code is missing or not enabled.
classes.ErrMissingPrimaryKey Host model has no persisted primary key.
classes.ErrEmptyMorphName MorphName is empty.
classes.ErrMissingContentLocale WhereIndex was used without WithLocale.
classes.Translator Context-only request locale resolver.
classes.NewTranslator Constructs a Translator from *gorm.DB and *backpack.App.
classes.Translator.Locale Returns the locale on ctx, or the enabled default.
classes.Translator.Resolve Front-end resolver implementing surf.LocaleResolver.
classes.Translator.Rewrite Strips an enabled locale prefix before routing.
classes.Translator.ResolveAPI Stateless API chain without persistence.

Configuration

Keys live under golem15.translate, from config/config.yaml:

Key Default Controls
forceDefaultLocale empty When set, that code is the default locale.
prefixDefaultLocale true Whether generated paths prefix the default locale (reserved for later path helpers).
disableLocalePrefixRoutes false When true, URL prefixes are not stripped.
browserDetection.enabled true Accept-Language detection for visitors without a remembered locale or manual flag.
browserDetection.manualSelectionCookie locale_manually_set Flag cookie name; value is 1.
browserDetection.manualSelectionExpiry 525600 Flag and remembered-locale cookie lifetime in minutes.

Dependencies

  • SummerCMS modules: backpack, bouncer, cabana, compass (tests), pact, party, surf, towel, lagoon (tests).
  • Third-party: gorm.io/gorm, github.com/go-gormigrate/gormigrate/v2. Test-only: github.com/testcontainers/testcontainers-go.
  • Standard library: bytes, context, encoding/json, embed, errors, fmt, io, io/fs, net/http, net/http/httptest, os, path/filepath, reflect, regexp, sort, strconv, strings, sync/atomic, testing, time.

Testing

go test ./...

Locales admin smokes start Postgres even under -short. Translatable fixture tests skip under -short.

Description
No description provided
Readme 218 KiB
Languages
Go 100%