Dev Helpers
← Back

Namespace dictionary keys for a menu

Namespace dictionary keys for a menu. Build the result in an App Router server page and send the rendered entries to the client.

Open this example in the interactive library

Source code

// app/namespace-dictionary-keys-for-a-menu/page.tsx
export const metadata = { title: "Namespace dictionary keys for a menu" };
export default function Page() {
  const rows = [
    { key: "home", en: "Home", fr: "Accueil" },
    { key: "save", en: "Save", fr: "Enregistrer" },
    { key: "help", en: "Help", fr: "" },
  ];
  const result = Object.fromEntries(rows.map((r) => ["nav." + r.key, r.en]));
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Namespace dictionary keys for a menu"}</h2>
      {entries.length ? (
        <dl>
          {entries.map(([key, value]) => (
            <div key={key}>
              <dt>{key}</dt>
              <dd>
                {typeof value === "string" ? value : JSON.stringify(value)}
              </dd>
            </div>
          ))}
        </dl>
      ) : (
        <p>No matching entries.</p>
      )}
    </section>
  );
}