我生成了一个局部插件并使用以下方法创建了一个文章模型:
"pluginOptions": {
"i18n": {
"localized": true
}
},
在他的article.settings.json文件中,为了使用国际化(I18N)插件使某些特定字段可翻译
问题是,在运行命令时:
strapi发展--手表-管理
最后,我有以下错误:
错误:无法读取未定义属性"uid“的属性”本地化“错误TypeError:无法读取未定义的属性”uid“
移除"pluginOptions“,将使我的本地插件在没有任何可翻译字段或articles__translations支点的情况下运行,这些插件应该生成到mysql数据库中。
"pluginOptions“是生成到模型设置中的相同参数,该参数使用内容类型生成器创建集合类型,但我不能让它在本地插件中使用。
这是我的article.settings.json:
plugins/博客/模型/文章.setings.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"
},
}
}
发布于 2022-08-01 14:49:05
您可以使用content-type-builder
插件作为解决办法。您不会在content-types
文件夹下创建内容类型,而是以编程方式创建它。
作为一个非常简单的tag
内容类型的例子:
{
"singularName": "tag",
"pluralName": "tags",
"displayName": "tag",
"description": "",
"draftAndPublish": false,
"pluginOptions": {
"i18n": {
"localized": true
}
},
"attributes": {
"label": {
"type": "string",
"pluginOptions": {
"i18n": {
"localized": true
}
},
"unique": true
}
}
}
注意,json的这个模式与plugin/server/content-types
中的模式有点不同。
然后,您可以以如下方式以编程方式创建内容类型:
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;
}
},
});
这正是管理员使用创建内容类型的方式。
它使用pluginOptions.i18n.localized: true
进行工作。
一种方法是这样做,例如,在插件的引导阶段。在这里,您还可以检查内容是否已创建。
作为奖励,您还可以创建否则无法工作的组件。
希望这能有所帮助。
链接:以编程方式在插件中创建组件:https://github.com/strapi/strapi-plugin-seo/blob/main/server/services/seo.js
编辑:您还可以保留原始模式并使用这个fn来转换它--至少现在是这样,只要另一种方法不起作用:
https://stackoverflow.com/questions/69562356
复制相似问题