【问题标题】:How to disable Plural suffix?如何禁用复数后缀?
【发布时间】:2021-05-12 16:01:06
【问题描述】:

Collection Types List 未列出 collectionName

API 名称不是“集合名称”,而是已创建集合类型的“显示名称”。

如何控制复数后缀?

有没有办法为复数实用程序定义新的不规则规则?

pluralize.plural('irregular') //=> "irregulars"

我无法创建英语以外的内容类型。它只是添加了它自己的后缀,它使 i18n 成为不可能。

重现行为的步骤

  1. 转到“内容类型生成器”
  2. 点击“创建新的集合类型”
  3. 在“显示名称”中输入“araba”(一个非英语单词)
  4. 在“高级设置”下的“集合名称”中输入“arabalar”(复数非英语单词)
  5. 查看左侧菜单,它不显示 collectionName,而是显示 API 名称

预期行为

它应该是“arabalar”而不是作为集合名称

输入的“arabas”

截图

“arabalar”是“araba”而不是“arabas”的复数

系统

  • Node.js 版本:v12.16.1
  • 纱线版本​​:1.22.4
  • Strapi 版本:v3.0.0-beta.19.3
  • 数据库:sqlite
  • 操作系统:windows

【问题讨论】:

  • 我认为没有简单的解决方案来禁用复数。

标签: strapi


【解决方案1】:

无法禁用复数,但有一种解决方法:

  1. 打开文件 api\araba\models\araba.settings.json(araba 是你的 API 名称)
  2. 将键 displayName 添加到
  3. 的 info 部分

    "info": {
       "name": "Araba",
       "displayName": "arabalar"
     },

  1. 创建文件 admin/src/components/LeftMenuLinkSection/index.js
  2. 将文件 node_modules/strapi-admin/admin/src/components/LeftMenuLinkSection/index.js 的内容复制到其中
  3. 更改此设置

   <LeftMenuLink
     location={location}
     key={index}
     iconName={link.icon}
     label={link.label}
     destination={getLinkDestination(link)}
   /> 

到这里

       <LeftMenuLink
         location={location}
         key={index}
         iconName={link.icon}
         label={link.schema && link.schema.info.displayName ? link.schema.info.displayName : link.label}
         destination={getLinkDestination(link)}
        />

yarn strapi build

【讨论】:

  • 行得通,谢谢!我有一个关于管理面板的问题。我在 node_modules 中自定义管理面板。我觉得这不是最佳做法。有没有更好的方法来做到这一点?
  • 是的,您可以将任何文件从 /node_modules/strapi-admin/admin/src/ 复制到 /admin/src/ 并在那里进行更改。不要在 node_modules 中自定义此文件。
  • 我认为这在 v3.x 中不再可能,我做了您建议的更改,但没有任何更改。我确实查看了它,这些是“链接”对象属性:{目标:“/plugins/content-manager/collectionType/application::slider-inicio.slider-inicio”,图标:“圆圈”,isDisplayed:true, label: "SliderInicios"...] 'schema' 属性不存在。
  • 在这里你可以看到如何为新版本做这件事,它几乎是一样的,但在其他地方:github.com/strapi/strapi/issues/6332
【解决方案2】:

恕我直言,正确(也是最简单)的解决方案可以在这个 Strapi 问题中找到:https://github.com/strapi/strapi/issues/8915#issuecomment-748436263。我会简单地报告它:

其实我只是想出了一个更简单的方法来解决这个问题。

我查看了代码,发现这个包: https://www.npmjs.com/package/pluralize 用于复数。 它有一个内置方法来添加新规则。所以我所做的就是我 添加了我当地语言(丹麦语)中的单词/单词,它们是 在这个文件中翻译错误:/config/functions/bootstrap.js 在应用启动之前执行。

像这样:

'use strict';

const pluralize = require('pluralize')

/**
 * An asynchronous bootstrap function that runs before
 * your application gets started.
 *
 * This gives you an opportunity to set up your data model,
 * run jobs, or perform some special logic.
 *
 * See more details here: https://strapi.io/documentation/developer-docs/latest/setup-deployment-guides/configurations.html#bootstrap
 */

module.exports = () => {
    // Add danish plural versions of collection names
    pluralize.addPluralRule('side', 'sider')
    pluralize.addPluralRule('aktivitet', 'aktiviteter')  }; 

然后让服务器重新启动。

【讨论】:

  • 谢谢!我让它在 Strapi 3.6.1 中工作。我可以在这里添加一件事:您在每个集合 JSON 中定义的名称,例如:“info”:{“name”:“Werk”,“description”:“”},必须是单数版本才能使其正常工作.
【解决方案3】:

无法在 Strapi 中禁用复数。

【讨论】:

    【解决方案4】:

    上述解决方案在 Strapi 版本 3 中不起作用。在版本 3.x 中,您必须这样做:

    1. 用“displayName”扩展你的模型,打开文件 api\{model}\models\{model}.settings.json 并添加一个新属性

      "info": { "name": "xy", "displayName": "Display Name" }
      
    2. admin\src\containers\LeftMenu\utils\generateModelsLinks.js 下创建一个新文件(从 node_modules\strapi-admin\admin\src\containers\LeftMenu 复制并粘贴原始文件\utils)

    3. 将第 (11) 行从

      label: link.info.label
      

      label: link.info && link.info.displayName ? link.info.displayName
      

    【讨论】:

      猜你喜欢
      • 2015-11-08
      • 2023-03-18
      • 2016-07-14
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 2010-09-17
      • 2012-10-03
      • 1970-01-01
      相关资源
      最近更新 更多