2021-01-04 22:01:37 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
2021-02-12 12:23:51 +01:00
|
|
|
const { prop } = require('lodash/fp');
|
|
|
|
const pluralize = require('pluralize');
|
|
|
|
|
|
|
|
const isLocalized = model => {
|
|
|
|
return prop('pluginOptions.i18n.localized', model) === true;
|
|
|
|
};
|
2021-01-04 22:01:37 +01:00
|
|
|
|
|
|
|
// add a register function to do some stuff after the loading but before the boot
|
|
|
|
module.exports = () => {
|
|
|
|
// need to add some logic to the db layer so we can add fields to the models
|
|
|
|
|
|
|
|
Object.values(strapi.models).forEach(model => {
|
2021-02-12 12:23:51 +01:00
|
|
|
if (isLocalized(model)) {
|
2021-01-04 22:01:37 +01:00
|
|
|
_.set(model.attributes, 'localizations', {
|
|
|
|
writable: true,
|
|
|
|
private: false,
|
|
|
|
configurable: false,
|
|
|
|
type: 'json',
|
|
|
|
});
|
|
|
|
|
|
|
|
_.set(model.attributes, 'locale', {
|
|
|
|
writable: true,
|
|
|
|
private: false,
|
|
|
|
configurable: false,
|
|
|
|
type: 'string',
|
|
|
|
});
|
2021-02-12 12:23:51 +01:00
|
|
|
|
|
|
|
// add new route
|
|
|
|
const route =
|
|
|
|
model.kind === 'singleType'
|
|
|
|
? _.kebabCase(model.modelName)
|
|
|
|
: _.kebabCase(pluralize(model.modelName));
|
|
|
|
|
|
|
|
const localizationRoutes = [
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
path: `/${route}/:id/localizations`,
|
|
|
|
handler: `${model.modelName}.createLocalization`,
|
|
|
|
config: {
|
|
|
|
policies: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const handler = function(ctx) {
|
|
|
|
ctx.body = 'works';
|
|
|
|
};
|
|
|
|
|
|
|
|
strapi.config.routes.push(...localizationRoutes);
|
|
|
|
|
|
|
|
_.set(
|
|
|
|
strapi,
|
|
|
|
`api.${model.apiName}.controllers.${model.modelName}.createLocalization`,
|
|
|
|
handler
|
|
|
|
);
|
2021-01-04 22:01:37 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// strapi.database.migrations.push({
|
2021-02-08 12:03:24 +01:00
|
|
|
// before() {
|
|
|
|
// // if model had i18N but doesn't anymore
|
|
|
|
// // on enable
|
|
|
|
// // -> set locale to default locale
|
|
|
|
// // -> init localizations json
|
|
|
|
// // -> init strapiId
|
|
|
|
// // on disabled
|
|
|
|
// // -> delete data not in default locale
|
|
|
|
// // -> remove default locale ?
|
|
|
|
// // needed operations
|
|
|
|
// },
|
|
|
|
// after() {
|
|
|
|
// // delete la data
|
|
|
|
// // deleteColumn('locale');
|
|
|
|
// },
|
2021-01-04 22:01:37 +01:00
|
|
|
// });
|
|
|
|
};
|
2021-02-08 12:03:24 +01:00
|
|
|
|
|
|
|
/**
|
2021-02-12 12:23:51 +01:00
|
|
|
* for the CM =>
|
2021-02-08 12:03:24 +01:00
|
|
|
*/
|