Back

gt@2.11.3

Ernest McCarter avatarErnest McCarter
gt-clideriveobjectsarrays

Overview

derive() now supports object and array access. Previously, derive() could only resolve function calls — now it can resolve values from objects, arrays, and dictionaries, making it easier to work with lookup patterns.

This builds on the derive() in tagged templates release from gt-react@10.15.0.

What's new

Object access

Use derive() with object property lookups. The CLI statically resolves all possible values at extraction time:

const statusMessages = {
  success: "Your order has been placed.",
  pending: "Your order is being processed.",
  error: "Something went wrong with your order.",
} as const;

export default function OrderBanner({ status }: { status: string }) {
  return <p>{t`Thanks for shopping! ${derive(statusMessages[status])}`}</p>;
}

The CLI extracts a separate translation for each possible value:

  • "Thanks for shopping! Your order has been placed."
  • "Thanks for shopping! Your order is being processed."
  • "Thanks for shopping! Something went wrong with your order."

Both static (statusMessages.success) and dynamic (statusMessages[status]) access are supported.

Array access

Arrays work the same way:

const steps = [
  "Create your account",
  "Verify your email",
  "Start your first project",
] as const;

export default function Onboarding({ step }: { step: number }) {
  return <h2>{t`Step ${step + 1}: ${derive(steps[step])}`}</h2>;
}

Extracted entries:

  • "Step {step + 1}: Create your account"
  • "Step {step + 1}: Verify your email"
  • "Step {step + 1}: Start your first project"

Nested access

Chain object and array access to any depth:

const content = {
  greetings: {
    formal: "Good morning.",
    casual: "Hey!",
  },
} as const;

// derive(content[category][style]) works

Python support

The same patterns work in Python with t(). Use derive() as the first argument or inside an f-string:

badges = {
    "admin": "Administrator",
    "mod": "Moderator",
    "user": "Member",
}

# Standalone derive
t(derive(badges[role]))

# derive inside f-string for surrounding context
t(f"Logged in as {derive(badges[role])}")

Extracted entries:

  • "Logged in as Administrator"
  • "Logged in as Moderator"
  • "Logged in as Member"