Strings
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
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>
<script setup lang="ts">
// useTenon() reads in the route's locale.
const { data: strings } = await useTenon().getGlobal("strings", { fallback: false });
</script>
<template>
<NuxtLink :to="`/posts/${post.slug}`">{{ strings?.readMore }}</NuxtLink>
</template>
import { Link } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/react-start";
import cms from "../../tenon.config";
// Call it in a route's loader with the page's locale.
export const getStrings = createServerFn()
.validator((locale: string) => locale)
.handler(({ data: locale }) => cms.getGlobal("strings", { locale, fallback: false }));
<Link to="/posts/$slug" params={{ slug: post.slug }}>
{strings.readMore}
</Link>;
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:
"strings".Build your own
strings() returns the complete localized global. Spread it to change the label or the place in the admin:
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" },
},
});