Languages
Read and change the texts the SDK and the web components show, with Salla.lang.
Read this when you need a text the SDK or a salla-* web component shows, or want to change one.
Salla.lang holds the store's texts: the messages the SDK shows and the labels inside the web components.
They are not your theme's texts. For those, use useTranslation and your locale files, as Languages and RTL shows.
Read a text
Pass the key. The SDK looks it up in the current language, then in English:
Salla.lang.get('common.messages.required_fields');
Salla.lang.get(key, { name: 'Sara' }); // fills :name in the text
Salla.lang.getLocale(); // 'ar'A key that exists in neither language comes back as the key itself.
Wait until the texts load
The texts load after the SDK starts. Read them once they are there:
await Salla.lang.onLoaded();
const label = Salla.lang.get('pages.cart.free_shipping');onLoaded resolves at once when the texts are already loaded. They fire languages::translations.loaded when they arrive.
Change or add texts
| Method | Does |
|---|---|
Salla.lang.set(key, text) | Changes a text in the current language |
Salla.lang.add(key, { ar, en }) | Adds a key in several languages |
Salla.lang.addBulk({ key: { ar, en }, ... }) | Adds several keys at once |
Salla.lang.add('pages.order.new_order', { ar: 'طلب جديد', en: 'New Order' });
Salla.lang.addBulk({
'pages.order.new_order': { ar: 'طلب جديد', en: 'New Order' },
'pages.product.new_product': { ar: 'منتج جديد', en: 'New Product' },
});Use these to change a label inside a web component, after onLoaded. The change lasts until the page reloads.
Gotchas
- Do not use
Salla.langfor your theme's own strings. Your locale files load on the server with the page, whileSalla.langexists only in the browser. setchanges the current language only. Pass the key without a language:set('pages.order.new_order', 'New Order').- The engine keeps the SDK's language in step with the URL. Do not change it yourself with
Salla.lang.setLocale.