2021-01-22 17:57:15 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-04 22:01:37 +01:00
|
|
|
const _ = require('lodash');
|
2021-02-12 12:23:51 +01:00
|
|
|
const { capitalize, prop, pick } = require('lodash/fp');
|
2021-01-25 20:58:33 +01:00
|
|
|
|
|
|
|
const actions = ['create', 'read', 'update', 'delete'].map(uid => ({
|
|
|
|
section: 'settings',
|
|
|
|
category: 'Internationalization',
|
|
|
|
subCategory: 'Locales',
|
|
|
|
pluginName: 'i18n',
|
|
|
|
displayName: capitalize(uid),
|
|
|
|
uid: `locale.${uid}`,
|
|
|
|
}));
|
|
|
|
|
|
|
|
module.exports = () => {
|
|
|
|
const { actionProvider } = strapi.admin.services.permission;
|
|
|
|
actionProvider.register(actions);
|
2021-01-04 22:01:37 +01:00
|
|
|
|
|
|
|
// register custom permissions
|
|
|
|
|
|
|
|
// register database mixin to modify model behaviours
|
|
|
|
|
|
|
|
// update model lifecycles
|
|
|
|
|
|
|
|
// create the localization of the object & link it to the other localizations it has
|
|
|
|
|
2021-02-12 12:23:51 +01:00
|
|
|
Object.values(strapi.models).forEach(model => {
|
|
|
|
if (isLocalized(model)) {
|
|
|
|
console.log('i18N is enabled for ', model.modelName);
|
2021-01-04 22:01:37 +01:00
|
|
|
|
2021-02-12 12:23:51 +01:00
|
|
|
_.set(model, 'lifecycles.beforeCreate', async data => {
|
|
|
|
if (!data.locale) {
|
|
|
|
data.locale = await getDefaultLocale();
|
|
|
|
}
|
|
|
|
});
|
2021-01-04 22:01:37 +01:00
|
|
|
|
2021-02-12 12:23:51 +01:00
|
|
|
_.set(model, 'lifecycles.afterCreate', async entry => {
|
|
|
|
// if new entry doesn't have localizations set then create it
|
2021-01-04 22:01:37 +01:00
|
|
|
|
2021-02-12 12:23:51 +01:00
|
|
|
if (entry.localizations === null) {
|
|
|
|
const localizations = [{ locale: entry.locale, id: entry.id }];
|
|
|
|
await strapi.query(model.uid).update({ id: entry.id }, { localizations });
|
|
|
|
|
|
|
|
Object.assign(entry, { localizations });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_.set(model, 'lifecycles.afterUpdate', async entry => {
|
|
|
|
const toUpdate = pick(getNonLocalizedFields(model), entry);
|
|
|
|
|
|
|
|
if (Array.isArray(entry.localizations)) {
|
|
|
|
await Promise.all(
|
|
|
|
entry.localizations.map(({ id }) => {
|
|
|
|
if (id === entry.id) return Promise.resolve();
|
|
|
|
return strapi.query(model.uid).update(
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
toUpdate
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2021-01-04 22:01:37 +01:00
|
|
|
|
|
|
|
_.set(model, 'lifecycles.beforeFind', async () => {});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// wrap content manager routes
|
|
|
|
|
2021-02-12 12:23:51 +01:00
|
|
|
|
2021-01-04 22:01:37 +01:00
|
|
|
|
|
|
|
// or overwrite controllers
|
2021-01-25 20:58:33 +01:00
|
|
|
};
|
2021-02-12 12:23:51 +01:00
|
|
|
|
|
|
|
const isLocalized = model => {
|
|
|
|
return prop('pluginOptions.i18n.localized', model) === true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getNonLocalizedFields = model => {
|
|
|
|
Object.keys(model.attributes).filter(attributeName => {
|
|
|
|
const attribute = model.attributes[attributeName];
|
|
|
|
return prop('pluginOptions.i18n.localized', attribute) !== true && !!attribute.type; // exclude relations
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDefaultLocale = async () => {
|
|
|
|
return 'en-US';
|
|
|
|
};
|