首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Strapi本地插件中实现I18n本地化

在Strapi本地插件中实现I18n本地化
EN

Stack Overflow用户
提问于 2021-10-13 21:09:06
回答 1查看 253关注 0票数 0

我生成了一个局部插件并使用以下方法创建了一个文章模型:

代码语言:javascript
运行
复制
"pluginOptions": {
  "i18n": {
    "localized": true
  }
},

在他的article.settings.json文件中,为了使用国际化(I18N)插件使某些特定字段可翻译

问题是,在运行命令时:

strapi发展--手表-管理

最后,我有以下错误:

错误:无法读取未定义属性"uid“的属性”本地化“错误TypeError:无法读取未定义的属性”uid“

移除"pluginOptions“,将使我的本地插件在没有任何可翻译字段或articles__translations支点的情况下运行,这些插件应该生成到mysql数据库中。

"pluginOptions“是生成到模型设置中的相同参数,该参数使用内容类型生成器创建集合类型,但我不能让它在本地插件中使用。

这是我的article.settings.json:

plugins/博客/模型/文章.setings.json

代码语言:javascript
运行
复制
{
  "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"
    },
  }
}
EN

回答 1

Stack Overflow用户

发布于 2022-08-01 14:49:05

您可以使用content-type-builder插件作为解决办法。您不会在content-types文件夹下创建内容类型,而是以编程方式创建它。

作为一个非常简单的tag内容类型的例子:

代码语言:javascript
运行
复制
  {
    "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中的模式有点不同。

然后,您可以以如下方式以编程方式创建内容类型:

代码语言:javascript
运行
复制
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

创建内容类型:https://github.com/strapi/strapi/blob/88caa92f878a068926255dd482180202f53fcdcc/packages/core/content-type-builder/server/controllers/content-types.js#L48

编辑:您还可以保留原始模式并使用这个fn来转换它--至少现在是这样,只要另一种方法不起作用:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69562356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档