Presets

Strings

Translated short texts like "Read more", with defaults in code.

stringsPreset() adds a global for the short texts your templates would otherwise hardcode, like "Read more". Each key becomes a text field that editors translate on the Settings page.

Example

tenon.config.ts
import { defineCms } from "tenoncms";
import { stringsPreset } from "tenoncms/presets";

export default defineCms({
  // ...
  presets: [
    stringsPreset({
      readMore: "Read more",
      search: { en: "Search", de: "Suche" },
      close: { label: "Close button", default: "Close" },
    }),
  ],
});
  • A value is the text for every language, or an object with the text per language.
  • { label, default } also sets the label in the admin. The label defaults to the key.
  • The global is always localized.

A read returns the saved text, or your default when nobody changed it. Your site works before anything is saved:

const strings = await cms.getGlobal("strings", { locale: "de", fallback: false });
strings.search; // "Suche"

Read it with fallback: false when your defaults differ per language. Otherwise a text saved in the default language wins over your default for another language. See Localization.

Render it

Read the global once per page and pass it down.

---
const { tenon, locale } = Astro.locals;
const strings = await tenon.getGlobal("strings", { locale, fallback: false });
---

<a href={`/posts/${post.slug}`}>{strings.readMore}</a>

Change a default

A text that equals your default is not stored. When you change a default in code, every language that kept it shows the new text.

stringsPreset({ readMore: "Continue reading" }),

Options

stringsPreset(entries, options) takes the texts first:

One text field per key, with its default.The global name. Default "strings".

Build your own

strings() returns the complete localized global. Spread it to change the label or the place in the admin:

tenon.config.ts
import { defineCms } from "tenoncms";
import { strings } from "tenoncms/presets";

const buttons = strings({ readMore: "Read more", back: "Back" });

export default defineCms({
  // ...
  globals: {
    buttons: { ...buttons, label: "Buttons" },
  },
});