translations
Fetches the platform's translation messages for one language from the CDN, and the merchant's own wording for it.
Use translations only to inspect the store's message catalog. For ordinary text use useTranslation: the engine already loads these files for the page's language and builds the i18n instance from them. Unlike the other modules, this one uses the cdn and filesCdn clients, so it sends none of the store, token or scope headers.
import { useQuery } from '@tanstack/react-query';
import { getTwilightContext } from '@salla.sa/twilight-theme-engine/tanstack';
import { translations } from '@salla.sa/twilight-theme-engine/api/translations';
export function Debug() {
const { locale } = getTwilightContext();
const { data } = useQuery(translations.queries.byLocale(locale));
const keys = Object.keys(data?.[`${locale}.trans`] ?? {});
return <p>{keys.length} keys</p>;
}Methods
| Method | Parameters | Returns | Request | Login needed |
|---|---|---|---|---|
translations.get | locale: string | Promise<TranslationMessages | null> | GET translations/common/{locale}.json on https://cdn.files.salla.network | No |
translations.forStore | storeId: string | number, locale: string | Promise<Record<string, unknown> | null> | GET translations/{storeId}/{locale}.json on the same host | No |
translations.merge | common, overrides, locale | TranslationMessages | null | None: lays the merchant's wording over the platform's | No |
TranslationMessages is Record<string, Record<string, string>>, keyed by <locale>.trans, for example 'ar.trans'. get returns one language, so the object has a single key.
forStore is the merchant's own edits to the wording. Most stores have none, so the engine asks for the file only when the language in the store settings has has_custom_translations set.
Queries
| Query | Key |
|---|---|
translations.queries.byLocale(locale) | ['translations', locale] |
translations.queries.byStore(storeId, locale) | ['translations', storeId, locale] |
Each language is its own file, so the locale is part of the key. storeId is store.id from the store settings, not the store identifier the client sends in its header.
Gotchas
- Fallback. If the platform file for a language is missing,
getfalls back to the older shared filejs/translations.jsononhttps://cdn.salla.network, and returnsnullif that fails too. - Errors are swallowed by
get. Any failure logs a warning andgetreturnsnull. Each wait is 5 seconds. forStoreis stricter. A 404 means "no overrides" and returnsnull, but any other failure throws, so the engine does not cache "no overrides" by mistake.- Your own strings. The store's messages live in the
appnamespace and your theme's locale files intheme. See Add translation keys.