2023-06-12 16:22:11 +02:00
|
|
|
import { prefixPluginTranslations } from '@strapi/helper-plugin';
|
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';
|
2023-06-12 16:22:11 +02:00
|
|
|
|
2021-03-12 14:07:48 +01:00
|
|
|
import CheckboxConfirmation from './components/CheckboxConfirmation';
|
2021-03-17 09:39:57 +01:00
|
|
|
import CMEditViewInjectedComponents from './components/CMEditViewInjectedComponents';
|
2023-06-12 16:22:11 +02:00
|
|
|
import DeleteModalAdditionalInfos from './components/CMListViewInjectedComponents/DeleteModalAdditionalInfos';
|
|
|
|
import PublishModalAdditionalInfos from './components/CMListViewInjectedComponents/PublishModalAdditionalInfos';
|
|
|
|
import UnpublishModalAdditionalInfos from './components/CMListViewInjectedComponents/UnpublishModalAdditionalInfos';
|
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';
|
2023-06-20 13:15:59 +02:00
|
|
|
import { PERMISSIONS } from './constants';
|
2023-06-12 16:22:11 +02:00
|
|
|
import addColumnToTableHook from './contentManagerHooks/addColumnToTable';
|
|
|
|
import addLocaleToCollectionTypesLinksHook from './contentManagerHooks/addLocaleToCollectionTypesLinks';
|
|
|
|
import addLocaleToSingleTypesLinksHook from './contentManagerHooks/addLocaleToSingleTypesLinks';
|
|
|
|
import mutateEditViewLayoutHook from './contentManagerHooks/mutateEditViewLayout';
|
2021-01-26 10:31:50 +01:00
|
|
|
import middlewares from './middlewares';
|
2023-11-08 14:29:42 +01:00
|
|
|
import { pluginId } from './pluginId';
|
2023-11-16 14:11:34 +00:00
|
|
|
import { reducers } from './store/reducers';
|
|
|
|
import { getTranslation } from './utils/getTranslation';
|
2021-02-05 17:41:12 +01:00
|
|
|
import LOCALIZED_FIELDS from './utils/localizedFields';
|
2023-06-12 16:22:11 +02:00
|
|
|
import mutateCTBContentTypeSchema from './utils/mutateCTBContentTypeSchema';
|
2021-01-22 17:57:15 +01:00
|
|
|
|
2023-11-16 14:11:34 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2021-05-11 12:24:00 +02:00
|
|
|
export default {
|
2023-11-08 14:29:42 +01:00
|
|
|
register(app: any) {
|
2021-05-12 10:11:59 +02:00
|
|
|
app.addMiddlewares(middlewares);
|
2021-05-11 12:24:00 +02:00
|
|
|
|
2023-11-16 14:11:34 +00:00
|
|
|
/**
|
|
|
|
* TODO: this should use the `useInjectReducer` hook when it's exported from the `@strapi/admin` package.
|
|
|
|
*/
|
|
|
|
app.addReducers(reducers);
|
2021-05-11 12:24:00 +02:00
|
|
|
|
|
|
|
app.registerPlugin({
|
|
|
|
id: pluginId,
|
|
|
|
initializer: Initializer,
|
|
|
|
isReady: false,
|
2023-11-08 14:29:42 +01:00
|
|
|
name: pluginId,
|
2021-05-11 12:24:00 +02:00
|
|
|
});
|
|
|
|
},
|
2023-11-08 14:29:42 +01:00
|
|
|
bootstrap(app: any) {
|
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: {
|
2023-11-16 14:11:34 +00:00
|
|
|
id: getTranslation('plugin.name'),
|
2021-06-08 09:02:20 +02:00
|
|
|
defaultMessage: 'Internationalization',
|
|
|
|
},
|
|
|
|
id: 'internationalization',
|
|
|
|
to: '/settings/internationalization',
|
2021-07-28 08:16:15 +02:00
|
|
|
|
2023-08-29 09:24:29 +02:00
|
|
|
async Component() {
|
2023-11-16 14:11:34 +00:00
|
|
|
const { ProtectedSettingsPage } = await import('./pages/SettingsPage');
|
2023-08-29 09:24:29 +02:00
|
|
|
|
2023-11-16 14:11:34 +00:00
|
|
|
return ProtectedSettingsPage;
|
2023-08-29 09:24:29 +02:00
|
|
|
},
|
2023-06-20 13:15:59 +02:00
|
|
|
permissions: PERMISSIONS.accessMain,
|
2021-06-08 09:02:20 +02:00
|
|
|
});
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2023-04-27 18:36:34 +02:00
|
|
|
app.injectContentManagerComponent('listView', 'publishModalAdditionalInfos', {
|
2023-04-28 12:46:42 +02:00
|
|
|
name: 'i18n-publish-bullets-in-modal',
|
2023-04-27 18:36:34 +02:00
|
|
|
Component: PublishModalAdditionalInfos,
|
|
|
|
});
|
|
|
|
|
2023-04-28 12:46:42 +02:00
|
|
|
app.injectContentManagerComponent('listView', 'unpublishModalAdditionalInfos', {
|
|
|
|
name: 'i18n-unpublish-bullets-in-modal',
|
|
|
|
Component: UnpublishModalAdditionalInfos,
|
|
|
|
});
|
|
|
|
|
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 [
|
2021-10-08 11:13:31 +02:00
|
|
|
{
|
|
|
|
name: 'pluginOptions.i18n.localized',
|
|
|
|
description: {
|
2023-11-16 14:11:34 +00:00
|
|
|
id: getTranslation('plugin.schema.i18n.localized.description-content-type'),
|
2023-01-20 13:20:03 +01:00
|
|
|
defaultMessage: 'Allows translating an entry into different languages',
|
2021-05-11 12:24:00 +02:00
|
|
|
},
|
2021-10-08 11:13:31 +02:00
|
|
|
type: 'checkboxConfirmation',
|
2021-10-08 12:54:05 +02:00
|
|
|
intlLabel: {
|
2023-11-16 14:11:34 +00:00
|
|
|
id: getTranslation('plugin.schema.i18n.localized.label-content-type'),
|
2023-01-20 13:20:03 +01:00
|
|
|
defaultMessage: 'Localization',
|
2021-10-08 12:54:05 +02:00
|
|
|
},
|
2021-10-08 11:13:31 +02:00
|
|
|
},
|
2021-05-11 12:24:00 +02:00
|
|
|
];
|
2021-01-26 16:50:54 +01:00
|
|
|
},
|
2021-05-11 12:24:00 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ctbFormsAPI.extendFields(LOCALIZED_FIELDS, {
|
2023-11-08 14:29:42 +01:00
|
|
|
validator: (args: any) => ({
|
2021-05-11 12:24:00 +02:00
|
|
|
i18n: yup.object().shape({
|
|
|
|
localized: yup.bool().test({
|
|
|
|
name: 'ensure-unique-localization',
|
2023-11-16 14:11:34 +00:00
|
|
|
message: getTranslation('plugin.schema.i18n.ensure-unique-localization'),
|
2021-05-11 12:24:00 +02:00
|
|
|
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: {
|
2023-11-08 14:29:42 +01:00
|
|
|
advanced({ contentTypeSchema, forTarget, type, step }: any) {
|
2021-05-11 12:24:00 +02:00
|
|
|
if (forTarget !== 'contentType') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasI18nEnabled = get(
|
|
|
|
contentTypeSchema,
|
|
|
|
['schema', 'pluginOptions', 'i18n', 'localized'],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!hasI18nEnabled) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'component' && step === '1') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2021-10-08 11:13:31 +02:00
|
|
|
{
|
|
|
|
name: 'pluginOptions.i18n.localized',
|
|
|
|
description: {
|
2023-11-16 14:11:34 +00:00
|
|
|
id: getTranslation('plugin.schema.i18n.localized.description-field'),
|
2021-10-08 16:07:52 +02:00
|
|
|
defaultMessage: 'The field can have different values in each locale',
|
2021-05-11 12:24:00 +02:00
|
|
|
},
|
2021-10-08 11:13:31 +02:00
|
|
|
type: 'checkbox',
|
2021-10-08 16:07:52 +02:00
|
|
|
intlLabel: {
|
2023-11-16 14:11:34 +00:00
|
|
|
id: getTranslation('plugin.schema.i18n.localized.label-field'),
|
2021-10-08 16:07:52 +02:00
|
|
|
defaultMessage: 'Enable localization for this field',
|
|
|
|
},
|
2021-10-08 11:13:31 +02:00
|
|
|
},
|
2021-05-11 12:24:00 +02:00
|
|
|
];
|
2021-02-05 17:41:12 +01:00
|
|
|
},
|
2021-05-11 12:24:00 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2023-11-08 14:29:42 +01:00
|
|
|
async registerTrads({ locales }: { locales: string[] }) {
|
2021-06-09 11:22:23 +02:00
|
|
|
const importedTrads = await Promise.all(
|
2022-08-08 23:33:39 +02:00
|
|
|
locales.map((locale) => {
|
2023-10-30 11:36:16 +00:00
|
|
|
return import(`./translations/${locale}.json`)
|
2021-06-09 11:22:23 +02:00
|
|
|
.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
|
|
|
};
|