I have fixed by the following lifecycle addon:
By adding this lifecycle, when the slug fieldis is empty it will gen the slug from the title field, the slug will only genetrate in the en locale.
The slug field:
"slug": {
"pluginOptions": {
"i18n": {
"localized": false
}
},
"type": "string",
"unique": false,
"regex": "^[a-z0-9]+(?:-[a-z0-9]+)*$"
}
The lifecycle.js put under ./api/[api-name]/content-types/[content-type-name]/ folder, replace all api::blog.blog to api::[your-api-name]:
let slugify = require("slugify");
const { ApplicationError } = require("@strapi/utils").errors;
module.exports = {
async beforeCreate(event) {
await generateSlug(event);
},
async beforeUpdate(event) {
await generateSlug(event);
},
};
const generateSlug = async (event) => {
const DEFAULT_LOCALE = "en";
const { data } = event.params;
const id = event.params?.where?.id ?? null;
const locale = !id ? "en" : await getLocale(id);
//Generate slug for en locale only
if (!data.slug && data.title && locale == DEFAULT_LOCALE) {
data.slug = slugify(data.title, { lower: true });
}
if (!data.slug) {
throw new ApplicationError("Slug is required!");
}
};
const getLocale = async (id) => {
const res = await strapi.service("api::portfolio.portfolio").findOne(id);
return res.locale;
};