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
|
|
|
|
2023-11-22 11:36:57 +00:00
|
|
|
import { CheckboxConfirmation } from './components/CheckboxConfirmation';
|
2024-02-22 17:18:32 +00:00
|
|
|
import { DeleteLocaleAction, LocalePickerAction } from './components/CMHeaderActions';
|
2023-11-20 09:38:50 +00:00
|
|
|
import {
|
|
|
|
DeleteModalAdditionalInfo,
|
|
|
|
PublishModalAdditionalInfo,
|
|
|
|
UnpublishModalAdditionalInfo,
|
|
|
|
} from './components/CMListViewModalsAdditionalInformation';
|
2023-11-22 11:36:57 +00:00
|
|
|
import { Initializer } from './components/Initializer';
|
2023-11-20 09:38:50 +00:00
|
|
|
import { LocalePicker } from './components/LocalePicker';
|
2023-06-20 13:15:59 +02:00
|
|
|
import { PERMISSIONS } from './constants';
|
2024-04-02 10:08:10 +01:00
|
|
|
import { mutateEditViewHook } from './contentManagerHooks/editView';
|
2023-11-22 11:36:57 +00:00
|
|
|
import { addColumnToTableHook } from './contentManagerHooks/listView';
|
|
|
|
import { extendCTBAttributeInitialDataMiddleware } from './middlewares/extendCTBAttributeInitialData';
|
|
|
|
import { extendCTBInitialDataMiddleware } from './middlewares/extendCTBInitialData';
|
|
|
|
import { localePermissionMiddleware } from './middlewares/localePermission';
|
2023-11-08 14:29:42 +01:00
|
|
|
import { pluginId } from './pluginId';
|
2024-02-22 17:18:32 +00:00
|
|
|
import { i18nApi } from './services/api';
|
2023-11-22 11:36:57 +00:00
|
|
|
import { LOCALIZED_FIELDS } from './utils/fields';
|
2023-11-16 14:11:34 +00:00
|
|
|
import { getTranslation } from './utils/getTranslation';
|
2024-03-19 17:33:46 +01:00
|
|
|
import { prefixPluginTranslations } from './utils/prefixPluginTranslations';
|
2023-11-22 11:36:57 +00:00
|
|
|
import { mutateCTBContentTypeSchema } from './utils/schemas';
|
2021-01-22 17:57:15 +01:00
|
|
|
|
2024-03-28 13:26:44 +00:00
|
|
|
import type { DocumentActionComponent } from '@strapi/plugin-content-manager/strapi-admin';
|
2024-02-22 17:18:32 +00: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) {
|
2023-11-22 11:36:57 +00:00
|
|
|
app.addMiddlewares([
|
|
|
|
extendCTBAttributeInitialDataMiddleware,
|
|
|
|
extendCTBInitialDataMiddleware,
|
|
|
|
localePermissionMiddleware,
|
|
|
|
]);
|
2024-02-22 17:18:32 +00:00
|
|
|
app.addMiddlewares([() => i18nApi.middleware]);
|
|
|
|
app.addReducers({
|
|
|
|
[i18nApi.reducerPath]: i18nApi.reducer,
|
|
|
|
});
|
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) {
|
2024-02-22 17:18:32 +00:00
|
|
|
// // Hook that adds a column into the CM's LV table
|
2021-06-24 08:22:44 +02:00
|
|
|
app.registerHook('Admin/CM/pages/ListView/inject-column-in-table', addColumnToTableHook);
|
2024-04-02 10:08:10 +01:00
|
|
|
app.registerHook('Admin/CM/pages/EditView/mutate-edit-view-layout', mutateEditViewHook);
|
2024-02-22 17:18:32 +00:00
|
|
|
|
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',
|
2024-01-15 11:13:36 +00:00
|
|
|
to: 'internationalization',
|
|
|
|
Component: () =>
|
|
|
|
import('./pages/SettingsPage').then((mod) => ({ default: mod.ProtectedSettingsPage })),
|
2023-06-20 13:15:59 +02:00
|
|
|
permissions: PERMISSIONS.accessMain,
|
2021-06-08 09:02:20 +02:00
|
|
|
});
|
|
|
|
|
2024-02-22 17:18:32 +00:00
|
|
|
const contentManager = app.getPlugin('content-manager');
|
|
|
|
|
|
|
|
contentManager.apis.addDocumentHeaderAction([LocalePickerAction]);
|
|
|
|
contentManager.apis.addDocumentAction((actions: DocumentActionComponent[]) => {
|
|
|
|
const indexOfDeleteAction = actions.findIndex((action) => action.type === 'delete');
|
|
|
|
actions.splice(indexOfDeleteAction, 0, DeleteLocaleAction);
|
|
|
|
return actions;
|
2021-06-24 08:22:44 +02:00
|
|
|
});
|
|
|
|
|
2024-03-28 13:26:44 +00:00
|
|
|
contentManager.injectComponent('listView', 'actions', {
|
2021-06-24 08:22:44 +02:00
|
|
|
name: 'i18n-locale-filter',
|
|
|
|
Component: LocalePicker,
|
|
|
|
});
|
|
|
|
|
2024-03-28 13:26:44 +00:00
|
|
|
contentManager.injectComponent('listView', 'publishModalAdditionalInfos', {
|
2023-04-28 12:46:42 +02:00
|
|
|
name: 'i18n-publish-bullets-in-modal',
|
2023-11-20 09:38:50 +00:00
|
|
|
Component: PublishModalAdditionalInfo,
|
2023-04-27 18:36:34 +02:00
|
|
|
});
|
|
|
|
|
2024-03-28 13:26:44 +00:00
|
|
|
contentManager.injectComponent('listView', 'unpublishModalAdditionalInfos', {
|
2023-04-28 12:46:42 +02:00
|
|
|
name: 'i18n-unpublish-bullets-in-modal',
|
2023-11-20 09:38:50 +00:00
|
|
|
Component: UnpublishModalAdditionalInfo,
|
2023-04-28 12:46:42 +02:00
|
|
|
});
|
|
|
|
|
2024-03-28 13:26:44 +00:00
|
|
|
contentManager.injectComponent('listView', 'deleteModalAdditionalInfos', {
|
2024-03-19 12:35:41 +01:00
|
|
|
name: 'i18n-delete-bullets-in-modal',
|
|
|
|
Component: DeleteModalAdditionalInfo,
|
|
|
|
});
|
|
|
|
|
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
|
|
|
};
|