Skip to content

k.label / k.t / k.site.labels ​

Manage and resolve multilingual site labels

Execution ContextPrerequisitesTypical Use
All server-side scripts (View, Page, Layout, API)Configure site cultures in Admin; missing keys auto-create in k.t/k.labelMultilingual UI copy, {key} placeholder substitution, label CRUD

Kooboo provides two complementary label APIs: the quick culture-aware translation helpers k.t() / k.label() with placeholder substitution, and the site label repository k.site.labels for managing Label resources.

Quick Start ​

js
// Resolve and format a label for the active culture (auto-creates if missing)
const title = k.t("Cancel")
const greeting = k.label("Welcome {name}", { name: "Alice" })

// Read or manage labels via k.site.labels without auto-creation
const exact = k.site.labels.getValue("Cancel")

Quick Reference ​

Method / PropertyDescriptionReturns
k.t(value[, params])Resolve active culture label (supports {key} placeholders; auto-creates)string
k.label(NameOrId[, params])Alias helper executing the same lookup as k.t()string
k.site.labels.all()Retrieve all label objects in the siteLabel[]
k.site.labels.get(nameOrId)Retrieve a single Label object by name or IDLabel | null
k.site.labels.getValue(nameOrId[, params])Read translated text without auto-creating missing entriesstring | null
k.site.labels.getOrAdd(nameOrId, DefaultValue)Read a label or create it with DefaultValue when missingLabel
k.site.labels.add(...)Add or update label values (by active or explicit culture, or plain object { name, ... })void
k.site.labels.update(...)Update an existing label value (by args, Label, or plain object)void
k.site.labels.delete(nameOrId)Delete a label by name or IDvoid

Difference from global t()

The bare global function t(key, params?) only reads existing labels and returns undefined when missing without creating a label, whereas k.t() and k.label() auto-create missing entries and return a string.

Prerequisites ​

  1. Configure site languages under Site Settings → Basic Settings → Basics → Multilingual (such as en and fr).
  2. Manage label translations under Content → Labels (see Admin: Labels).

For example, for a label named Cancel:

  • en: Cancel
  • fr: Annuler

k.t() / k.label() ​

At runtime, k.t() and k.label() run the same translation lookup: they resolve text for the current request culture, replace {key} placeholders, and automatically create a missing label using the passed key as its initial value under the active request culture.

ParameterTypeRequiredDescription
value / NameOrIdstringYesLabel name (value on k.t, NameOrId on k.label)
paramsRecord<string, any>NoPlaceholder substitution values

Returns: string — Translated text for the active culture.

js
// Read an existing label
const text = k.t("Cancel")
const text2 = k.label("Cancel")

// Placeholder substitution
const text3 = k.t("Items: {count}", { count: 123 })
const text4 = k.label("Welcome {name}", { name: "Alice" })

k.site.labels Methods ​

all() ​

Retrieve all labels configured on the site.

Parameters: None.

Returns: Label[].

js
const labels = k.site.labels.all()

get() ​

Retrieve a single Label object (containing multilingual values) by name or ID.

ParameterTypeRequiredDescription
nameOrIdanyYesLabel name or ID

Returns: Label | null, null when missing.

js
const label = k.site.labels.get("Cancel")

getValue() ​

Read the translated text for the active culture (with optional placeholder formatting) without auto-creating missing labels.

ParameterTypeRequiredDescription
nameOrIdanyYesLabel name or ID
paramsRecord<string, any>NoPlaceholder substitution map

Returns: string | null, null when missing.

js
const plain = k.site.labels.getValue("Cancel")
const formatted = k.site.labels.getValue("Welcome {name}", { name: "Alice" })

getOrAdd() ​

Get an existing label or create one with DefaultValue for the current culture when missing.

ParameterTypeRequiredDescription
nameOrIdanyYesLabel name or ID
DefaultValuestringYesDefault text stored if created

Returns: Label.

js
const cancelLabel = k.site.labels.getOrAdd("Cancel", "Cancel")

add() ​

Create or set a label value.

OverloadDescription
add(name, value)Add or update label for the active request culture
add(name, value, culture)Add or update label for an explicit culture (such as "en")
add(SiteObject)Pass a plain object with a name field and culture keys (e.g. { name: "Cancel", en: "Cancel", zh: "取消" }); do not pass a Label object returned by get() (MultilingualRepository.Add does not special-case MultilingualObject, so name becomes null)

Returns: void.

js
k.site.labels.add("demo_key", "Initial value")
k.site.labels.add("demo_key", "Valeur initiale", "fr")
k.site.labels.add({ name: "demo_key", en: "Initial value", fr: "Valeur initiale" })

update() ​

Update an existing label (supports (name, value), (name, value, culture), and (SiteObject)). Unlike add(SiteObject), update(SiteObject) accepts either a Label object returned by get() or a plain object with name and culture keys.

Returns: void.

js
k.site.labels.update("demo_key", "Updated value")
k.site.labels.update("demo_key", "Valeur mise à jour", "fr")

delete() ​

Delete a label by name or ID.

ParameterTypeRequiredDescription
nameOrIdanyYesLabel name or ID

Returns: void.

js
k.site.labels.add("demo_key", "Initial value")
const v = k.site.labels.getValue("demo_key")
k.site.labels.update("demo_key", "Updated value")
k.site.labels.delete("demo_key")

TypeScript Definitions ​

ts
interface k {
  t(value: string): string;
  t(value: string, params: Record<string, any>): string;
  label(NameOrId: string): string;
  label(NameOrId: string, params: Record<string, any>): string;
}

interface LabelRepository {
  all(): Label[];
  get(nameOrId: any): Label;
  getValue(nameOrId: any): string;
  getValue(nameOrId: any, params: Record<string, any>): string;
  getOrAdd(nameOrId: any, DefaultValue: string): Label;
  add(name: string, value: string): void;
  add(name: string, value: string, culture: string): void;
  // Runtime expects a plain object with `name` and culture keys (not a `Label` returned by `get()`)
  add(SiteObject: { name: string; [culture: string]: string } | Label): void;
  update(name: string, value: string): void;
  update(name: string, value: string, culture: string): void;
  update(SiteObject: Label | { name: string; [culture: string]: string }): void;
  delete(nameOrId: any): void;
}