k.label / k.t / k.site.labels
Manage and resolve multilingual site labels
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| All server-side scripts (View, Page, Layout, API) | Configure site cultures in Admin; missing keys auto-create in k.t/k.label | Multilingual 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
// 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 / Property | Description | Returns |
|---|---|---|
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 site | Label[] |
k.site.labels.get(nameOrId) | Retrieve a single Label object by name or ID | Label | null |
k.site.labels.getValue(nameOrId[, params]) | Read translated text without auto-creating missing entries | string | null |
k.site.labels.getOrAdd(nameOrId, DefaultValue) | Read a label or create it with DefaultValue when missing | Label |
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 ID | void |
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
- Configure site languages under Site Settings → Basic Settings → Basics → Multilingual (such as
enandfr). - Manage label translations under Content → Labels (see Admin: Labels).
For example, for a label named Cancel:
en:Cancelfr: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.
| Parameter | Type | Required | Description |
|---|---|---|---|
value / NameOrId | string | Yes | Label name (value on k.t, NameOrId on k.label) |
params | Record<string, any> | No | Placeholder substitution values |
Returns: string — Translated text for the active culture.
// 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[].
const labels = k.site.labels.all()get()
Retrieve a single Label object (containing multilingual values) by name or ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | any | Yes | Label name or ID |
Returns: Label | null, null when missing.
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.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | any | Yes | Label name or ID |
params | Record<string, any> | No | Placeholder substitution map |
Returns: string | null, null when missing.
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.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | any | Yes | Label name or ID |
DefaultValue | string | Yes | Default text stored if created |
Returns: Label.
const cancelLabel = k.site.labels.getOrAdd("Cancel", "Cancel")add()
Create or set a label value.
| Overload | Description |
|---|---|
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.
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.
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.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | any | Yes | Label name or ID |
Returns: void.
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
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;
}Related Docs
- k.site — Site resource repositories
- KScript Language and Runtime — Global
t()behavior - k.request — Switching request culture with
setCulture()