不在模型定义上翻译键

Not translating key on model define

问题

使用18next.t函数翻译键,得到的是键而不是翻译后的键值

环境

续集@5.22.4

i18next@21.3.3

模型定义candidate.js

...
module.exports = (sequelize, DataTypes) => {
  const Candidate = sequelize.define('Candidate', {
    status: {
      type: DataTypes.ENUM,
      values: [
        ...
      ],
    },
    type: {
      type: DataTypes.ENUM,
      values: [types.CANDIDATE, types.EMPLOYEE],
      validate: {
        isIn: {
          args: [allowedTypes],
          msg: i18next.t('candidate-invalid-type', { allowedTypes, joinArrays: ', ' })
...

结果:

candidate-invalid-type

预计:

{Custom error message located on lang.json}

我被发现的原因是语言延迟加载

所以我选择使用 sequelize 模型字段 属性 msg 作为 getter 属性

效果很好

替换

msg: i18next.t('candidate-invalid-type', { allowedTypes, joinArrays: ', ' })

对于

get msg() { return i18next.t('candidate-invalid-type', { allowedTypes, joinArrays: ', ' })}

完整的代码片段

...
module.exports = (sequelize, DataTypes) => {
  const Candidate = sequelize.define('Candidate', {
    status: {
      type: DataTypes.ENUM,
      values: [
        ...
      ],
    },
    type: {
      type: DataTypes.ENUM,
      values: [types.CANDIDATE, types.EMPLOYEE],
      validate: {
        isIn: {
          args: [allowedTypes],
          get msg() { return i18next.t('candidate-invalid-type', { allowedTypes, joinArrays: ', ' })}
...