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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-02-12 12:52:29 +01:00
|
|
|
strapi.db.migrations.register({
|
|
|
|
before() {
|
|
|
|
console.log('before migration');
|
|
|
|
},
|
|
|
|
after() {
|
|
|
|
console.log('after migration');
|
|
|
|
},
|
|
|
|
});
|
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
|
|
|
*/
|