Localisation
Locales
You can enable localisation using the Studio under Site settings.
When enabled you will have the following value on the Site object:
- locale (string|null): The default locale of the site
- languages (string[]): The list of available locales, including the default locale.
Loading the default locale
By default, the EntityManager#load() method will load data in the default locale according to the Site#locale value.
Loading a specific locale
You can configure another start language by providing it when constructing Options object.
const em = new EntityManager({
"endpoint": "https://api.adsum.io",
"site": 322,
"username": "323-device",
"key": "343169bf805f8abd5fa71a4f529594a654de6afbac70a2d867a8b458c526fb7d",
"locale": "fr", // Request French locale
});
// Load data in "fr" (assuming "fr" is allowed, otherwise API will throw an Error)
em.load();
In case you are not able to know what are the available locale, you need to first load the default one to access Site#languages property which define allowed locales.
// use default locale
const em = new EntityManager({
"endpoint": "https://api.adsum.io",
"site": 322,
"username": "323-device",
"key": "343169bf805f8abd5fa71a4f529594a654de6afbac70a2d867a8b458c526fb7d",
});
// Reload the EntityManager with the "es" locale
em.reloadWithLocale("es").then(() => {
// It's loaded
});
reloadWithLocale does effectively reload, so you need to wonder about modifications such as creation, deletion...
Loading multiple locales
Loading multiple locale into one entityManager is not supported, but you can create multiple entityManagers
// use default locale
const em = new EntityManager({
"endpoint": "https://api.adsum.io",
"site": 322,
"username": "323-device",
"key": "343169bf805f8abd5fa71a4f529594a654de6afbac70a2d867a8b458c526fb7d",
});
const emByLocale = new Map();
const site = em.getRepository('Site').getCurrent();
site.languages.forEach((locale) => {
if (locale === site.locale) {
emByLocale.set(locale, em);
} else {
emByLocale.set(
locale,
new EntityManager({
"endpoint": "https://api.adsum.io",
"site": 322,
"username": "323-device",
"key": "343169bf805f8abd5fa71a4f529594a654de6afbac70a2d867a8b458c526fb7d",
}),
);
}
});