0

I generated a local plugin and created an article model using:

"pluginOptions": {
  "i18n": {
    "localized": true
  }
},

inside his article.settings.json file, in order to make some specific fields translatables using the Internationalization(I18N) plugin

Problem is, while running the command:

strapi develop --watch-admin

I end up having the following errors:

error Something went wrong in the model "Article" with the attribute "localizations"

error TypeError: Cannot read property "uid" of undefined

Removing the "pluginOptions" instead, gives my local plugin running without any translatable field or articles__translations pivot that should be generated into my mysql database

"pluginOptions" is the very same parameter that gets generated into the model settings creating a collection type using the Content-Types Builder, but I can't have it to work while using it for a local plugin.

Here is my article.settings.json:

plugins/blog/models/article.settings.json

{
  "kind": "collectionType",
  "collectionName": "articles",
  "info": {
    "name": "article"
  },
  "options": {
    "draftAndPublish": false,
    "timestamps": true,
    "populateCreatorFields": true,
    "increments": true,
    "comment": ""
  },
  "pluginOptions": {
    "i18n": {
      "localized": true
    }
  },
  "attributes": {
    "title": {
      "pluginOptions": {
        "i18n": {
          "localized": true
        }
      },
      "type": "string",
      "required": true,
      "maxLength": 255,
      "minLength": 3
    },
    "slug": {
      "pluginOptions": {
        "i18n": {
          "localized": true
        }
      },
      "type": "uid",
      "targetField": "title",
      "required": true
    },
    "featured": {
      "pluginOptions": {
        "i18n": {
          "localized": false
        }
      },
      "type": "boolean",
      "default": false
    },
    "published_date": {
      "pluginOptions": {
        "i18n": {
          "localized": false
        }
      },
      "type": "datetime"
    },
  }
}

1 Answer 1

0

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: https://github.com/strapi/strapi/blob/88caa92f878a068926255dd482180202f53fcdcc/packages/core/content-type-builder/server/controllers/content-types.js#L48

EDIT: 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:

https://github.com/strapi/strapi/blob/1eab2fb08c7a4d3d40a5a7ff3b2f137ce0afcf8a/packages/core/content-type-builder/server/services/content-types.js#L37

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.