40 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-01-22 17:57:15 +01:00
'use strict';
2021-01-04 22:01:37 +01:00
const _ = require('lodash');
2021-02-15 11:24:28 +01:00
const { capitalize } = require('lodash/fp');
const { getService } = require('../../utils');
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
2021-02-12 12:23:51 +01:00
Object.values(strapi.models).forEach(model => {
2021-02-15 11:24:28 +01:00
if (getService('content-types').isLocalized(model)) {
2021-02-15 11:25:03 +01:00
// TODO: support adding lifecycles programmatically or connecting to a database event handler to avoid conflicts with existing lifecycles fonctions
2021-02-12 12:23:51 +01:00
_.set(model, 'lifecycles.beforeCreate', async data => {
if (!data.locale) {
2021-02-15 11:24:28 +01:00
data.locale = await getService('locales').getDefaultLocale();
2021-02-12 12:23:51 +01:00
}
});
2021-01-04 22:01:37 +01:00
2021-02-12 12:23:51 +01:00
_.set(model, 'lifecycles.afterCreate', async entry => {
2021-02-15 11:24:28 +01:00
await getService('localizations').addLocalizations(entry, { model });
2021-02-12 12:23:51 +01:00
});
_.set(model, 'lifecycles.afterUpdate', async entry => {
2021-02-15 11:24:28 +01:00
await getService('localizations').updateNonLocalizedFields(entry, { model });
2021-02-12 12:23:51 +01:00
});
2021-01-04 22:01:37 +01:00
}
});
2021-02-12 12:23:51 +01:00
};