Is it possible to add i18n locale options in a custom plugin content-types?

Hi, is it possible to add i18n locale options in a custom plugin content-types? I’m developing a plugin and want i18n locale options enabled in my Plugin content-types. When I try to add the code below to my Custom Plugin content-types schema, I get this error “Cannot read property ‘routes’ of undefined
TypeError: Cannot read property ‘routes’ of undefined”.

“pluginOptions”: {
“i18n”: {
“localized”: false
}
},

1 Like

You can use the content-type-builder plugin as a workaround. You would not create the content type under the content-types folder but create it programmatically.

As an example of a very simple tag content type:

  {
    "singularName": "tag",
    "pluralName": "tags",
    "displayName": "tag",
    "description": "",
    "draftAndPublish": false,
    "pluginOptions": {
      "i18n": {
        "localized": true
      }
    },
    "attributes": {
      "label": {
        "type": "string",
        "pluginOptions": {
          "i18n": {
            "localized": true
          }
        },
        "unique": true
      }
    }
  }

Note, this schema of the json is a bit different from the ones in plugin/server/content-types.

Then you can create the content type programmatically like this:

import { Strapi } from "@strapi/strapi";
import tag from "../content-types/tag.json";
import page from "../content-types/page.json";

export default ({ strapi }: { strapi: Strapi }) => ({
  async createContentComponent() {
    if (!tag) return null;

    try {
      const components: any = [];

      const contentType = await strapi
        .plugin("content-type-builder")
        .services["content-types"].createContentType({
          contentType: tag,
          components,
        });

      return contentType;
    } catch (e) {
      console.log("error", e);
      return null;
    }
  },
});

This is exactly how the admin creates content types using the content builder UI.

And it works using the pluginOptions.i18n.localized: true.

One approach would be to do this, e.g., on the bootstrap phase of the plugin. Here you could also check whether or not the contents are created or not.

As a bonus, you can also create components that otherwise would not work.

Hope that helps.

Links:
Create components programmatically in a plugin: https://github.com/strapi/strapi-plugin-seo/blob/main/server/services/seo.js

Create content types:

1 Like

You could also keep the original schema and use this fn to transform it - at least for now as long as the other approach is not working:

This is great. Thank you for sharing this. :slightly_smiling_face: