Express with i18next in Typescript.

huangapple go评论61阅读模式
英文:

Express with i18next in Typescript

问题

我最近在我的Express API中添加了国际化功能。我找到了一个示例 https://github.com/bmanley91/express-i18n-example/blob/master/src/server.js 。我按照这个示例一步一步地进行了一些小的修改。但主要的区别在于这个示例是用JavaScript编写的,而我的项目是用TypeScript编写的。

出于示例目的,我将代码简化了。

// services/i18next.ts

import i18next from 'i18next';
import en_GB_translation from '../resources/locales/en_GB/translation.json';
const resources = {
    en_GB: {
        translation: en_GB_translation
    }
}
i18next
    .init(
        {
            resources,
            lng: "en_GB",
            supportedLngs: ["en_GB"],
            fallbackLng: "en_GB"
        }
    )
export default i18next;
// index.ts
const i18nextMiddleware = require('i18next-express-middleware');
import i18next from './services/i18next';
app.use(i18nextMiddleware.handle(i18next));
app.use('/users', usersRoute);
export const create = async (req: Request, res: Response, next: NextFunction) => {
   const response =  req.t("successful");
   res.status(200);
   res.send(response);
}

但TypeScript报错了:

Property 't' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

是否有人实现了相同的功能并遇到了相同的问题?

英文:

I added internationalization into my Express API recently. I found example https://github.com/bmanley91/express-i18n-example/blob/master/src/server.js .
I followed that example step by step with some little modifications. But the main different is that example is written in JavaScript and my project is written in Typescript.

For example purposes I minimized code.

// services/i18next.ts

import i18next from &#39;i18next&#39;
import en_GB_translation from &#39;../resources/locales/en_GB/translation.json&#39;
const resources = {
    en_GB: {
        translation: en_GB_translation
    }
}
i18next
    .init(
        {
            resources,
            lng: &quot;en_GB&quot;,
            supportedLngs: [&quot;en_GB&quot;],
            fallbackLng: &quot;en_GB&quot;
        }
    )
export default i18next
// index.ts
const i18nextMiddleware = require(&#39;i18next-express-middleware&#39;)
import import i18next from &#39;./services/i18next&#39;
app.use(i18nextMiddleware.handle(i18next))
app.use(&#39;/users&#39;, usersRoute)
export const create = async (req: Request, res: Response, next: NextFunction) =&gt; {
   const response =  req.t(&quot;successful)
   res.status(200);
   res.send(response)
}

But Typescript throw error

Property &#39;t&#39; does not exist on type &#39;Request&lt;ParamsDictionary, any, any, ParsedQs, Record&lt;string, any&gt;&gt;&#39;.

Is here someone who implemented same thing and ran into the same problem ?

答案1

得分: 1

卸载i18next-express-middleware并安装i18next-http-middleware

这是express-i18next的最新且更好的包。

然后:

// index.ts
const i18nextMiddleware = require('i18next-http-middleware');
import i18next from './services/i18next';
app.use(i18nextMiddleware.handle(i18next));
app.use('/users', usersRoute);
英文:

Just uninstall i18next-express-middleware and install i18next-http-middleware.

It is newest and better package for express-i18next

Then:

// index.ts
const i18nextMiddleware = require(&#39;i18next-http-middleware&#39;)
import i18next from &#39;./services/i18next&#39;
app.use(i18nextMiddleware.handle(i18next))
app.use(&#39;/users&#39;, usersRoute)

答案2

得分: 0

解决方法是在 .d.ts 文件中声明 "t" 函数。

declare global {
  namespace Express {
    interface Request {
      t: (...args: any) => void
    }
  }
}

export {}
英文:

The solution is to declare "t" function in .d.ts file.

declare global {
  namespace Express {
    interface Request {
      t: (...args: any) =&gt; void
    }
  }
}

export {}

huangapple
  • 本文由 发表于 2023年2月7日 03:41:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定