2021-04-06 11:19:39 +02:00
|
|
|
import get from 'lodash/get';
|
2021-01-26 16:18:26 +01:00
|
|
|
import * as yup from 'yup';
|
2021-06-09 12:11:51 +02:00
|
|
|
import { prefixPluginTranslations } from '@strapi/helper-plugin';
|
2021-01-22 17:57:15 +01:00
|
|
|
import pluginPkg from '../../package.json';
|
2021-03-12 14:07:48 +01:00
|
|
|
import pluginLogo from './assets/images/logo.svg';
|
|
|
|
import CheckboxConfirmation from './components/CheckboxConfirmation';
|
2021-03-17 09:39:57 +01:00
|
|
|
import CMEditViewInjectedComponents from './components/CMEditViewInjectedComponents';
|
2021-05-27 07:35:43 +02:00
|
|
|
import Initializer from './components/Initializer';
|
2021-03-12 14:07:48 +01:00
|
|
|
import LocalePicker from './components/LocalePicker';
|
2021-01-26 10:31:50 +01:00
|
|
|
import middlewares from './middlewares';
|
2021-03-12 14:07:48 +01:00
|
|
|
import pluginPermissions from './permissions';
|
2021-01-22 17:57:15 +01:00
|
|
|
import pluginId from './pluginId';
|
2021-01-28 11:33:37 +01:00
|
|
|
import { getTrad } from './utils';
|
2021-02-08 08:24:34 +01:00
|
|
|
import mutateCTBContentTypeSchema from './utils/mutateCTBContentTypeSchema';
|
2021-02-05 17:41:12 +01:00
|
|
|
import LOCALIZED_FIELDS from './utils/localizedFields';
|
2021-03-03 10:00:45 +01:00
|
|
|
import i18nReducers from './hooks/reducers';
|
2021-09-21 06:06:07 +02:00
|
|
|
import DeleteModalAdditionalInfos from './components/CMListViewInjectedComponents/DeleteModalAdditionalInfos';
|
2021-06-24 08:22:44 +02:00
|
|
|
import addLocaleToCollectionTypesLinksHook from './contentManagerHooks/addLocaleToCollectionTypesLinks';
|
|
|
|
import addLocaleToSingleTypesLinksHook from './contentManagerHooks/addLocaleToSingleTypesLinks';
|
|
|
|
import addColumnToTableHook from './contentManagerHooks/addColumnToTable';
|
|
|
|
import mutateEditViewLayoutHook from './contentManagerHooks/mutateEditViewLayout';
|
2021-01-22 17:57:15 +01:00
|
|
|
|
2021-05-11 12:24:00 +02:00
|
|
|
const pluginDescription = pluginPkg.strapi.description || pluginPkg.description;
|
|
|
|
const icon = pluginPkg.strapi.icon;
|
|
|
|
const name = pluginPkg.strapi.name;
|
|
|
|
|
|
|
|
export default {
|
|
|
|
register(app) {
|
2021-05-12 10:11:59 +02:00
|
|
|
app.addMiddlewares(middlewares);
|
2021-05-11 12:24:00 +02:00
|
|
|
|
2021-05-12 10:11:59 +02:00
|
|
|
app.addReducers(i18nReducers);
|
2021-05-11 12:24:00 +02:00
|
|
|
|
|
|
|
app.registerPlugin({
|
|
|
|
description: pluginDescription,
|
|
|
|
icon,
|
|
|
|
id: pluginId,
|
|
|
|
initializer: Initializer,
|
|
|
|
isReady: false,
|
|
|
|
isRequired: pluginPkg.strapi.required || false,
|
|
|
|
name,
|
|
|
|
pluginLogo,
|
|
|
|
});
|
|
|
|
},
|
2021-07-21 09:05:20 +02:00
|
|
|
bootstrap(app) {
|
2021-06-23 07:54:47 +02:00
|
|
|
// Hooks that mutate the collection types links in order to add the locale filter
|
2021-06-24 08:22:44 +02:00
|
|
|
app.registerHook(
|
|
|
|
'Admin/CM/pages/App/mutate-collection-types-links',
|
|
|
|
addLocaleToCollectionTypesLinksHook
|
|
|
|
);
|
|
|
|
app.registerHook(
|
|
|
|
'Admin/CM/pages/App/mutate-single-types-links',
|
|
|
|
addLocaleToSingleTypesLinksHook
|
|
|
|
);
|
|
|
|
// Hook that adds a column into the CM's LV table
|
|
|
|
app.registerHook('Admin/CM/pages/ListView/inject-column-in-table', addColumnToTableHook);
|
|
|
|
// Hooks that mutates the edit view layout
|
|
|
|
app.registerHook('Admin/CM/pages/EditView/mutate-edit-view-layout', mutateEditViewLayoutHook);
|
2021-06-08 09:02:20 +02:00
|
|
|
// Add the settings link
|
|
|
|
app.addSettingsLink('global', {
|
|
|
|
intlLabel: {
|
|
|
|
id: getTrad('plugin.name'),
|
|
|
|
defaultMessage: 'Internationalization',
|
|
|
|
},
|
|
|
|
id: 'internationalization',
|
|
|
|
to: '/settings/internationalization',
|
2021-07-28 08:16:15 +02:00
|
|
|
|
|
|
|
Component: async () => {
|
|
|
|
const component = await import(
|
2021-08-19 09:42:19 +02:00
|
|
|
/* webpackChunkName: "i18n-settings-page" */ './pages/SettingsPage'
|
2021-07-28 08:16:15 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return component;
|
|
|
|
},
|
2021-06-08 09:02:20 +02:00
|
|
|
permissions: pluginPermissions.accessMain,
|
|
|
|
});
|
|
|
|
|
2021-06-24 08:22:44 +02:00
|
|
|
app.injectContentManagerComponent('editView', 'informations', {
|
|
|
|
name: 'i18n-locale-filter-edit-view',
|
|
|
|
Component: CMEditViewInjectedComponents,
|
|
|
|
});
|
|
|
|
|
|
|
|
app.injectContentManagerComponent('listView', 'actions', {
|
|
|
|
name: 'i18n-locale-filter',
|
|
|
|
Component: LocalePicker,
|
|
|
|
});
|
|
|
|
|
|
|
|
app.injectContentManagerComponent('listView', 'deleteModalAdditionalInfos', {
|
|
|
|
name: 'i18n-delete-bullets-in-modal',
|
|
|
|
Component: DeleteModalAdditionalInfos,
|
|
|
|
});
|
|
|
|
|
2021-05-11 12:24:00 +02:00
|
|
|
const ctbPlugin = app.getPlugin('content-type-builder');
|
2021-06-23 19:25:28 +02:00
|
|
|
|
2021-05-11 12:24:00 +02:00
|
|
|
if (ctbPlugin) {
|
|
|
|
const ctbFormsAPI = ctbPlugin.apis.forms;
|
|
|
|
ctbFormsAPI.addContentTypeSchemaMutation(mutateCTBContentTypeSchema);
|
|
|
|
ctbFormsAPI.components.add({ id: 'checkboxConfirmation', component: CheckboxConfirmation });
|
|
|
|
|
|
|
|
ctbFormsAPI.extendContentType({
|
|
|
|
validator: () => ({
|
|
|
|
i18n: yup.object().shape({
|
|
|
|
localized: yup.bool(),
|
2021-01-27 09:32:54 +01:00
|
|
|
}),
|
2021-05-11 12:24:00 +02:00
|
|
|
}),
|
|
|
|
form: {
|
|
|
|
advanced() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'pluginOptions.i18n.localized',
|
|
|
|
description: {
|
|
|
|
id: getTrad('plugin.schema.i18n.localized.description-content-type'),
|
2021-01-26 16:50:54 +01:00
|
|
|
},
|
2021-05-11 12:24:00 +02:00
|
|
|
type: 'checkboxConfirmation',
|
|
|
|
label: { id: getTrad('plugin.schema.i18n.localized.label-content-type') },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
];
|
2021-01-26 16:50:54 +01:00
|
|
|
},
|
2021-05-11 12:24:00 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ctbFormsAPI.extendFields(LOCALIZED_FIELDS, {
|
|
|
|
validator: args => ({
|
|
|
|
i18n: yup.object().shape({
|
|
|
|
localized: yup.bool().test({
|
|
|
|
name: 'ensure-unique-localization',
|
|
|
|
message: getTrad('plugin.schema.i18n.ensure-unique-localization'),
|
|
|
|
test(value) {
|
|
|
|
if (value === undefined || value) {
|
2021-04-06 11:19:39 +02:00
|
|
|
return true;
|
2021-05-11 12:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const unique = get(args, ['3', 'modifiedData', 'unique'], null);
|
|
|
|
|
|
|
|
// Unique fields must be localized
|
|
|
|
if (unique && !value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2021-02-04 14:18:54 +01:00
|
|
|
}),
|
2021-02-05 17:41:12 +01:00
|
|
|
}),
|
2021-05-11 12:24:00 +02:00
|
|
|
}),
|
|
|
|
form: {
|
|
|
|
advanced({ contentTypeSchema, forTarget, type, step }) {
|
|
|
|
if (forTarget !== 'contentType') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasI18nEnabled = get(
|
|
|
|
contentTypeSchema,
|
|
|
|
['schema', 'pluginOptions', 'i18n', 'localized'],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!hasI18nEnabled) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'component' && step === '1') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'pluginOptions.i18n.localized',
|
|
|
|
description: {
|
|
|
|
id: getTrad('plugin.schema.i18n.localized.description-field'),
|
2021-02-05 17:41:12 +01:00
|
|
|
},
|
2021-05-11 12:24:00 +02:00
|
|
|
type: 'checkbox',
|
|
|
|
label: { id: getTrad('plugin.schema.i18n.localized.label-field') },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
];
|
2021-02-05 17:41:12 +01:00
|
|
|
},
|
2021-05-11 12:24:00 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-06-09 11:22:23 +02:00
|
|
|
async registerTrads({ locales }) {
|
|
|
|
const importedTrads = await Promise.all(
|
|
|
|
locales.map(locale => {
|
|
|
|
return import(
|
|
|
|
/* webpackChunkName: "i18n-translation-[request]" */ `./translations/${locale}.json`
|
|
|
|
)
|
|
|
|
.then(({ default: data }) => {
|
|
|
|
return {
|
2021-06-09 12:11:51 +02:00
|
|
|
data: prefixPluginTranslations(data, pluginId),
|
2021-06-09 11:22:23 +02:00
|
|
|
locale,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
return {
|
|
|
|
data: {},
|
|
|
|
locale,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return Promise.resolve(importedTrads);
|
|
|
|
},
|
2021-01-22 17:57:15 +01:00
|
|
|
};
|