You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express中使用i18next翻译验证错误消息时全部显示“Invalid value”的问题求助

Express中使用i18next翻译验证错误消息时全部显示“Invalid value”的问题求助

我在Express项目里安装了i18next相关包并启用了国际化功能,配置文件是middleware\localization.ts:

import i18next from "i18next";
import Backend from "i18next-fs-backend";
import { LanguageDetector } from "i18next-http-middleware";

i18next
  .use(Backend)
  .use(LanguageDetector)
  .init({
    lng: "sr",
    ns: ["validation"],
    fallbackLng: "en",
    backend: { loadPath: `../lang/{lng}/{ns}.json` }
  });

同时我也编写了对应的翻译文件lang/sr/validation.json,里面包含了所有验证相关的翻译文本。

接下来我在验证规则文件里用i18next来生成错误提示,比如validation\authSchemas.ts里的注册验证规则:

import { checkSchema } from "express-validator";
import i18next from "i18next";
import moment from "moment";

// 输入验证与清理
export const registerSchema = checkSchema({
  email: {
    notEmpty: { errorMessage: i18next.t("field.empty", { attr: i18next.t("attrs.email") }) },
    isEmail: { errorMessage: i18next.t("email.email", { attr: i18next.t("attrs.email") }) }
  },
  password1: {
    notEmpty: { errorMessage: i18next.t("field.empty", { attr: i18next.t("attrs.password1") }) }
  },
  password2: {
    notEmpty: { errorMessage: i18next.t("field.empty", { attr: i18next.t("attrs.password2") }) },
    custom: {
      errorMessage: i18next.t("password2.confirmed"),
      options: (value, { req }) => value === req.body.password1
    }
  },
  phone: {
    notEmpty: { errorMessage: i18next.t("field.empty", { attr: i18next.t("attrs.phone") }) },
    matches: {
      errorMessage: i18next.t("phone.regex"),
      options: /\+381 \d{2} \d{6,7}/gm
    }
  },
  firstName: {
    notEmpty: { errorMessage: i18next.t("field.empty", { attr: i18next.t("attrs.firstName") }) },
    isAlpha: { errorMessage: i18next.t("field.alpha", { attr: i18next.t("attrs.firstName") }) }
  },
  lastName: {
    notEmpty: { errorMessage: i18next.t("field.empty", { attr: i18next.t("attrs.lastName") }) },
    isAlpha: { errorMessage: i18next.t("field.alpha", { attr: i18next.t("attrs.lastName") }) }
  },
  birthDate: {
    notEmpty: { errorMessage: i18next.t("field.empty", { attr: i18next.t("attrs.birthDate") }) },
    isDate: { errorMessage: i18next.t("field.date", { attr: i18next.t("attrs.birthDate") }) },
    custom: {
      errorMessage: i18next.t("birthDate.adult"),
      options: (value) => {
        const age = moment().diff(new Date(value), "years");
        return age >= 18;
      }
    }
  }
});

路由配置里也正确引用了这个验证规则,routes\index.ts:

import { Router } from "express";

import controller from "../controllers/authController";
import { requireAuth } from "../middleware/auth";
import { limitAuth } from "../middleware/rateLimits";
import { loginSchema, registerSchema } from "../validation/authSchemas";

const router = Router();

// 输入验证与清理
router.post("/registracija", limitAuth, registerSchema, controller.register);

但是现在点击提交按钮后,所有的验证错误消息都显示成了Invalid value,如下所示:

Invalid value

有没有大佬知道怎么解决这个问题,让正确的翻译错误消息显示出来?

备注:内容来源于stack exchange,提问作者IgorArnaut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 18:13:04