SPA Multilingual (Frontend Endpoint)
Serves a JSON translation dictionary for single-page applications; entries are maintained in CMS: SPA Multilingual
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| Browser SPA (Vue, React, or vanilla JS) | Entries maintained under Development → SPA Multilingual | Fetch language-specific JSON dictionaries from GET /_spa/lang/{lang} |
No KScript management API
There is no k.spaMultilingual or other k.* scripting API for creating, editing, or deleting SPA entries. Import and edit entries in the admin under Development → SPA Multilingual; if you need server-side labels in scripts, use k.label() / k.t().
Quick Start
// Browser client: fetch the localized JSON dictionary
fetch("/_spa/lang/en")
.then((res) => res.json())
.then((dict) => {
console.log(dict.welcome || "Welcome")
})Overview
Kooboo SPA Multilingual stores translations as site resources (entry key + multilingual value dictionary) and serves a language-specific key-to-text JSON map at a fixed URL for frontend frameworks such as Vue and React.
Comparison with k.label
| Capability | SPA Multilingual | k.label / k.t |
|---|---|---|
| Management | Admin SPA Multilingual page | Admin Content → Labels |
| Consumption | Browser fetch('/_spa/lang/en') | Template k-label, script k.t('key') |
| Language source | URL path segment {lang} | Site multilingual / current request culture |
Fetch Dictionary
Request
GET /_spa/lang/{lang}| Segment | Description |
|---|---|
{lang} | Language code matching the admin column (lowercase), such as en, zh-cn, or zh-tw |
No SiteId query parameter is needed: the site context is resolved from the request domain.
Response
- Status code: 200
- Content-Type:
application/json - Body: JSON object where each key is an entry name and each value is a string (returns
{}when no entries are configured)
{
"welcome": "Welcome",
"nav.home": "Home"
}Only entries whose value in the requested language (or its fallback) is non-null are returned (empty strings "" are preserved); keys that are missing or null in both the requested language and the default language are omitted.
Fallback
For each entry:
- Read the text for
{lang}from the request path. - If the value is missing or
null(note: an empty string""does not trigger fallback), fall back to the default language configured for that item in the admin. - Include the entry if the resolved value is non-
null(including""); otherwise omit the key.
The default language corresponds to the column marked (Default) in the admin list.
Caching
The server caches the serialized JSON for about 3 seconds to reduce load from frequent refreshes.
Cache key is per site ID only, not per {lang}
Currently, the 3-second server cache is keyed solely by site ID (WebSite.Id) without {lang}. Consequently, requesting a different language within 3 seconds of another language request on the same site (for example, fetching /_spa/lang/zh-cn and switching to /_spa/lang/en within 3 seconds) can return the previously cached language's JSON until the 3-second entry expires. After editing entries in the admin, clients may also briefly read stale data, and runtime language switchers should account for this 3-second window.
CORS
This endpoint sets CORS response headers automatically so SPA dev servers can call it cross-origin during local development; in production, same-origin deployment with the site is still recommended.
Frontend Integration Examples
Native fetch
const lang = "zh-cn" // Match your vue-i18n locale or app locale
const dict = await fetch(`/_spa/lang/${lang}`).then((r) => r.json())
document.querySelector("h1").textContent = dict.welcome ?? "welcome"Vue 3 Example
import { createApp, ref } from "vue"
const locale = ref("zh-cn")
const messages = ref({})
async function loadLocale(code) {
messages.value = await fetch(`/_spa/lang/${code}`).then((r) => r.json())
locale.value = code
}
createApp({
setup() {
loadLocale("zh-cn")
return { locale, messages, loadLocale }
},
template: `<p>{{ messages.welcome }}</p>`,
}).mount("#app")In a real application, wrap loadLocale in your i18n helper and call it again whenever the user switches languages (keep the 3-second site-wide server cache caveat above in mind, as switching languages within 3 seconds on the same site can return the previous language's cached JSON).
Coordination with Site Multilingual Routing
If the site also uses Kooboo route-level multilingual (URL prefix or ?lang=), keep {lang} consistent with the site's UI language codes (for example both zh-cn or both zh) so dictionary lookups match the active route culture.
Import JSON Format
Prepare JSON files in this structure for batch Import in the CMS:
{
"keyName": {
"en": "English text",
"zh-cn": "简体中文"
}
}keyName: Up to 50 characters.- Nested keys: Language codes (stored in lowercase after import).
Related Docs
- SPA Multilingual (CMS) — Import, export, language columns, and editing
- k.label / k.site.labels — Server-side template and script labels
- k-data · culture — Multilingual URLs inside templates